0

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)
      }
    },
    []
  )
}
  • Everything works fine on Android and iOS when the app is in the foreground: the sound is played and the console.log('END setTimeout') is displayed
  • But when the application is in the background on iOS the sound is not played (but the console.log('END setTimeout') is displayed well)

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

Gaylord.P
  • 1,539
  • 2
  • 24
  • 54

1 Answers1

1

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

Muhammad Shaheem
  • 607
  • 5
  • 14