I have a remote URL that is played when an alarm (setTimeout) goes off.
The alarm should go off and the sound should play when app is in background.
How it works in Simulator:
The setTimeout goes off and sound plays, whether in background or not.
How it works on device:
The setTimeout goes off and sound plays in foreground mode.
If setTimeout goes off in background mode, sound doesn't play.
It starts playing immediately when I put app back in foreground.
I know my setTimeout is working in background because my console log is working.
BackgroundTimer.setTimeout(() => {
TrackPlayer.setupPlayer().then(async () => {
await TrackPlayer.add({
id: 'trackId',
url: streamUrl,
title: 'Track Title',
artist: 'Track Artist'
});
});
TrackPlayer.play()
console.log('this is working')
}, window.timeLeft * 1000)
and my info.plist:
<key>UIBackgroundModes</key>
<array>
<string>audio</string>
<string>fetch</string>
<string>remote-notification</string>
</array>
I have also tried setting up the player outside of the setTimeout.
Any help would be appreciated! Thank you
You need to also enable Background modes for AUDIO, AIRPLAY and PICTURE IN PICTURE mode in Xcode capability's section.
And put:
stopWithApp: false,
in your code
@maendamedia thanks for your reply!
I already had the audio, airplay, picture in picture enabled in XCode & info.plist.
I also added
TrackPlayer.setupPlayer().then(async () => {
TrackPlayer.updateOptions({
stopWithApp: false
})
await TrackPlayer.add({
id: 'trackId',
url: streamUrl,
title: 'Track Title',
artist: 'Track Artist'
});
});
Still no luck. Any other ideas? Thank you!
This is because once you go to background and there's no audio playing. iOS kills the application.
iOS only supports playing music in background if, you go to background while playing music.
then how can I play alarm audio when I get notification in background mode?
Hello,
Please i get a similar error, but mine is that the song begins to crack when the app in in background,
any assistance please
I have experienced this on iOS simulator a few times. Are you testing on real iOS device?
I have experienced this on iOS simulator a few times. Are you testing on real iOS device?
No i am not, i am testing it on an iOS simulator, that's where i get the error.
I don't have any more info other than for me this doesn't happen all the time. And it has never happened on a real device.
Try restarting your development machine? I know that is silly but...
oh okay, i'll try that. thanks
@violetforest any luck with this?
@dcvz I do not agree with you on this because one more library react-native-music-control already doing this So I think it's possible
oh okay, i'll try that. thanks
@curiousdustin I restarted my development machine, it still does that... the ios simulator does not play song in the background... although i now tried building on a real device and the song was playing in background!
Thank You.