uni-app 音频播放错误 {"errMsg":"MediaError","errCode":-99}
uni-app 音频播放错误 {“errMsg”:“MediaError”,“errCode”:-99}
开发环境 | 版本号 | 项目创建方式 |
---|---|---|
Mac | 11.2 | HBuilderX |
产品分类:uniapp/App
PC开发环境操作系统:Mac
PC开发环境操作系统版本号:11.2
HBuilderX类型:正式
HBuilderX版本号:3.1.2
手机系统:Android
手机系统版本号:Android 10
手机厂商:安卓
手机机型:10
页面类型:vue
打包方式:云端
示例代码:
```javascript
created(){
this.innerAudioContext = uni.createInnerAudioContext();
this.innerAudioContext.onPlay(() => {
console.log('开始播放');
});
this.innerAudioContext.onError(res => {
console.log(res.errMsg);
console.log(res.errCode);
});
},
methods:{
play(href){
this.innerAudioContext.src = href;
this.innerAudioContext.stop();
this.innerAudioContext.play();
}
}
操作步骤: 一直点击播放即可,点击一次更换一个音频链接。
预期结果: 预期应正常播放
实际结果: 不正常
bug描述: 音频播放错误: {“errMsg”:“MediaError”,“errCode”:-99}
音频链接正常,点击播放,播放十几个之后报错 然后后续音频和视频就都无法正常播放了(音频不出声音,视频没有声音来回跳动)
文档上也没有注明-99是个什么错误,什么情况下会出现这种错误呢??
更多关于uni-app 音频播放错误 {"errMsg":"MediaError","errCode":-99}的实战教程也可以访问 https://www.itying.com/category-93-b0.html
我不知道这算不算bug,但是我之前碰到过,在网上找到了解决方法
var music={
//因为这个音效听起来是de的声音,所以我取这个函数名,方便记忆音效,找的合适的场景就能想起来。
play_dede:function(){
const innerAudioContext = uni.createInnerAudioContext();
innerAudioContext.autoplay = true;
innerAudioContext.src = ‘…/…/static/click.wav’;
innerAudioContext.onPlay(() => {
// console.log(‘点击声’);
});
innerAudioContext.onError((res) => {
console.log(res.errMsg);
console.log(res.errCode);
});
innerAudioContext.onPause(function() {
// console.log(‘end’);
innerAudioContext.destroy();//音频播放停止后,就销毁实例
});
},
}
export default music;
更多关于uni-app 音频播放错误 {"errMsg":"MediaError","errCode":-99}的实战教程也可以访问 https://www.itying.com/category-93-b0.html
切换时清空src
报-99 是因为生成的音频时长太短了;比如在语速10的情况下报长度为1~2的字符串;就会报错;;
解决:判断播报字符串长度<3时语速为5(或更小);否则为10左右
这个错误通常是由于音频资源加载或播放过程中出现问题导致的。errCode:-99表示媒体错误,可能的原因包括:
- 频繁切换音频源导致资源未正确释放
- 音频格式不支持
- 网络问题导致资源加载失败
- 系统音频资源耗尽
建议修改代码:
- 在切换音频前先停止并销毁之前的实例
- 添加错误处理和资源释放逻辑
- 确保音频格式兼容(推荐使用MP3格式)
修改后的播放方法示例:
play(href) {
if(this.innerAudioContext) {
this.innerAudioContext.stop();
this.innerAudioContext.destroy();
}
this.innerAudioContext = uni.createInnerAudioContext();
this.innerAudioContext.src = href;
this.innerAudioContext.play();
}