我正在动态创建音频文件并动态更改源代码。但是,在我更改src并尝试更改currentTime后,我总是会收到无效状态错误。你是如何进行测试的呢?或者更好地在事件准备好时触发事件,然后调用currentTime来改变其音频位置。
this.doneLoading = function(aTime){
try{
this.mAudioPlayer.currentTime = aTime / 1000.0;
}catch(err){
console.log( err );
}
this.mAudioPlayer.play();
}
this.playAtTime = function(aTime) {
Debug("play at time audio: " + aTime);
Debug("this.mAudioPlayer.currentTime: " + this.mAudioPlayer.currentTime);
this.startTime = aTime;
if (this.mAudioPlayer.src != this.mAudioSrc) {
this.mAudioPlayer = new Audio();
this.mAudioPlayer.src = this.mAudioSrc;
this.mAudioPlayer.load();
this.mAudioPlayer.play();
this.mAudioPlayer.addEventListener('canplaythrough', this.doneLoading(aTime), false );
}
else if ((isChrome() || isMobileSafari()) && aTime == 0) {
this.mAudioPlayer.load();
this.mAudioPlayer.currentTime = aTime / 1000.0;
this.mAudioPlayer.play();
Debug("Reloading audio");
}else{
this.mAudioPlayer.currentTime = aTime / 1000.0;
this.mAudioPlayer.play();
}
};
答案 0 :(得分:19)
对于那些真正需要测试以防止此无效状态错误的人,您可以尝试这样做:
if(this.readyState > 0)
this.currentTime = aTime;
无论如何似乎对我有用:)
答案 1 :(得分:10)
您没有将函数引用传递给addEventListener - 您正在调用函数内联。 doneLoading()函数立即执行(在文件加载之前)并且浏览器正确抛出INVALID_STATE_ERR:
this.mAudioPlayer.addEventListener('canplaythrough', this.doneLoading(aTime), false );
尝试传入函数 reference 。像这样:
this.mAudioPlayer.addEventListener('loadedmetadata',function(){
this.currentTime = aTime / 1000.0;
}, false );