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

Fatal Exception: android.app.RemoteServiceException$ForegroundServiceDidNotStartInTimeException

Open roganantonys opened this issue 9 months ago • 2 comments

🚨 Fatal Exception: ForegroundServiceDidNotStartInTimeException in react-native-track-player

🔴 Issue occurs only for users in production (not locally or in release APK testing).

📌 Bug Description

I am encountering the following error:

Fatal Exception: android.app.RemoteServiceException$ForegroundServiceDidNotStartInTimeException: Context.startForegroundService() did not then call Service.startForeground(): ServiceRecord{com.doublesymmetry.trackplayer.service.MusicService}

at android.app.ActivityThread.generateForegroundServiceDidNotStartInTimeException(ActivityThread.java:2501) at android.app.ActivityThread.throwRemoteServiceException(ActivityThread.java:2472) at android.app.ActivityThread.-$$Nest$mthrowRemoteServiceException() at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2780) at android.os.Handler.dispatchMessage(Handler.java:108) at android.os.Looper.loopOnce(Looper.java:226) at android.os.Looper.loop(Looper.java:328) at android.app.ActivityThread.main(ActivityThread.java:9242) at java.lang.reflect.Method.invoke(Method.java) at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:594) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1099)

  • This does not happen when running locally or testing a release APK.
  • However, users in production are experiencing crashes.
  • The error suggests that a foreground service was started but Service.startForeground() was not called in time.

🛠 Steps to Reproduce

I don't know how to reproduce this error in local but it is occurring very frequently for users


my version

"react-native-track-player": "^3.2.0", "react-native": 0.75.2 I have tested it in real device oppo - android 10

📝 Code Snippet

const playerState = usePlaybackState();

useEffect(() => {
    setupTrackPlayer();
}, []);

const setupTrackPlayer = async () => {
    try {
        const isSetup = await TrackPlayer.isServiceRunning(); // Check if already running
        if (!isSetup) {
            await TrackPlayer.setupPlayer(); // Set up only if not already running
        }
        setIsTrackPlayerReady(true);
    } catch (error) {
        console.warn(error);
    }
};

const start = async () => {
    try {
        await TrackPlayer.reset();
        await TrackPlayer.add({
            id: 'startSound',
            url: require('location to the mp3'),
            title: 'Track Started',
        });
        await TrackPlayer.play();
    } catch (error) {
        console.log('Error playing:', error);
    }
};

const end = async () => {
    try {
        await TrackPlayer.reset();
        await TrackPlayer.add({
            id: 'endSound',
            url: require('location to the mp3'),
            title: 'Track Ended',
        });
        await TrackPlayer.play();
    } catch (error) {
        console.log('Error playing:', error);
    }
};

roganantonys avatar Apr 01 '25 13:04 roganantonys

well for one, please use the issue template. they are there for a reason. for example i cant tell if ur on the long deprecated RNTP v3.2 or ur lockfile actually uses v4. and imo this emoji stuff is cringey. just use the template is good enough.

to begin with, u should know a bug report without any sorts of reproduction steps will most likely ends nowhere. i do have a crystal ball but im not willing to use it on ur app.

though luckily ur issue seemed familiar, and thats where some googling skills can carry u a long way.

lovegaoshi avatar Apr 01 '25 21:04 lovegaoshi

@lovegaoshi , I found a few related issues, but I was unable to find a definite solution in those chats. That's why I raised a new one, hoping to find a solution. If you look at the issue link you provided, you'll see that most of the users were also unable to reproduce the issue, just like I am.

roganantonys avatar Apr 02 '25 06:04 roganantonys

@roganantonys , we need a reproduction in the example app before we're able to look into this. If you're able to create a reproduction we'll take a deeper look.

jspizziri avatar Jun 30 '25 12:06 jspizziri