检查音频是否已经加载

时间:2021-06-25 05:16:39

标签: flutter just-audio

问题:我如何知道从远程源获取音频的操作(例如使用 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 等效的属性。或者另一种获得类似结果的方法。

1 个答案:

答案 0 :(得分:1)

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

// `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的数据的方法...

相关问题