首页 > 解决方案 > 检查音频是否已经加载

问题描述

问题:我如何知道从远程源获取音频的操作(例如,使用 加载播放器 player.setUrl(url1, preload: true))是否已经为此播放器完成?

    AudioPlayer player = AudioPlayer();
     
    // Desired:
    // `true` if the `load()` action has been completed and that audio is currently 
    // `loaded` in the player (i.e. it is not necessary to fetch that audio again 
    // in order to play it)
    bool loaded = player.hasAudio; // false 

    // Once this is awaited, the player now has an audio `loaded`  
    await player.setUrl(url1, preload: true); 

    loaded = player.hasAudio; // true

换句话说,我想要的是连续调用player.setUrl(url1, preload: true)两次,获取两次数据。

我正在寻找与player.hasAudio我上面的示例等效的属性。或另一种获得类似结果的方法。

标签: flutterjust-audio

解决方案


好的,所以根据文档,我可以为我的用例推断这一点:

// `true` if the `load()` action has been completed and an audio is currently 
// `loaded` in the player
bool loaded = player.processingState == ProcessingState.ready ||
 player.processingState == ProcessingState.completed ||  
 player.processingState == ProcessingState.buffering;

// Or with less code but probably less intuitive
bool loaded = player.processingState.index > ProcessingState.loading.index;


loadedtrue如果之前已加载播放器并且:

  • 播放结束:player.playing == true && ProcessingState.completed
  • 播放尚未开始:player.playing == true && player.processingState == ProcessingState.ready
  • 正在播放:player.playing == true && player.processingState == ProcessingState.ready
  • 播放已暂停:player.playing == false && player.processingState == ProcessingState.ready
  • 从暂停状态恢复播放(触发多个状态更改)
    1. player.playing == true && player.processingState == ProcessingState.ready
    2. 然后player.playing == true && player.processingState == ProcessingState.buffering
    3. 然后player.playing == true && player.processingState == ProcessingState.ready

从文档:

重要的是要了解,即使在 时playing == true,实际上也不会听到任何声音,除非processingState == ready这表明缓冲区已填满并准备好播放。


至于当前加载的AudioSource,我还没有找到暴露当前加载AudioSource的数据的方法……


推荐阅读