uni-app中uni.createInnerAudioContext总是提示{"errMsg":"MediaError","errCode":-5}报错,且播放一段时间闪退
uni-app中uni.createInnerAudioContext总是提示{“errMsg”:“MediaError”,“errCode”:-5}报错,且播放一段时间闪退
开发环境 | 版本号 | 项目创建方式 |
---|---|---|
Windows | Windows10 | HBuilderX |
产品分类:uniapp/App
PC开发环境操作系统:Windows
HBuilderX类型:正式
HBuilderX版本号:4.13
手机系统:Android
手机系统版本号:Android 14
手机厂商:华为
手机机型:oppo reno
页面类型:vue
vue版本:vue3
打包方式:云端
示例代码:
initInnerAudioContext() {
this.innerAudioContext = uni.createInnerAudioContext();
this.innerAudioContext.autoplay = true;
this.innerAudioContext.sessionCategory = 'ambient';
this.bindEvents();
},
// 销毁innerAudioContext实例,移除所有事件监听器
destroyInnerAudioContext() {
if (this.innerAudioContext) {
this.innerAudioContext.stop();
// 移除所有已绑定的事件监听器
this.innerAudioContext.offEnded();
this.innerAudioContext.offTimeUpdate();
this.innerAudioContext.offError();
this.innerAudioContext.destroy();
this.innerAudioContext = null;
}
},
// 监听音频播放错误事件
this.innerAudioContext.onError((error) => {
this.recordError(error);
// 记录错误信息并显示提示信息“音频播放错误”
console.error(`${this.playListKey}-音频播放错误: ` + JSON.stringify(error));
});
```
操作步骤:
- 直接播放音频
预期结果:
- 不提示莫名其妙问题
实际结果:
- 监听报错{"errMsg":"MediaError","errCode":-5}
bug描述:
- 监听报错 {"errMsg":"MediaError","errCode":-5} ,偶发,定位不到问题
更多关于uni-app中uni.createInnerAudioContext总是提示{"errMsg":"MediaError","errCode":-5}报错,且播放一段时间闪退的实战教程也可以访问 https://www.itying.com/category-93-b0.html
1 回复
更多关于uni-app中uni.createInnerAudioContext总是提示{"errMsg":"MediaError","errCode":-5}报错,且播放一段时间闪退的实战教程也可以访问 https://www.itying.com/category-93-b0.html
在 uni-app 中使用 uni.createInnerAudioContext
时,如果出现 {"errMsg":"MediaError","errCode":-5}
报错,并且播放一段时间后闪退,可能的原因和解决方法如下:
1. 音频文件路径问题
- 原因: 音频文件路径错误或文件无法访问。
- 解决方法: 确保音频文件路径正确,并且文件能够正常访问。如果是网络音频文件,确保 URL 是正确的,并且服务器允许跨域访问。
const innerAudioContext = uni.createInnerAudioContext();
innerAudioContext.src = 'https://example.com/your-audio-file.mp3'; // 确保 URL 正确
innerAudioContext.play();
2. 音频文件格式不支持
- 原因: 音频文件格式不被当前平台支持。
- 解决方法: 确保音频文件格式是平台支持的格式。例如,iOS 和 Android 通常支持 MP3、AAC 等格式。
3. 音频文件损坏
- 原因: 音频文件可能损坏或不完整。
- 解决方法: 尝试使用其他音频文件进行测试,确保文件没有损坏。
4. 网络问题
- 原因: 网络不稳定或音频文件加载失败。
- 解决方法: 确保网络连接稳定,并且音频文件能够正常加载。可以使用
onError
事件监听错误信息。
const innerAudioContext = uni.createInnerAudioContext();
innerAudioContext.src = 'https://example.com/your-audio-file.mp3';
innerAudioContext.onError((res) => {
console.log('播放错误:', res);
});
innerAudioContext.play();
5. 内存不足
- 原因: 播放音频时内存不足,导致应用闪退。
- 解决方法: 检查应用的内存使用情况,确保没有内存泄漏。可以尝试减少音频文件的大小或优化应用的内存管理。
6. 平台兼容性问题
- 原因: 不同平台对
uni.createInnerAudioContext
的支持可能存在差异。 - 解决方法: 检查 uni-app 的官方文档,确保 API 在当前平台上是支持的。如果有兼容性问题,可以尝试使用其他音频播放 API 或插件。
7. 音频播放器实例未正确销毁
- 原因: 如果多次创建
innerAudioContext
实例而未销毁,可能会导致资源占用过多。 - 解决方法: 在不需要播放音频时,使用
innerAudioContext.destroy()
销毁实例,释放资源。
const innerAudioContext = uni.createInnerAudioContext();
innerAudioContext.src = 'https://example.com/your-audio-file.mp3';
innerAudioContext.play();
// 在不需要播放时销毁实例
innerAudioContext.destroy();