Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
user-o712345 opened this issue Sep 24, 2024 · 4 comments

Comments

@user-o712345
Copy link

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

  • Formats: AAC, MP3
  • Directories:
    file:///data/user/0/audiopath...
    file:///storage/emulated/0/Android/data/audiopath...
    file:///storage/emulated/0//Download/audiopath...

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:

  • I could confirm that the audio files do exist in the directories listed above, via Android Studio or File Explorer.
  • TrackPlayer.getActiveTrack() does pick the 1st item in the array passed to TrackPlayer.add() , but still no audible play.
  • Having RNN and RNGH installed, I wrapped all the View registries with Navigation.registerComponent() and gestureHandlerRootHOC(). Not sure if there are lib conflicts, but for RNTP, I only used TrackPlayer.registerPlaybackService(() => PlaybackService)

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
Copy link
Contributor

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

see discussions:
podverse/podverse-rn#2117

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

@user-o712345
Copy link
Author

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:

  • file:///data/user/0/audiopath...
  • file:///storage/emulated/0/Android/data/audiopath...

@lovegaoshi
Copy link
Contributor

lovegaoshi commented Sep 25, 2024

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.

Copy link
Contributor

This issue is stale because it has been open 90 days with no activity. Remove stale label or comment or this will be closed in 7 days.

@github-actions github-actions bot added the Stale label Dec 25, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants