小弟我刚刚做微信小程序,里面有个播放语音的本来很高兴在微信开发者工具里都正常了,结果放到手机预览的时候坏菜了,也没有错误,就是播放不出来。
补充一下,我为了测试在assets文件加下放了一个测试mp3.然后代码改成了:
this.currentAudioContext.src = '/assets/test.mp3';
this.currentAudioContext.play();
还是不出声音,用预览模式用的ipthon16 pro max测试的。
我的日志:
我的代码:
playVoice() {
if (!this.data.audioPath) {
console.error('音频路径无效');
this.showErrorToast('暂无有效音频');
return;
}
// 停止当前播放
if (this.currentAudioContext) {
this.currentAudioContext.stop();
this.currentAudioContext.destroy();
}
// 创建新的音频上下文
this.currentAudioContext = wx.createInnerAudioContext();
this.currentAudioContext.obeyMuteSwitch = false;
// 错误处理
this.currentAudioContext.onError((res) => {
console.error('播放错误:', {
errCode: res.errCode,
errMsg: res.errMsg,
path: this.data.audioPath
});
this.showErrorToast(this.getPlayErrorMsg(res.errCode));
});
this.currentAudioContext.onPlay(() => {
console.log('音频开始播放:', this.data.audioPath);
});
console.log('尝试播放音频:', this.data.audioPath);
this.currentAudioContext.src = this.data.audioPath;
this.currentAudioContext.play();
},
愁的我都睡不着了,咋办呢?
可能是音频文件用的是临时文件路径的缘故。