tonybabb
01-04-2015, 12:31 PM
Good morning,
I'm trying to create a short list of sound files, each with a <Play> icon beside it, when pressed the mp3 file plays and the icon changes to a <Pause> icon. I have the beginnings of it working but can't figure out two things:
- how I should change the words play/ pause to be icons or images and
- the code I have only works for a single entry and I'd like to have multiple entries on the same page. Below is the html and below that the js I have working so far
<audio id="yourAudio" preload='none'>
<source src='http://dl.dropbox.com/u/1538714/article_resources/song.m4a' type='audio/mpeg' />
</audio>
<a href="#" id="audioControl">play!</a> Song 1<br />
Song 2<br />
Song 3<br />
And here is the js I have
<script type="text/javascript">
var yourAudio = document.getElementById('yourAudio'),
ctrl = document.getElementById('audioControl');
ctrl.onclick = function () {
// Update the Button
var pause = ctrl.innerHTML === 'pause!';
ctrl.innerHTML = pause ? 'play!' : 'pause!';
// Update the Audio
var method = pause ? 'pause' : 'play';
yourAudio[method]();
// Prevent Default Action
return false;
};
</script>
The only controls I'd like are Play/ Pause
I'd really appreciate any suggestions on this.
Thanks
I'm trying to create a short list of sound files, each with a <Play> icon beside it, when pressed the mp3 file plays and the icon changes to a <Pause> icon. I have the beginnings of it working but can't figure out two things:
- how I should change the words play/ pause to be icons or images and
- the code I have only works for a single entry and I'd like to have multiple entries on the same page. Below is the html and below that the js I have working so far
<audio id="yourAudio" preload='none'>
<source src='http://dl.dropbox.com/u/1538714/article_resources/song.m4a' type='audio/mpeg' />
</audio>
<a href="#" id="audioControl">play!</a> Song 1<br />
Song 2<br />
Song 3<br />
And here is the js I have
<script type="text/javascript">
var yourAudio = document.getElementById('yourAudio'),
ctrl = document.getElementById('audioControl');
ctrl.onclick = function () {
// Update the Button
var pause = ctrl.innerHTML === 'pause!';
ctrl.innerHTML = pause ? 'play!' : 'pause!';
// Update the Audio
var method = pause ? 'pause' : 'play';
yourAudio[method]();
// Prevent Default Action
return false;
};
</script>
The only controls I'd like are Play/ Pause
I'd really appreciate any suggestions on this.
Thanks