【问题标题】:Expo : Cannot load an AV asset from a null playback source世博会:无法从空播放源加载 AV 资产
【发布时间】:2022-12-17 01:45:37
【问题描述】:

嗨,我正在尝试使用 Expo-AV,但一直收到警告 [Unhandled promise rejection: Error: Cannot load an AV asset from a null playback source]

当第一次调用声音播放功能时,它会显示此警告并且不会播放,但是当我再次调用它播放的功能时,它会在没有警告的情况下播放。

const [sound, setSound] = useState();
const [isPlaying, setIsPlaying] = useState(false);

async function playSound() {
    console.log("Loading Sound");
    const { sound } = await Audio.Sound.createAsync(
      { uri },
      { shouldPlay: true }
    );
    setSound(sound);
    console.log("Playing Sound");
    setIsPlaying(true);
    await sound.playAsync();
    sound._onPlaybackStatusUpdate = (status) => {
      if (status.didJustFinish) {
        setIsPlaying(false);
        console.log("Finished");
      }
    };
  }
<TouchableOpacity onPress={playSound()}> 
  <Text>Play</Text>
</TouchableOpacity>

无论如何加载正确后是否可以播放。

【问题讨论】:

  • 检查uri是不是undefined
  • 没有 url 不是未定义的:(
  • 我也遇到了同样的问题。有趣的是,视频也开始崩溃。

标签: react-native expo expo-av


【解决方案1】:

这对我有用。试试看

const prepareToPlay = async (audioPath) => {
    const sound = new Audio.Sound();

    try {
        await sound.loadAsync({
            uri: audioPath.file,
            shouldPlay: true,
        });
        await sound.playAsync();
        // Your sound is playing!

        // Dont forget to unload the sound from memory
        // when you are done using the Sound object
        // await sound.unloadAsync();
    } catch (error) {
        // An error occurred!
        console.error('AUDIO PLAY: ', error);
    }
};

const audioToBePlayed =  {
    "file": "file:///Users/crosbyroads/Library/Developer/CoreSimulator/Devices/54AE93CW-1667-491F-A9C7-B457N8BC1207/data/Containers/Data/Application/5C442DA1-4EEA-4F24-93C2-38131484EE9E/Library/Caches/ExponentExperienceData/%crosbyroads%252FDraftSounds/DocumentPicker/EE3ECE8E-DFDC-46C9-8B68-4A8E0A8BB808.wav",
    "name": "assets_audio_OS_SB_160_Dm_Cream_Stack_1.wav",
    "size": 3180496,
    "type": "audio/wav",
  }

...

<Button onPress={() => prepareToPlay(audioToBePlayed)} title="Play"></Button>

/** Subscribe to my channel @ https://youtube.com/crosbyroads */

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-08-28
    • 2019-05-26
    • 2022-10-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-01-16
    • 1970-01-01
    相关资源
    最近更新 更多