1***@qq.com
1***@qq.com
  • 发布:2025-01-06 13:25
  • 更新:2025-04-25 09:15
  • 阅读:134

【报Bug】uni.createInnerAudioContext 安卓app频繁播放音频时,声音卡顿,微信小程序中未出现问题

分类:uni-app

产品分类: uniapp/App

PC开发环境操作系统: Mac

PC开发环境操作系统版本号:

HBuilderX类型: 正式

HBuilderX版本号: 4.36

手机系统: Android

手机系统版本号: Android 12

手机厂商: OPPO

手机机型: oppo a36

页面类型: vue

vue版本: vue2

打包方式: 云端

项目创建方式: HBuilderX

示例代码:

// 创建一个新的音频上下文对象
initAudioContext: function() {
console.log("[initAudioContext]");
// 在初始化时销毁之前的音频上下文
this.destroyAudioContext();

            // 创建一个新的 InnerAudioContext 实例  
            t = uni.createInnerAudioContext({  
                useWebAudioImplement: !0 // 使用 WebAudio 实现  
            });  
            // 设置自动播放为 false  
            t.autoplay = !1;  
            // 绑定音频播放事件  
            t.onPlay(function() {  
                // 播放开始时的回调(当前为空)  
            });  
            // 绑定音频播放结束事件  
            t.onEnded(function() {  
                // 播放结束时的回调(当前为空)  
            });  
            // 绑定音频播放错误事件  
            t.onError(function(error) {  
                console.log("[initAudioContext] 播放错误 cod=" + error.errCode + ", message=" + error.errMsg);  
                // 打印播放错误的错误码和信息  
            });  
            // 绑定音频停止播放事件  
            t.onStop(function() {  
                // 打印停止播放的日志  
            });  
            // 绑定音频等待事件(例如等待缓冲等)  
            t.onWaiting(function() {  
                console.log("[initAudioContext] onWaiting");  
                // 打印等待状态的日志  
            });  

            // #ifdef MP-WEIXIN  
            // 检查是否支持设置音频选项  
            if (wx.setInnerAudioOption) {  
                // 设置音频选项,不受静音开关影响  
                wx.setInnerAudioOption({  
                    obeyMuteSwitch: !1  
                });  
            } else {  
                // 如果不支持 setInnerAudioOption,则直接设置属性  
                t.obeyMuteSwitch = !1;  
            }  
            // #endif  
        },  

// 处理逻辑
updatePlayBeat() {
// 触发播放
// this.lineIsLeft = (this.lineIsLeft === 0 || this.lineIsLeft === 1) ? 2 : 1; // 切换指针
// 音频判断
this.startIndex = (this.startIndex === this.beat[0] || this.startIndex === -1) ? 0 : this.startIndex + 1;
this.playBeat(this.startIndex)
return true
},
// 播放音频
playBeat: function(index) {
if (t) {
t.stop();
var n = this.getAudioSrc(index);
if (n) {
t.src = n;
console.log('播放声音' + index);
t.play()
}
}
},

// 设置一个定时器,根据节拍时间间隔更新节拍
let a = setInterval(function() {
// 更新当前播放的节拍,如果更新成功,继续设置新的间隔时间
if (t.updatePlayBeat()) {
t.updateIntervalWithBPM(e); // 更新节拍的间隔时间
} else {
console.log("[handlePlayIgnorePlayStatus] #更新失败,停止播放#");
t.endFunction(); // 更新失败,停止播放
}
}, e);
t.intervalNumber = a // 保存定时器的 ID,以便后续停止定时器

bug描述:

uni.createInnerAudioContext创建音频,setInterval定时切换的时候,音频播放卡顿,不精准且定时任务是没问题,慢一点的时候是可以的,当快一点 400ms - 250ms 的时候就会有问题

2025-01-06 13:25 负责人:无 分享
已邀请:
苍山暮色烟雨迟

苍山暮色烟雨迟

怎么处理的哥们?

  • 1***@qq.com (作者)

    我现在是创建了多个音频,然后分别调用的播放,不去操作同一个音频,能强一点吧,但是感觉还会有一点

    2025-04-22 16:01

要回复问题请先登录注册