react-nativereact-native-sound

iOS : Can't play sound when the application is in the background


With react-native-sound and react-native-background-timer, I want to play a sound after 10 seconds delay.

Here is my very simple code :

import BackgroundTimer from 'react-native-background-timer'
import Sound from 'react-native-sound'

Sound.setCategory('Playback')
const soundEnd = new Sound('end.mp3', Sound.MAIN_BUNDLE)

const Timer = () => {
  useEffect(
    () => {
      const timeoutId = BackgroundTimer.setTimeout(() => {
        console.log('END setTimeout')
        soundEnd.play(() => {})
      }, 10000)

      return () => {
        BackgroundTimer.clearTimeout(timeoutId)
      }
    },
    []
  )
}

Can you help me ? According to react-native-background-timer, the sounds are supposed to play in the background


Solution

  • on ios, you need to first enable UIBackgroundModes from Xcode and then add this in Info.plist.

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

    https://github.com/zmxv/react-native-sound/issues/302

    enter image description here