iOS : 应用程序在后台时无法播放声音

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

使用 react-native-sound and react-native-background-timer,我想在延迟 10 秒后播放声音。

这是我非常简单的代码:

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)
      }
    },
    []
  )
}

你能帮帮我吗?根据 react-native-background-timer,声音应该在后台播放

在 ios,您需要先从 Xcode 启用 UIBackgroundModes,然后将其添加到 Info.plist。

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

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