javascript - Audio pause/play with JS variable (ubernewb) -
javascript - Audio pause/play with JS variable (ubernewb) -
i'm working on simple project includes media (mp3) player in sidebar. can play/pause button visually switch , can turn off sound assigning href image when trying swapped image pause sound can't seem figure out, here's code..
edit: deleted shotty code
edit: figured out 3 ways this, 2 kind people below posted great ways figured out how crudely via jquery.
$('#left-05-pause_').click(function(){ $('#left-05-pause_').hide(); $('#left-05-play_').show(); }); $('#left-06-audio_').click(function(){ audio.volume = 1; $('#left-06-audio_').hide(); $('#left-06-mute_').show(); });
mitch, have 3 points you:
there's no need wrap<a>
around <img>
for performance avoid overuse of selecting elements (like getelementbyid), because 1 time you've selected link element set variable , utilize 1 time again access same element use native info element's state (for <audio>
in example) - explore properties all in seek next sample (file names have been changed clarity):
<body> <audio id="audioid"> <source src="song.mp3" type="audio/mp3" /> </audio> <img id="imageid" src="play.png" onclick="toggle()" /> <script> var sound = document.getelementbyid( 'audioid' ) , image = document.getelementbyid( 'imageid' ) function toggle() { if ( audio.paused ) { image.src = 'pause.png' audio.play() } else { image.src = 'play.png' audio.pause() } } </script> </body>
javascript html variables audio
Comments
Post a Comment