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

后端 未结 2 801
我寻月下人不归
我寻月下人不归 2021-02-15 16:01

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. H

相关标签:
2条回答
  • 2021-02-15 16:31

    You are not passing a function reference to your addEventListener - you are calling the function inline. The doneLoading() function executes immediately (before the file has loaded) and the browser correctly throws an INVALID_STATE_ERR:

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

    Try passing in a function reference instead. Like this:

    this.mAudioPlayer.addEventListener('loadedmetadata',function(){
        this.currentTime = aTime / 1000.0;
    }, false );
    
    0 讨论(0)
  • 2021-02-15 16:45

    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 :)

    0 讨论(0)
提交回复
热议问题