HarmonyOS 鸿蒙Next AVPlayer播放问题 音频资源切换后,在initialized中prepare的回调函数中调用AVPlayer的play方法,音频不进人播放(偶发性的),具体代码如下,帮忙看一下吧,谢谢
async initAudioAvPlayer() {
this.avPlayer = await media.createAVPlayer()
this.avPlayer?.on("stateChange", async (state) => {
if (state === "initialized") {
this.avPlayer?.prepare().then(() => {
this.avPlayer?.play()
}, (err: BusinessError) => {
console.error('prepare filed,error message is :' + err.message)
})
} else if (state == "completed") {
//音频播放结束
}else if(state == 'error'){
console.info('state error called');
}else{
console.info('other state :' + state);
}
})
this.avPlayer.on('error', (error: BusinessError) => {
console.info('error happened,and error message is :' + error.message)
console.info('error happened,and error code is :' + error.code)
})
}
playAudio(url:string){
this.avPlayer?.reset().then(()=>{
if(this.avPlayer){
this.avPlayer.url = url;
}
}).catch(()=>{})
}