1

I'm building a jquery/html5 audio player and I have some problems on changing the source to the audio tag. The method I've implemented works on all browsers except IE9.

Here is a sample of code:

audio = $("<audio>").attr("id", "audioElement")
                        .attr("preload", "auto")
                        .appendTo(player);

function addMp3Source(sourceUrl) {
    audio.empty();
    var newSrc = $("<source>").attr("src", config.tracksURL + sourceUrl).appendTo(audio);
}

On IE9, the source is changing, but when I'm playing the track, it's the old sound that plays. On other browsers works fine.

Any ideeas?

3
  • Maybe you need to remove the whole audio element and add a new one with the new source as a workaround. Commented Oct 31, 2011 at 12:29
  • All you doing in there is emptying the html/children of <audio> you should use .remove() and $('<audio/>'); to create a new element audio, that way u loose trace of the old element, :) Commented Oct 31, 2011 at 12:35
  • so I can't change only the source? I already have some references to that audio tag, and if I remove the tag I lose the references. Commented Oct 31, 2011 at 12:53

1 Answer 1

1
function addMp3Source(sourceUrl) {
    audio.empty();
    var newSrc = $("<source>").attr("src", config.tracksURL + sourceUrl).appendTo(audio);
    /****************/
    audio[0].pause();
    audio[0].load();//suspends and restores all audio element
    /****************/
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks. This approach worked for jQuery Mobile 1.0.1 in FF 10 and Chrome 16 on Mac OS X 10.7, while several other methods didn't. However, I got the error pause() is not a function. So, I removed audio[0].pause(); and now my revised audio sources play:-)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.