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

TrackPlayer.removeUpcomingTracks crashes with RNTP 3.1.0

Open hvihvi opened this issue 2 years ago • 3 comments

Describe the Bug The app crashes upon calling TrackPlayer.removeUpcomingTracks() with RN 0.68.0 and RNTP 3.1.0.

--------- beginning of crash
08-26 16:33:43.750 27413 27413 E AndroidRuntime: FATAL EXCEPTION: main
08-26 16:33:43.750 27413 27413 E AndroidRuntime: Process: com.rn_0680_rntp_test, PID: 27413
08-26 16:33:43.750 27413 27413 E AndroidRuntime: java.lang.IllegalArgumentException
08-26 16:33:43.750 27413 27413 E AndroidRuntime: 	at com.google.android.exoplayer2.util.Assertions.checkArgument(Assertions.java:39)
08-26 16:33:43.750 27413 27413 E AndroidRuntime: 	at com.google.android.exoplayer2.ExoPlayerImpl.removeMediaItemsInternal(ExoPlayerImpl.java:2189)
08-26 16:33:43.750 27413 27413 E AndroidRuntime: 	at com.google.android.exoplayer2.ExoPlayerImpl.removeMediaItems(ExoPlayerImpl.java:655)
08-26 16:33:43.750 27413 27413 E AndroidRuntime: 	at com.doublesymmetry.kotlinaudio.players.QueuedAudioPlayer.removeUpcomingItems(QueuedAudioPlayer.kt:192)
08-26 16:33:43.750 27413 27413 E AndroidRuntime: 	at com.doublesymmetry.trackplayer.service.MusicService.removeUpcomingTracks(MusicService.kt:238)
08-26 16:33:43.750 27413 27413 E AndroidRuntime: 	at com.doublesymmetry.trackplayer.module.MusicModule$removeUpcomingTracks$1.invokeSuspend(MusicModule.kt:328)
08-26 16:33:43.750 27413 27413 E AndroidRuntime: 	at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:33)
08-26 16:33:43.750 27413 27413 E AndroidRuntime: 	at kotlinx.coroutines.DispatchedTask.run(DispatchedTask.kt:106)
08-26 16:33:43.750 27413 27413 E AndroidRuntime: 	at android.os.Handler.handleCallback(Handler.java:938)
08-26 16:33:43.750 27413 27413 E AndroidRuntime: 	at android.os.Handler.dispatchMessage(Handler.java:99)
08-26 16:33:43.750 27413 27413 E AndroidRuntime: 	at android.os.Looper.loopOnce(Looper.java:226)
08-26 16:33:43.750 27413 27413 E AndroidRuntime: 	at android.os.Looper.loop(Looper.java:313)
08-26 16:33:43.750 27413 27413 E AndroidRuntime: 	at android.app.ActivityThread.main(ActivityThread.java:8669)
08-26 16:33:43.750 27413 27413 E AndroidRuntime: 	at java.lang.reflect.Method.invoke(Native Method)
08-26 16:33:43.750 27413 27413 E AndroidRuntime: 	at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:571)
08-26 16:33:43.750 27413 27413 E AndroidRuntime: 	at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1135)
08-26 16:33:43.750 27413 27413 E AndroidRuntime: 	Suppressed: kotlinx.coroutines.DiagnosticCoroutineContextException: [StandaloneCoroutine{Cancelling}@796c4c4, Dispatchers.Main]

From my understanding, the kotlin code calls exoPlayer.removeMediaItems with wrong arguments ? At this location https://github.com/doublesymmetry/KotlinAudio/blob/ff5f1806ed38308250b9ffd80b2f7a9094bb8fcf/kotlin-audio/src/main/java/com/doublesymmetry/kotlinaudio/players/QueuedAudioPlayer.kt#L189.

Steps To Reproduce Clone the sandbox project available at https://github.com/hvihvi/react_native_sandbox/tree/master/RN_0680_RNTP_test

git clone [email protected]:hvihvi/react_native_sandbox.git
cd react_native_sandbox/RN_0680_RNTP_test
yarn install
yarn android # should crash

The steps to create the sandbox were:

npx react-native init RN_0680_RNTP310 --version 0.68.0
yarn add [email protected]
  • setup RNTP (service.js...) Add an effect on App.js to call removeUpcomingTracks:
  useEffect(() => {
    TrackPlayer.setupPlayer().then(TrackPlayer.removeUpcomingTracks);
  }, []);

yarn android The app crashes. Code To Reproduce Please provide a simple code example that allows others to replicate the bug.

Environment Info: Paste the results of npx react-native info System: OS: macOS 12.5 CPU: (12) x64 Intel(R) Core(TM) i7-9750H CPU @ 2.60GHz Memory: 63.25 MB / 16.00 GB Shell: 5.8.1 - /bin/zsh Binaries: Node: 16.14.0 - /usr/local/bin/node Yarn: 1.22.19 - /usr/local/bin/yarn npm: 8.3.1 - /usr/local/bin/npm Watchman: 2022.03.21.00 - /usr/local/bin/watchman Managers: CocoaPods: 1.11.3 - /usr/local/bin/pod SDKs: iOS SDK: Platforms: DriverKit 21.4, iOS 15.5, macOS 12.3, tvOS 15.4, watchOS 8.5 Android SDK: Not Found IDEs: Android Studio: 2021.2 AI-212.5712.43.2112.8815526 Xcode: 13.4.1/13F100 - /usr/bin/xcodebuild Languages: Java: 11.0.15 - /usr/bin/javac npmPackages: @react-native-community/cli: Not Found react: 17.0.2 => 17.0.2 react-native: 0.68.0 => 0.68.0 react-native-macos: Not Found npmGlobalPackages: react-native: Not Found Paste the exact react-native-track-player version you are using: 3.1.0 Real device? Or simulator? Real device What OS are you running? Android

How I can Help What can you do to help resolve this? I'll try to provide informations and investigate further, but I'm a bit stuck on the android part Have you investigated the underlying JS or Swift/Android code causing this bug? Yes Can you create a Pull Request with a fix? Yes if I find the fix

hvihvi avatar Aug 26 '22 15:08 hvihvi

I'm not sure how to run the doublesymmetry/kotlinaudio project to make sure, but I think the issue is with this part:

https://github.com/doublesymmetry/KotlinAudio/blob/main/kotlin-audio/src/main/java/com/doublesymmetry/kotlinaudio/players/QueuedAudioPlayer.kt#L192

    /**
     * Removes all the upcoming items, if any (the ones returned by [next]).
     */
    fun removeUpcomingItems() {
        val lastIndex = queue.lastIndex

        exoPlayer.removeMediaItems(currentIndex, lastIndex)
        queue.subList(currentIndex, lastIndex).clear()
    }

queue.lastIndex returns -1 when empty according to its javadoc, my guess is that the call exoPlayer.removeMediaItems(0, -1) throws an exception when exoplayer checks for its params at ExoplayerImpl:2191:

Assertions.checkArgument(
        fromIndex >= 0 && toIndex >= fromIndex && toIndex <= mediaSourceHolderSnapshots.size());

Because toIndex (-1) is lower than fromIndex (0).

hvihvi avatar Aug 26 '22 17:08 hvihvi

@hvihvi FYI, here's you you build and locally work with KotlinAudio in the context of RNTP:

https://github.com/doublesymmetry/react-native-track-player/tree/main/example#kotlinaudio

I'm taking a look at this issue and your PR now.

jspizziri avatar Aug 29 '22 12:08 jspizziri

@hvihvi now that your KotlinAudio PR was merged, we just need to wait for a release and bump the build.gradle file.

jspizziri avatar Aug 30 '22 18:08 jspizziri

This should be fixed in v3.2.0

jspizziri avatar Sep 26 '22 15:09 jspizziri

Hello @jspizziri, I am on v3.2.0 and still this is happening. Running on Android 13, here's a log on my Firebase Crashlytics.

Screenshot 2023-01-12 at 11 18 44

Dajust avatar Jan 12 '23 10:01 Dajust

@Dajust if you can repro in the example app, I'll reopen.

jspizziri avatar Jan 12 '23 10:01 jspizziri

Hi, thanks for your response @jspizziri. Upon further investigation, I found something else was causing this crash and found a way around it. Sorry for the stress.

Dajust avatar Jan 12 '23 13:01 Dajust

@Dajust do you remember what it was that caused this issue for you? We have a frequent error happening using the nightly tag, but it is slightly different as it calls remove instead of removeUpcomingTracks

I have not been able to reproduce this issue, no matter what arguments I pass to the RNTP remove method:

com.google.android.exoplayer2.util.Assertions.checkArgument Assertions.java:39
com.google.android.exoplayer2.ExoPlayerImpl.removeMediaItemsInternal ExoPlayerImpl.java:2189
com.google.android.exoplayer2.ExoPlayerImpl.removeMediaItems ExoPlayerImpl.java:655
com.google.android.exoplayer2.BasePlayer.removeMediaItem BasePlayer.java:79
com.doublesymmetry.kotlinaudio.players.QueuedAudioPlayer.remove QueuedAudioPlayer.kt:155
com.doublesymmetry.kotlinaudio.players.QueuedAudioPlayer.remove QueuedAudioPlayer.kt:168
com.doublesymmetry.trackplayer.service.MusicService.remove MusicService.kt:276
com.doublesymmetry.trackplayer.module.MusicModule$remove$1.invokeSuspend MusicModule.kt:317
kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith ContinuationImpl.kt:33
kotlinx.coroutines.DispatchedTask.run DispatchedTask.kt:106
android.os.Handler.handleCallback Handler.java:938
android.os.Handler.dispatchMessage Handler.java:99
android.os.Looper.loopOnce Looper.java:233
android.os.Looper.loop Looper.java:344
android.app.ActivityThread.main ActivityThread.java:8212
java.lang.reflect.Method.invoke Method.java
com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run RuntimeInit.java:584
com.android.internal.os.ZygoteInit.main ZygoteInit.java:1034

Any ideas what could be going wrong?

mitchdowney avatar Mar 15 '23 16:03 mitchdowney