react-native-sound icon indicating copy to clipboard operation
react-native-sound copied to clipboard

Sound does not play in background on iOS 11

Open joshbalfour opened this issue 7 years ago • 20 comments

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

joshbalfour avatar Oct 28 '17 18:10 joshbalfour

Hi, I have same issue. Did someone found a solution ?

Franklin62120 avatar Nov 03 '17 18:11 Franklin62120

Dealing with the same thing here.

tfaieta avatar Nov 04 '17 19:11 tfaieta

I haven't found anything that works, so I unfortunately had to roll my own. This does background music playing and the lock screen controls but only works on iOS: https://gist.github.com/joshbalfour/35afac16027c8745f7e6e71fabcc4226

If this is helpful enough I'll put it properly on github and npm.

joshbalfour avatar Nov 04 '17 19:11 joshbalfour

Thank you for this @joshbalfour ; it still doesn't work in my simulator. I'd read somewhere yesterday that this is probably because of some bug in simulator for iOS 11.

aditya-sodhani avatar Nov 05 '17 12:11 aditya-sodhani

It's just a simulator issue, it works if you run your application on your phone.

tfaieta avatar Nov 05 '17 17:11 tfaieta

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>):

<key>UIBackgroundModes</key>
<array>
    <string>audio</string>
</array>

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.

lgraubner avatar Nov 13 '17 12:11 lgraubner

@lgraubner Sound.setCategory('Playback'), doh! Works like a charm now. Yes, that can be overlooked easily.

frenkie avatar Dec 01 '17 17:12 frenkie

@frenkie does that work for both iOS and Android?

ShayanAhmad avatar Jan 04 '18 20:01 ShayanAhmad

@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.

frenkie avatar Jan 05 '18 12:01 frenkie

@lgraubner it worked like a charm.

batmanhit avatar Feb 28 '18 14:02 batmanhit

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.

screen shot 2018-05-27 at 9 53 28 pm

After rebuilding, the audio should work in the background.

dlipeles avatar May 28 '18 01:05 dlipeles

Hey, I was having the same issue, and I resolved it by adding the correct configuration to my Info.plist file. However, it's now working on the simulator but not on a real device. I am using the Ambient category though. Has anyone encounter such a case? Thanks in advance!

jonathanadler avatar Jul 18 '18 13:07 jonathanadler

@jonathanadler I have same issue , did u solve it ?

sployad avatar Aug 23 '18 14:08 sployad

@sployad No, not yet

jonathanadler avatar Aug 23 '18 14:08 jonathanadler

Same issue, works fine on debug for for real device and simulator, but not on archived release version

sergeylaptev avatar Jan 22 '19 11:01 sergeylaptev

Same issue, it's now working on the simulator but not on a real device

meiqi1992 avatar Feb 01 '19 01:02 meiqi1992

Same issue, can’t play in silence mode and background

fukuball avatar Feb 27 '19 14:02 fukuball

if it still doesn't work then try adding these lines.

UIBackgroundModes audio fetch processing

in your plist file. It worked for me. Or you can check mark those in xcode background modes as well.

MFazal1996 avatar Jul 30 '21 14:07 MFazal1996

If it still doesn't work, then try adding these lines. audio fetch processing error: terminated due to signal 9 song stops after 30 seconds I am using react-native-player This issue is getting only iOS device but android is working fine

chhotu50 avatar Apr 05 '24 04:04 chhotu50

still not working, do we need to add any native code in react-native project too ???

MrutyunjayaB avatar May 01 '24 07:05 MrutyunjayaB