// 创建一个新的音频上下文对象
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,以便后续停止定时器
1***@qq.com (作者)
我现在是创建了多个音频,然后分别调用的播放,不去操作同一个音频,能强一点吧,但是感觉还会有一点
2025-04-22 16:01