flutter - 检查音频是否已经加载
问题描述
问题:我如何知道从远程源获取音频的操作(例如,使用 加载播放器 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
我上面的示例等效的属性。或另一种获得类似结果的方法。
解决方案
好的,所以根据文档,我可以为我的用例推断这一点:
// `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
的数据的方法……
推荐阅读
- python - Python JSON转储格式转义引号
- google-sheets - 从其他工作表中提取复杂数据
- c# - 在 OData 中匹配大小写属性时出现 AmbiguousMatchException
- css - 有没有办法使用 CSS 使字体真棒图标看起来更薄/更苗条?
- android - 迁移到 androidX 后,OkHttp 帖子内容在发布版本中为空
- spring-boot - Spring-Boot 1.5.10.release 与 apache-kafka-2.3.0 客户端的兼容性
- android - 使用特定的物理相机 ID 而非逻辑相机 ID 拍摄照片
- php - 带狮身人面像的智能过滤器
- javascript - 将保留按钮添加到表格的第 4 列。链接到灯箱/弹出窗口
- angular - 如何使用 Angular 6 中的一些 UI 即时编辑翻译文件?