声音播放完不触发onEnded事件
发布于 4 年前 作者 weishi 7954 次浏览 来自 问答
  • 声音播放结束不触发onEnded
  • 期望播放结束 触发onEnded
  • 不是所有播放都不触发 , 一段1分钟背景音乐 结束 切换下一首 第一次100%不触发
  • no demo
3 回复

你好,请提供一下出现问题的机型和微信版本,以及能复现问题的简单代码片段(https://developers.weixin.qq.com/miniprogram/dev/devtools/minicode.html)。

用了个土方法,监听到 onPlay 事件 同时写个setttimeout 事件 时间是当前播放音乐时长+1秒 回调如果检测没进行播放下一个,执行下一个声音播放

我也遇到同样的问题了,楼主的问题解决了吗?

//监听玩家发送的语音消息
onPlayerWsAudioMessage:function (o) {
cc.xdConst.HALL_VOICE[cc.xdConst.HALL_VOICE.length] = o;
this.playerPlayVoice(cc.xdConst.HALL_VOICE[0]);
},

playerPlayVoice:function (voiceData) {
let seat = voiceData.seat;
let audioPath = voiceData.audioPath;
if(this.playerScripts[seat]!=null){
this.playerScripts[seat].showAudioMessage(audioPath, ()=>{ //开始播放
           cc.xdConst.HALL_VOICE.splice(0,1);
}, (len)=>{ //播放中

       }, ()=>{ //播放完成
           if(cc.xdConst.HALL_VOICE.length > 0){
this.playerPlayVoice(cc.xdConst.HALL_VOICE[0]);
}
});
}

},


//展示语音消息
showAudioMessage:function (o, startPlay, playing, playEnd) {
let anim = this.audioMessage.node.getChildByName("audioplay").getComponent(cc.Animation);
cc.xdSdk.playVoiceFile(o, ()=>{ //开始播放
       cc.xdSdk.log('开始播放语音');
startPlay();
}, (len)=>{ //播放中
       playing();
cc.xdSdk.log('播放语音ing ' + + len + '秒');
this.audioMessage.node.active = true;
this.audioMessage.node.getChildByName("audiolength").getComponent(cc.Label).string = len+"\"";
anim.play();
cc.xdAudioManager.pauseMusic();
}, ()=>{ //播放完成
       playEnd();
// cc.xdConst.HALL_VOICE.splice(0,1);
       cc.xdSdk.log('停止语音');
this.audioMessage.node.active = false;
anim.stop();
cc.xdAudioManager.resumeMusic();
});

},


//小游戏播放语音
playVoiceFile:function (audioPath, callStarPlay, callbackPlaying, callbackEnd) {
if(window.wx){
let flag = true;
let innerAudioContext = wx.createInnerAudioContext();
innerAudioContext.src = audioPath;
//播放语音
       innerAudioContext.play();
innerAudioContext.onPlay(() => {
console.log('开始播放');
callStarPlay();
});
innerAudioContext.onTimeUpdate(res=>{
if(flag){
callbackPlaying(innerAudioContext.duration.toFixed(2));
flag = false;
}
});
innerAudioContext.onEnded(()=>{
console.log('播放完成');
callbackEnd();
});
innerAudioContext.onError((res) => {
console.log(res.errMsg);
console.log(res.errCode);
});
}
},



收到多条语音时,采用逐条播放,第一条语音可以正常播放,正常走onEnded(), 但第二条语音播放完成后不回调onEnded(),是因为什么原因呢?

回到顶部