Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HTML5 audio - testing for Invalid State Error ( Or Dom Exception 11 )

I am dynamically creating a audio file and changing the source on the fly. However after i change the src and try to change the currentTime i always get a Invalid state error. How do you go about testing for it? Or better fire a event when its ready and then calling currentTime to change its audio position.

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();
    }       



};
like image 326
Neablis Avatar asked Oct 10 '12 16:10

Neablis


1 Answers

For those coming after who actually need a test to prevent this invalid state error, you can try this:

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

Seems to work for me anyways :)

like image 92
Svish Avatar answered Sep 19 '22 05:09

Svish