Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

switch audio source with jquery and HTML5 audio tag

I have only found one other solution but it was incomplete so I need help here.

i have the audio set up:

<audio id="player" controls="controls">           <source id="ogg_src" src="lib/audio/barger01.ogg" type="audio/ogg" />           <source id="mp3_src" src="lib/audio/barger01.mp3" type="audio/mp3" />           Your browser does not support the audio element.     </audio> 

I have a dynamically generated table of links to change the track:

<div id="audio_list">    <a href="#" class="track" data-location="http://www.newoggtrack.ogg">sample</a> </div> 

i have this jquery that i have no clue what to do with to change the track

$('.track').click(function(){      load_track = $(this).attr('data-location');//gets me the url of the new track      change_track(load_track);// function to change the track of the loaded audio player without page refresh preferred...  }); 

i found this function but i am not using it the right way

 function change_track(sourceUrl) {          audio.empty();         $("#ogg_src").attr("src", sourceUrl).appendTo(audio);         /****************/         audio[0].pause();         audio[0].load();//suspends and restores all audio element         /****************/     }  audio = $("<audio>").attr("id", "player")                         .attr("preload", "auto"); 
like image 959
Daniel Hunter Avatar asked Feb 23 '12 21:02

Daniel Hunter


People also ask

Can multiple audio sources can be played in HTML?

The <audio> HTML element is used to embed sound content in documents. It may contain one or more audio sources, represented using the src attribute or the <source> element: the browser will choose the most suitable one.

Does HTML5 support audio tag?

HTML5 supports <audio> tag which is used to embed sound content in an HTML or XHTML document as follows. The current HTML5 draft specification does not specify which audio formats browsers should support in the audio tag. But most commonly used audio formats are ogg, mp3 and wav.

How do I embed audio in HTML5?

To embed audio in HTML, we use the <audio> tag. Before HTML5, audio cannot be added to web pages in the Internet Explorer era. To play audio, we used web plugins like Flash.


2 Answers

Your change function should be like this:

function change(sourceUrl) {     var audio = $("#player");           $("#ogg_src").attr("src", sourceUrl);     /****************/     audio[0].pause();     audio[0].load();//suspends and restores all audio element      //audio[0].play(); changed based on Sprachprofi's comment below     audio[0].oncanplaythrough = audio[0].play();     /****************/ } 

The problems were audio.empty() and the audio var. You were attempting to append an emptied audio tag, and didn't write out the audio tag back to the browser.

like image 118
Justice Erolin Avatar answered Sep 28 '22 06:09

Justice Erolin


You might also want to rename the function since 'change' is already a function in the jQuery universe.

like image 31
Jay Blanchard Avatar answered Sep 28 '22 08:09

Jay Blanchard