react-native-track-player icon indicating copy to clipboard operation
react-native-track-player copied to clipboard

Setup Player hangs in android

Open Manpreet-Singh-Kamboj opened this issue 8 months ago • 17 comments


const setupPlayer = async () => {
  try {
    // Check if the player is already setup by trying to get the current track
    const currentTrack = await TrackPlayer.getActiveTrack();
    console.log(currentTrack);
    if (currentTrack !== null) {
      console.log('Player is already setup');
      return true;
    }
  } catch (error) {
    // If there's an error, it means the player is not set up yet
    console.log('Player is not setup, setting up now');
  }

  // Setup the player
  try {
    await TrackPlayer.setupPlayer()
      .then(() => {
        console.log('Player setup');
      })
      .catch(er => console.log(er));
    console.log('Player is ready');

    await TrackPlayer.updateOptions({
      capabilities: [Capability.Play, Capability.Pause],
      compactCapabilities: [Capability.Play, Capability.Pause],
    });

    return true;
  } catch (error) {
    console.error('Error setting up TrackPlayer:', error);
    return false;
  }
};

export default setupPlayer;

THis is my TrackPlayer Setup file and in android device the the setupPlayer() is called it just gets hanged in there no error or anything neither it gets setup. in IOS it works fine.

Manpreet-Singh-Kamboj avatar Apr 30 '25 08:04 Manpreet-Singh-Kamboj

@Manpreet-Singh-Kamboj Hi, I ran into the same issue — have you found any solution? I think it might be related to the latest version of React Native. I’ll try downgrading to 0.79.0. If I’m not mistaken, it was working correctly on that version.

Kirillsocivka avatar May 04 '25 10:05 Kirillsocivka

@Kirillsocivka I tried to a lot to fix it. But didn’t find any solution for it. So i just changed the library. I am now using react native audio pro. Here is the link https://rnap.dev/ . It’s a new library so it works fine for now i havent faced issues. You can try it out.

Manpreet-Singh-Kamboj avatar May 04 '25 11:05 Manpreet-Singh-Kamboj

react-native-audio-pro looks great, but the lack of a queue is unfortunately a dealbreaker for me. I tried my best to find a way to make react-native-track-player work and managed to do so. This is the patch that made things click for me on Android:

This is the patch that made things work for me:

diff --git a/android/src/main/java/com/doublesymmetry/trackplayer/service/MusicService.kt b/android/src/main/java/com/doublesymmetry/trackplayer/service/MusicService.kt
index baeebce2489e6f9e47ce2ebb9332072e31ba24d5..e52a053d99fcc568d03a0294840b0463f04d01c9 100644
--- a/android/src/main/java/com/doublesymmetry/trackplayer/service/MusicService.kt
+++ b/android/src/main/java/com/doublesymmetry/trackplayer/service/MusicService.kt
@@ -756,10 +756,15 @@ class MusicService : HeadlessJsTaskService() {
         return HeadlessJsTaskConfig(TASK_KEY, Arguments.createMap(), 0, true)
     }
 
-    // @MainThread
-    // fun onBind(intent: Intent?): IBinder {
-    //    return binder
-    // }
+    @MainThread
+    override fun onBind(intent: Intent): IBinder {
+        return binder
+    }
+
+    @MainThread
+    override fun onUnbind(intent: Intent): Boolean {
+        return super.onUnbind(intent)
+    }
 
     @MainThread
     override fun onTaskRemoved(rootIntent: Intent?) {

I did drop that into the @weights-ai/react-native-track-player fork I found in another issue. This makes the whole thing tick again. I do not necessarily understand what's going on, but I do know this fixes things. If someone wants to turn this into a pull request, feel free to do so!

I'm using pnpm to manage the fork and to ensure I don't have to update all imports:

pnpm add react-native-track-player@npm:@weights-ai/react-native-track-player
pnpm patch @weights-ai/react-native-track-player  

Then you apply the patch, and you run:

pnpm patch-commit '/Users/lei/Documents/Code/jellyfin-audio-player/node_modules/.pnpm_patches/@weights-ai/[email protected]'

I can confirm this makes react-native-track-player work on both iOS and Android. I'm still waiting on long-time test reports.

leinelissen avatar May 05 '25 18:05 leinelissen

@jonluca I couldn't create an issue on your repository, and I don't have time right now to do so. Is this something you can work with?

leinelissen avatar May 05 '25 18:05 leinelissen

Are you using the new architecture or old? What react native version?

I can probably change it so that it applies the onbind if needed

jonluca avatar May 05 '25 18:05 jonluca

Using new architecture, 0.79.2

leinelissen avatar May 05 '25 18:05 leinelissen

i dont know what's behind the decision to comment out onBind in MusicService - MusicModule need to bind that specific binder via https://github.com/doublesymmetry/react-native-track-player/blob/2765d09cb93541d89be94a4809320e4df33c0d47/android/src/main/java/com/doublesymmetry/trackplayer/module/MusicModule.kt#L55. though i believe this discussion is more suitable for @weights-ai/react-native-track-player's issue tracker.

lovegaoshi avatar May 05 '25 19:05 lovegaoshi

To be frank, I have no idea what's going on there necessarily. I was just looking for a way to get it working on Android with the new architecture. This fixed things for me.

leinelissen avatar May 07 '25 08:05 leinelissen

@leinelissen thanks for the fix it works

kesha-antonov avatar May 14 '25 10:05 kesha-antonov

I'm not sure why but when I tried with patch-package directly the setupPlayer function didn't work but when I tried the pnpm method it worked RN: 0.79.2 Expo: 53.0.9 @weights-ai/react-native-track-player fork

Joel-Mercier avatar May 14 '25 12:05 Joel-Mercier

I've published 4.1.5 with that patch. let me know if it works for you.

jonluca avatar May 19 '25 20:05 jonluca

@jonluca How to apply?

Manpreet-Singh-Kamboj avatar May 19 '25 20:05 Manpreet-Singh-Kamboj

Just install the latest version of @weights-ai/react-native-track-player

jonluca avatar May 19 '25 20:05 jonluca

@jonluca Do we need to install @weights-ai/react-native-track-player (not able to find the github page) directly or is there patch for original library(doublesymmetry). and @weights-ai/react-native-track-player will it support new architecture, android auto, carplay?

developerdhana avatar May 20 '25 03:05 developerdhana

The GitHub page is here: https://github.com/weights-ai/react-native-track-player. It supports new architecture (the patch fixes it breaking on Android new architecture). At least CarPlay is already supported, it does not depend on something happening in react-native-track-player. I'm not sure about Android Auto, but I imagine it's just the same.

leinelissen avatar May 20 '25 08:05 leinelissen

I'm running the patch in production on a couple thousand installs and I see no problems so far. Thanks @jonluca for publishing it.

leinelissen avatar May 20 '25 08:05 leinelissen

Hi @jonluca, when doing TrackPlayer.reset() with your module, I get this error

Handler (android.media.MediaCodec$EventHandler) {3dc9686} sending message to a Handler on a dead thread
                                                                                                    java.lang.IllegalStateException: Handler (android.media.MediaCodec$EventHandler) {3dc9686} sending message to a Handler on a dead thread
                                                                                                    	at android.os.MessageQueue.enqueueMessage(MessageQueue.java:560)
                                                                                                    	at android.os.Handler.enqueueMessage(Handler.java:786)
                                                                                                    	at android.os.Handler.sendMessageAtTime(Handler.java:735)
                                                                                                    	at android.os.Handler.sendMessageDelayed(Handler.java:705)
                                                                                                    	at android.os.Handler.sendMessage(Handler.java:643)
                                                                                                    	at android.media.MediaCodec.postEventFromNative(MediaCodec.java:5222)
2025-05-31 11:54:04.255 30710-31374 MessageQueue            com.megahit                          W  Handler (android.media.MediaCodec$EventHandler) {3dc9686} sending message to a Handler on a dead thread
                                                                                                    java.lang.IllegalStateException: Handler (android.media.MediaCodec$EventHandler) {3dc9686} sending message to a Handler on a dead thread
                                                                                                    	at android.os.MessageQueue.enqueueMessage(MessageQueue.java:560)
                                                                                                    	at android.os.Handler.enqueueMessage(Handler.java:786)
                                                                                                    	at android.os.Handler.sendMessageAtTime(Handler.java:735)
                                                                                                    	at android.os.Handler.sendMessageDelayed(Handler.java:705)
                                                                                                    	at android.os.Handler.sendMessage(Handler.java:643)
                                                                                                    	at android.media.MediaCodec.postEventFromNative(MediaCodec.java:5222)
2025-05-31 11:54:04.256 30710-31374 MessageQueue            com.megahit                          W  Handler (android.media.MediaCodec$EventHandler) {3dc9686} sending message to a Handler on a dead thread
                                                                                                    java.lang.IllegalStateException: Handler (android.media.MediaCodec$EventHandler) {3dc9686} sending message to a Handler on a dead thread
                                                                                                    	at android.os.MessageQueue.enqueueMessage(MessageQueue.java:560)
                                                                                                    	at android.os.Handler.enqueueMessage(Handler.java:786)
                                                                                                    	at android.os.Handler.sendMessageAtTime(Handler.java:735)
                                                                                                    	at android.os.Handler.sendMessageDelayed(Handler.java:705)
                                                                                                    	at android.os.Handler.sendMessage(Handler.java:643)
                                                                                                    	at android.media.MediaCodec.postEventFromNative(MediaCodec.java:5222)
2025-05-31 11:54:04.260 30710-31374 MessageQueue            com.megahit                          W  Handler (android.media.MediaCodec$EventHandler) {3dc9686} sending message to a Handler on a dead thread
                                                                                                    java.lang.IllegalStateException: Handler (android.media.MediaCodec$EventHandler) {3dc9686} sending message to a Handler on a dead thread
                                                                                                    	at android.os.MessageQueue.enqueueMessage(MessageQueue.java:560)
                                                                                                    	at android.os.Handler.enqueueMessage(Handler.java:786)
                                                                                                    	at android.os.Handler.sendMessageAtTime(Handler.java:735)
                                                                                                    	at android.os.Handler.sendMessageDelayed(Handler.java:705)
                                                                                                    	at android.os.Handler.sendMessage(Handler.java:643)
                                                                                                    	at android.media.MediaCodec.postEventFromNative(MediaCodec.java:5222)

Can you send me a way to contact you, to debug this together?

DennisdeWitNL avatar May 31 '25 09:05 DennisdeWitNL

At a cursory glance, this sounds like an issue with newArch not being supported? If that's the case please take a look at this PR https://github.com/doublesymmetry/react-native-track-player/pull/2478 which adds TurboModule support.

jspizziri avatar Jun 25 '25 21:06 jspizziri

Closing this until I get a response with a clarification to my question.

jspizziri avatar Jun 26 '25 12:06 jspizziri