8

我正在动态创建音频文件并动态更改源。但是,在我更改 src 并尝试更改 currentTime 之后,我总是会收到 Invalid state 错误。你如何进行测试?或者更好地在它准备好时触发一个事件,然后调用 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();
    }       



};
4

2 回答 2

20

对于那些真正需要测试来防止这种无效状态错误的人,你可以试试这个:

if(this.readyState > 0)
    this.currentTime = aTime;

无论如何似乎对我有用:)

于 2014-05-12T18:22:29.320 回答
10

您没有将函数引用传递给 addEventListener - 您正在调用内联函数。doneLoading ()函数立即执行(在文件加载之前)并且浏览器正确地抛出一个 INVALID_STATE_ERR:

this.mAudioPlayer.addEventListener('canplaythrough', this.doneLoading(aTime), false );

尝试传入函数引用。像这样:

this.mAudioPlayer.addEventListener('loadedmetadata',function(){
    this.currentTime = aTime / 1000.0;
}, false );
于 2012-10-11T13:25:11.843 回答