问题:我如何知道从远程源获取音频的操作(例如使用 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
等效的属性。或者另一种获得类似结果的方法。
答案 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;
loaded
为 true
,如果播放器之前已加载并且:
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
player.playing == true && player.processingState == ProcessingState.ready
player.playing == true && player.processingState == ProcessingState.buffering
player.playing == true && player.processingState == ProcessingState.ready
来自文档:
<块引用>重要的是要明白,即使当 playing == true
时,也没有声音
除非 processingState == ready
表示缓冲区已满,可以播放了。
至于当前加载的AudioSource
,我还没有找到暴露当前加载的AudioSource
的数据的方法...