報(bào)錯(cuò)截圖
最下面這個(gè)this.setData()報(bào)錯(cuò)可不用理會(huì),是this取值的問題文章來源地址http://www.zghlxwxcb.cn/news/detail-559980.html
解決
- 需要播放和暫停功能時(shí),需要把a(bǔ)udio以及他的src放在Page外面。
- 不能缺少 audioCtx.onPlay() 和 audioCtx.onError()兩個(gè)方法,且需要放在play()方法之前
- 如果在wx.createInnerAudioContext()添加了 {useWebAudioImplement:true},把它去掉。
//需要播放暫停音頻功能時(shí),需要將這兩個(gè)變量放在 Page()上面
let audioCtx=wx.createInnerAudioContext()
audioCtx.src='http://ws.stream.qqmusic.qq.com/M500001VfvsJ21xFqb.mp3' //填入你自己的音頻鏈接
Page({
data: {
isPlay: false,
},
playMusic(){
audioCtx.onPlay(()=>{
console.log("開始播放");
})
audioCtx.onError((res)=>{
wx.showToast({
title: '音樂播放錯(cuò)誤',
icon: 'error',
duration: 3000
})
})
let isPlay = !this.data.isPlay //控制播放和暫停的變量
this.setData({
isPlay:isPlay
})
if(isPlay){
audioCtx.play()
audioCtx.loop = true;
}else{
audioCtx.pause()
}
},
},
onLoad() {
this.playMusic(); //加載完畢,開始播放
},
});
文章來源:http://www.zghlxwxcb.cn/news/detail-559980.html
到了這里,關(guān)于微信小程序音頻播放失?。篢ypeError: Cannot read property ‘duration‘ of undefined的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!