javascript - Change audio file on button click -
i trying change audio file on button click , getting error
uncaught typeerror: audio.load not function
my code below, idea press button , audio file change.
without audio.load() line, code run , src update, audio not change. missing?
html:
<!doctype html> <html> <body> <br> <audio controls> <source src = "audio_file_1.mp3" id="audio" type="audio/mpeg"> browser not support audio element! </audio> <br> <button onclick="changeaudio();">change</button> </body> <script src="temp.js"></script> </html> javascript:
// temp js var = 1; function changeaudio() { var audio = document.getelementbyid('audio'); if (a==1) { audio.src = 'audio_file_1.mp3'; = 2; } else { audio.src = 'audio_file_2.mp3'; = 1; } audio.load(); } changeaudio(); (also, if knows example sound files can use instead of audio_file_1.mp3 , audio_file_2.mp3, happy update code can run more easily)
edit wrt rob m's answer:
for same problem, solution change 2 lines:
first, in html code, <audio control> should turn <audio control id="audio_id">
second, in javascript code audio.load() should turn document.getelementbyid('audio_id').load();
there no .load() event on <source> tag, on <audio> tag - update code call load() on parent <audio> tag , should work.
Comments
Post a Comment