react-native-sound: Sound does not play in background on iOS 11
expected: with Required background modes
set to
App plays audio or streams audio/video using AirPlay
App downloads content from the network
and the following in the code:
Sound.setActive(true)
Sound.enableInSilenceMode(true)
Sound.setCategory('Playback', true)
const sound = new Sound('https://s3.amazonaws.com/data.monstercat.com/blobs/21e6e0336ac16963b063b0ccba5797e1a44fcdf4', undefined, error => {
if (error) {
console.log('error loading sound', error)
return
}
sound.play(() => { })
})
When playing sound, and sending the app to the background, the audio should still play.
I’ve created a minimal setup which reproduces the issue: https://github.com/joshbalfour/rn-sound-issue
About this issue
- Original URL
- State: open
- Created 7 years ago
- Comments: 18
I found a solution. The docs seem to be incomplete. You have to add the following to
ios/Info.plist
(just before the closing</dict>
):Also don’t forget
Sound.setCategory('Playback')
. Thats enough to get it working.After rebuilding/restarting the simulator it works fine for me.
Edit: This is indeed mentioned in the docs. You guys might have overlooked it as I have.
If anyone is still seeing this issue, one reason is because iOS11 requires background audio capabilities to be enabled.
@lgraubner proposed one fix for this, another way is to select your project in Xcode, enter the Capabilities tab, and set background modes ‘on’, then toggle Audio, AirPlay, and Picture in Picture.
After rebuilding, the audio should work in the background.
It’s just a simulator issue, it works if you run your application on your phone.
@ShayanAhmad if I recall correctly, it should work out of the box for Android. Nothing special in code or the AndroidManifest to enable background audio. The
Sound.setCategory
call is only implemented (and needed) for iOS. We currently switched to a different library though because we needed streaming audio. React Native Sounds needs to download the entire file before it can play.