使用expo-av

时间:2019-12-23 05:11:53

标签: react-native expo

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

Invalid call at line 77: require(audioUrl)
Failed building JavaScript bundle.

我知道我们不能动态要求本机响应。我想知道是否有其他解决方案来播放下载的音频?

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

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)
  }

1 个答案:

答案 0 :(得分:0)

您是正确的,require用于静态资源,而不是动态资源。根据文档here,source参数可以是三种类型之一。

  
      
  • 来源(对象/数字/资产)-声音的来源。支持以下形式:      
        
    • 形式为{ uri: string, headers?: { [string]: string }, overrideFileExtensionAndroid?: string }的字典,带有指向网络上媒体文件的网络URL,[...]
    •   
    • 需要('path / to / file')在源代码目录中获取音频文件资产。
    •   
    • 音频文件资产的资产对象。
    •   
  •   

您可以将第一类参数用于动态资源。您可以这样做:

const source = { uri: audioUrl },其中audioUrl也可以是本地资源:file:///path/to/resource