小编Sai*_*z K的帖子

使用 expo-av 动态要求音频的替代解决方案

我正在开发一个音频应用程序,我一次下载所有音频文件,然后使用 expo-av 库播放它们。问题是当我尝试要求音频文件时,出现以下错误

Invalid call at line 77: require(audioUrl)
Failed building JavaScript bundle.
Run Code Online (Sandbox Code Playgroud)

我知道我们不能在 react-native 中动态地要求。我想知道是否有任何替代解决方案来播放下载的音频?

我需要下载音频文件的功能

const _loadNewPlaybackInstance = async isPlaying => {
    if (playBackInstance != null) {
      await playBackInstance.unloadAsync()
      await setPlaybackInstance(null)
    }
    const source = require(audioUrl)
    const initialStatus = {
      shouldPlay: isPlaying,
      volume,
      rate: audioRate,
      shouldCorrectPitch: true,
      pitchCorrectionQuality: Audio.PitchCorrectionQuality.Low
    }
    const { sound, status } = await Audio.Sound.createAsync(
      source,
      initialStatus,
      _onPlaybackStatusUpdate
    )
    setPlaybackInstance(sound)
    _updateScreenForLoading(false)
  }
Run Code Online (Sandbox Code Playgroud)

javascript react-native expo

0
推荐指数
1
解决办法
690
查看次数

标签 统计

expo ×1

javascript ×1

react-native ×1