如何通过expo-av在react native expo项目上播放背景声音?

时间:2019-10-10 14:45:30

标签: react-native audio expo

我想在react-native博览会项目上播放背景声音。 为此,我使用了expo-av库。 但是我无法复制它,并且出现了一些错误。

错误:

[Unhandled promise rejection: Error: Cannot load an AV asset from a null playback source]

Stack trace:
  node_modules\expo-av\build\AV.js:115:14 in getNativeSourceAndFullInitialStatusForLoadAsync$
  node_modules\regenerator-runtime\runtime.js:45:44 in tryCatch
  node_modules\regenerator-runtime\runtime.js:271:30 in invoke
  node_modules\regenerator-runtime\runtime.js:45:44 in tryCatch
  node_modules\regenerator-runtime\runtime.js:135:28 in invoke
  node_modules\regenerator-runtime\runtime.js:170:17 in <unknown>
  node_modules\promise\setimmediate\core.js:45:7 in tryCallTwo
  node_modules\promise\setimmediate\core.js:200:23 in doResolve
  node_modules\promise\setimmediate\core.js:66:12 in Promise
  node_modules\regenerator-runtime\runtime.js:169:27 in callInvokeWithMethodAndArg
  node_modules\regenerator-runtime\runtime.js:192:38 in enqueue
  node_modules\regenerator-runtime\runtime.js:216:8 in async
  node_modules\expo-av\build\AV.js:88:7 in getNativeSourceAndFullInitialStatusForLoadAsync
  node_modules\expo-av\build\Audio\Sound.js:89:111 in loadAsync$
  node_modules\regenerator-runtime\runtime.js:45:44 in tryCatch
  node_modules\regenerator-runtime\runtime.js:271:30 in invoke
  node_modules\regenerator-runtime\runtime.js:45:44 in tryCatch
  node_modules\regenerator-runtime\runtime.js:135:28 in invoke
  node_modules\regenerator-runtime\runtime.js:170:17 in <unknown>
  node_modules\promise\setimmediate\core.js:45:7 in tryCallTwo
  node_modules\promise\setimmediate\core.js:200:23 in doResolve
  node_modules\promise\setimmediate\core.js:66:12 in Promise
  node_modules\regenerator-runtime\runtime.js:169:27 in callInvokeWithMethodAndArg
  node_modules\regenerator-runtime\runtime.js:192:38 in enqueue
  node_modules\regenerator-runtime\runtime.js:216:8 in async

enter image description here 这是我的零食代码。 https://snack.expo.io/@milutin/58ff49

请帮助我。

谢谢。

1 个答案:

答案 0 :(得分:1)

所以您做错了是将声音文件传递到uri属性,该属性期望文件传递URL,而不是文件本身。

因此,您所需要做的就是删除作为第一个参数传递的对象,并仅用require语句替换它。

const playbackObject = await Audio.Sound.createAsync(
  require('./assets/countDown.mp3'),
  { shouldPlay: true }
);

playbackObject.playAsync();

这里正在运行snack