react-native-track-player: Background mode not working ?
Issue
I’m currently use react-native-track-player to stream audio from Internet. Works fine.
I would like now to use it in background mode, to be able to continue to listen to radio if my ap is in background mode.
I followed instructions from https://react-native-kit.github.io/react-native-track-player/background/, so add capabilities in xCode, with should be enough.
It doesn’t work so I also add
TrackPlayer.updateOptions({
stopWithApp: false
});
but stil not working.
Any idea or experience with this audio library in background mode ? Thanks !
Configuration
“react-native-track-player”: “^1.1.8”
System: OS: macOS 10.15.1 CPU: (8) x64 Intel® Core™ i7-4790K CPU @ 4.00GHz Memory: 1.45 GB / 24.00 GB Shell: 3.2.57 - /bin/bash Binaries: Node: 12.13.1 - /usr/local/bin/node npm: 6.13.1 - /usr/local/bin/npm SDKs: iOS SDK: Platforms: iOS 13.2, DriverKit 19.0, macOS 10.15, tvOS 13.2, watchOS 6.1 IDEs: Xcode: 11.2.1/11B500 - /usr/bin/xcodebuild npmPackages: react: 16.9.0 => 16.9.0 react-native: 0.61.4 => 0.61.4 npmGlobalPackages: create-react-native-app: 2.0.2 react-native-cli: 2.0.1
About this issue
- Original URL
- State: closed
- Created 5 years ago
- Reactions: 4
- Comments: 21 (2 by maintainers)
try selecting Background processing as well in Background modes:
it worked for me.
@mynameisguy I’ve been trying to make this fixes for a week now. What you mentioned didn’t work for me. Some more info from my specific case:
It seems the app gets killed because it uses too much CPU in background?
We’ve run into this same issue in our application. After some thorough debugging in XCode, we’ve found the issue to be messages being issued the native system from the
useTrackPlayerProgresshook, as @zeritte mentioned above. The fix they suggested also works–making the sole parameter of the hook a number larger than the default of1000. I used50000in my case, because the time to app crash turned out to be geometric to the increase in that value, i.e.: a value of4000crashed in the same amount (roughly) that the1000setting would, but4800would run for over 20 minutes before crashing, where5000seems to never crash. The value of50000should give us an interminal play period, and is probably overkill.The root of this must be in the track player library. Aside from this duration parameter, there’s nothing else exposed through the API which we could be misusing.
Below is the code I used for the fix. It is very similar to @zeritte’s code:
It is also worth noting that I found this impossible to duplicate on any iOS simulator target that I tried, both before and after the iOS 14.0 release that broke so many media-related things. I was also unable to reproduce it on an old iPad Mini 3 running iOS 12 that was tethered to my Mac via XCode.
It was working well on android but on iOS it was getting signal 9 error from xcode. The only reason was this hook and after editing it the error is gone. I update my screens by using position and duration values useTrackPlayerProgress hook provides. However, they (my screens) were getting updated unnecessarily when I go to background. So I implemented a simple logic:
After getting app state information to state; I updated my component:
const { position, duration } = TrackPlayer.useTrackPlayerProgress(appState !== "active" ? 5000 : 500);Meaning that it does not unnecessarily updates my component when app is in the background mode. Hope this helps!This module does not work in background mode with iOS 13. Is there any plan to update it ? Or does anyone know another audio module that can work in background mode with iOS 13 ?
I had all of that stuff added too and it wasn’t working. All I had forgotten was
TrackPlayer.setupPlayer();so make sure the track player is initialized (even if the audio is already working!) because my audio worked, just not in the background, even though I forgot to initialize the track player.I experienced this error and it is not related to the library. Please check your app’s performance thru xcode. In my case, I had a memory leak caused by
useTrackPlayerProgress(500). After setting it to “1000”, the error is gone.You do not need the
Background Processingmode to be enabled for this react-native-track-player.I can also confirm that background playback does work in my production app in iOS 13.
For those experiencing the issue, are you able to reproduce the issue using the example app?
Anyone an idea ? The extension looses its usefulness if not being able to play in the background! Is it working well for someone ?