Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

<audio> src change with javascript

I want to change the src attribute of a tag using javascript and a button :

<audio id="playme" src="" controls="controls">Your browser...</audio>

And a little further down the page :

<input type="button" style="font-size: 10px;"
        OnClick="document.getElementById('playme').src='snd/SOUND.WAV';"
        value="Listen">

It seems to just do nothing. Anyone has a clue ? Thanks a million

Using Firefox 3.6 on Xubuntu 10.10

EDIT : it seems to work on Chrome but Firefox doesn't like it. Should I report a bug ? Do you know a way to bypass that ?

like image 407
Aliénor Latour Avatar asked Feb 08 '11 15:02

Aliénor Latour


People also ask

How do I change the audio source in JavaScript?

You can change the audio file of the HTML5 player with just one line of JavaScript code that you can see below: document. getElementById("my-audio"). setAttribute('src', 'AUDIO_SRC_FILE');

How do I customize my audio tag?

HTML 5 audio tags can be styled. By using the audio tag with “controls” attribute, the default browsers player is used. You can customize by not using the browsers controls. You can also add CSS classes to each one of the elements and style them accordingly.


1 Answers

I believe you have to tell the browser to load the new file when you change the src attribute, by calling load:

var playme = document.getElementById('playme'); playme.src='snd/SOUND.WAV'; playme.load();
like image 51
lonesomeday Avatar answered Nov 01 '22 11:11

lonesomeday