js以window.onload方式获取audio.duration的值为NaN,ios 解决audio元素获取duration为Infinity

不想在oncanplay里监听,那样就是回调了,讨厌回调地狱
(js以window.onload方式获取audio.duration的值为NaN)[https://ask.csdn.net/questions/163442]

function getTime() {
setTimeout(function () {
var duration = $("#audio")[0].duration;
if(isNaN(duration)){
getTime();
}
else{
console.info("该歌曲的总时间为:"+$("#audio")[0].duration+"秒")
}
}, 10);
}

ios 解决audio元素获取duration为Infinity

    private async audioCanplay(e: any) {
      const firsthandAudio: HTMLAudioElement = e.target;
      while (firsthandAudio.duration === Infinity) {
        await new Promise(r => setTimeout(r, 200));
        firsthandAudio.currentTime = 10000000 * Math.random();
      }
      this.duration = firsthandAudio.duration;
    }

我的实现

 // 远程音频
                let audioUrl = "https://cdn1.miyamibao.com/mp3/111.mp3?" + new Date().getTime()
                let audio = new Audio(audioUrl)
                audio.load()
                audio.play()
             //   let _tim = audio.duration
                // audio.oncanplay = async function () {
                //     _tim = audio.duration
                //     cc.log(_tim)                    
                // }

                function getTime() {
                    return new Promise((resolve,reject)=>{
                         let repeatCount = 0
                         function _getTime() {
                            setTimeout(function () {
                                let _tim = audio.duration
                                if(isNaN(_tim) || _tim === Infinity){
                                    repeatCount++
                                    if(repeatCount < 20){
                                        _getTime();
                                    }else{
                                        reject("err")
                                    }
                                }
                                else{
                                    resolve(_tim)
                                }
                            }, 1000);
                        }
                        return _getTime()
                    })
                    
                }
               
                let[getTimeErr, durationTime] = await awaitWrap(getTime())
                if(getTimeErr == 'err'){

                }
                // cc.log(getTimeErr)
                // cc.log(durationTime)

              let [_t, [getScoreErr, scoreData]] = await Promise.all([
                    sleep(durationTime),
                    awaitWrap(audioPingce(
                        // recordAudioUrl,
                        "https://cdn1.miyamibao.com/mp3/111.mp3",
                        "哈哈"
                    ))
                ])
                // cc.log(_t)
                // cc.log(err)
                // cc.log(score_data)
                
                if(getScoreErr == 'err'){

                }
                // cc.log(_t)
                // cc.log(err)
                // cc.log(score_data)
           

promise.all 异常处理

你可能感兴趣的:(js以window.onload方式获取audio.duration的值为NaN,ios 解决audio元素获取duration为Infinity)