doublesymmetry / react-native-track-player

A fully fledged audio module created for music apps. Provides audio playback, external media controls, background mode and more!
https://rntp.dev/
Apache License 2.0
3.3k stars 1.02k forks source link

RNTP - Play from Android file system with USB debugger is failing without error from Metro or React Native Debugger #2377

Open user-o712345 opened 1 month ago

user-o712345 commented 1 month ago

Possible Bug:

I want to play tracks from my local file system using react-native-track-player, but getting no audible audio play on the Android device that I am emulating and testing on. Plus, no error feedback on Metro or React Native Debugger.

What I Tried:

I have tried several audio formats and local directories, but still not getting past "ready" PlaybackState in TrackPlayer.addEventListener

I read through all RNFS, RNFetchBlob, RNFB related issues in this lib, but nothing helped.

Code To Reproduce:

const Player = ({list}) => {
  const [isPlayerReady, setIsPlayerReady] = useState(false);

  useEffect(() => {
    async function setup() {
      let isSetup = await SetupTrackPlayer();
      const queue = await TrackPlayer.getQueue();

      if (isSetup && queue.length <= 0) {
        await addTracksToQueue(list);
      }

      setIsPlayerReady(isSetup);
    }

    setup();
  }, [list]);

  const addTracksToQueue = async list => {
      list.docs.map(doc => ({
        id: docID,
        url: file:///data/user/0/audiopath...,  //tried other dir as stated above
        artwork: file:///data/user/0/imagepath...,  //also not loading
        title: doc.title,
        artist: doc.author,
      })),
    await TrackPlayer.add(tracks);
    await TrackPlayer.setRepeatMode(RepeatMode.Queue);
  };

//jsx

};

export async function SetupTrackPlayer() {
  let isSetup = false;

  try {
    await TrackPlayer.getActiveTrackIndex();
    isSetup = true;
  } catch {
    await TrackPlayer.setupPlayer();
    await TrackPlayer.updateOptions({
      android: {
        alwaysPauseOnInterruption: true,
        appKilledPlaybackBehavior: AppKilledPlaybackBehavior.ContinuePlayback,
      },
      capabilities: [
        Capability.Play,
        Capability.Pause,
        Capability.SkipToNext,
        Capability.SkipToPrevious,
      ],
      compactCapabilities: [Capability.Play, Capability.Pause],
    });

    isSetup = true;
  } finally {
    return isSetup;
  }
}

Current Status:

Env Info:

```
OS:Windows 11 10.0.22631
Android:12
Nodeversion:20.11.1
openjdk:11.0.22
react-native:0.72.15
react-native-track-player:4.1.1
audiopathreact-native-navigation:7.35.2
audiopathreact-native-reanimated:3.3.0
audiopathreact-native-gesture-handler:2.11.0
react-native-fs:2.20.0
hermesEnabled:true
newArchEnabled:false
androidmanifest.xml permissions:
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
    <uses-permission android:name="android.permission.MANAGE_EXTERNAL_STORAGE"/>


**How I can Help:**
- Have you investigated the underlying JS or Swift/Android code causing this bug? 
- If it is an overlook on my side, can you give some pointers about how to solve?
lovegaoshi commented 1 month ago

u need some special tricks to play local files on android 12+. its due to the "newly" introduced file access permissions.

see discussions: https://github.com/podverse/podverse-rn/issues/2117

and PRs https://github.com/lovegaoshi/azusa-player-mobile/pulls?q=is%3Apr+local+playback+ https://github.com/podverse/podverse-rn/pull/2154

user-o712345 commented 1 month ago

Thanks @lovegaoshi! I'll look through these.

On a quick skim, it seems you did not use MediaStore.Files (ie. file:///data/user/0/audiopath...) collection, but a directory that would look like file:///storage/emulated/0/Download/..., correct?

I'm just confirming, because someone had mentioned in one of the comments that they were able to use MediaStore.Files as the RNTP audio URL, without fully illustrating their solution. The thing is, I would much rather store the audio in one of the following directories:

lovegaoshi commented 1 month ago

just read the linked issue

The other solution is MANAGE_EXTERNAL_STORAGE which google will heavily scrutinize for apps on google play.

and my posted snippet literally says MediaStore.Audio.Media.EXTERNAL_CONTENT_URI. it queries all media files without a filter.