Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HTML5 audio DOM Exception 11 error with currentTime=0

EDIT: I posted this in the Wordpress side but some admin thinks it goes here because they say it's a client side problem but I don't think it is because he script works as is (in a non-wordpress site) but only throws the DOM Exception 11 error when the script is used on a Wordpress site and only on certain browsers. The very same script works under Wordpress but only on Chrome.


I have taken a script that plays an audio clip when an anchor link is hovered and turned it into a Wordpress plugin.

It works fine but only on Chrome (Mac). On Firefox or Safari (current versions) the script is throwing a DOM error.

Here is the error:

INVALID_STATE_ERR: DOM Exception 11: An attempt was made to use an object that is not, or is no longer, usable.

The line that is producing the error is: html5audio.currentTime=0

The below script works fine on the browsers I am having a problem with in a non-Wordpress site. http://www.javascriptkit.com/script/script2/soundlink.shtml#current

I have tried researching the problem and using some fixes but I can not get to work.

var html5_audiotypes={ 
    "mp3": "audio/mpeg",
    "mp4": "audio/mp4",
    "ogg": "audio/ogg",
    "wav": "audio/wav"
}

function audio(sound){
    var html5audio=document.createElement('audio')
    if (html5audio.canPlayType){ //check support for HTML5 audio
        for (var i=0; i<arguments.length; i++){
            var sourceel=document.createElement('source')
            sourceel.setAttribute('src', arguments[i])
            if (arguments[i].match(/\.(\w+)$/i))
                sourceel.setAttribute('type', html5_audiotypes[RegExp.$1])
            html5audio.appendChild(sourceel)
        }
        html5audio.load()
        html5audio.playclip=function(){
            html5audio.pause()
            html5audio.currentTime=0
            html5audio.play()
        }
        return html5audio
    }
}
like image 493
Jason Avatar asked Dec 05 '13 01:12

Jason


1 Answers

I guess it's too late for the original poster, but in case anyone else comes looking...

I had a similar problem - in my case the code worked on Chrome, but nothing else as far as I could tell, but it's not a Wordpress site. After some experimenting, I realised that the error only happened if currentTime was already 0. So the solution was quite simple:

html5audio.pause(); 
if (html5audio.currentTime != 0) {
   html5audio.currentTime=0;
}  
html5audio.play();

I hope that helps someone.

like image 99
Thailandian Avatar answered Oct 04 '22 21:10

Thailandian