uni-app 移动端audio的currentTime会大于duration

uni-app 移动端audio的currentTime会大于duration

操作步骤:

  • 安卓7,8系统

预期结果:

  • current最大值应该是duration

实际结果:

  • current会大于duration

bug描述:

  • 移动端uni.getBackgroundAudioManager()获取audio,播放完后currentTime会大于duration

| 项目创建方式 | 开发环境 | 版本号 |
|--------------|----------|--------|
| HBuilderX    | Windows  | 3.2.9  |
|              | Android  | 8.0    |
|              | 手机厂商  | 华为   |
|              | 手机机型  | 6x     |
|              | 页面类型  | vue    |

更多关于uni-app 移动端audio的currentTime会大于duration的实战教程也可以访问 https://www.itying.com/category-93-b0.html

2 回复

未复现。 用示例代码hello uni-app能出现你的问题吗? 不能的话你需要排查出来具体你哪个页面,甚至哪一行导致的。 找出来具体原因后提供一个能复现你描述的bug的最小化demo,让我们及时定位问题,及时修复。 【bug优先处理规则】https://ask.dcloud.net.cn/article/38139

更多关于uni-app 移动端audio的currentTime会大于duration的实战教程也可以访问 https://www.itying.com/category-93-b0.html


这是一个已知的音频播放器兼容性问题,主要出现在部分安卓系统上。

问题原因:

  1. 系统底层差异:不同安卓版本和厂商对音频播放器的实现存在差异,某些系统在音频播放结束后,currentTime的更新逻辑可能出现偏差。
  2. 时间戳精度问题:音频播放的时间计算可能存在微小误差,导致结束时currentTime略微超过duration
  3. 背景音频管理器的特殊性uni.getBackgroundAudioManager() 在后台播放时,时间同步机制可能受到影响。

解决方案:

  1. 边界值处理:在获取currentTime时进行判断:
const audioManager = uni.getBackgroundAudioManager()
const currentTime = Math.min(audioManager.currentTime, audioManager.duration)
  1. 监听播放结束事件:在onEnded回调中重置时间:
audioManager.onEnded(() => {
    audioManager.seek(0)
    // 或记录状态,避免使用大于duration的currentTime
})
  1. 使用进度时进行校验
// 计算进度时
const progress = audioManager.duration > 0 
    ? Math.min(audioManager.currentTime / audioManager.duration, 1)
    : 0
回到顶部