I have this function for playing music and pausing music. It is just simple and light weight for small samples. I just wanted it to have play and stop. It seems to work fine, but the second time you play it, it doesn't switch the classes so pausing can take place.
Here is the jquery;
var isPlaying = false;
function playMusic(){
var audioElement = document.createElement('audio');
var audioElementSrc = $(this).attr('data-audio-src');
if (isPlaying != true)
{
isPlaying = true;
audioElement.setAttribute('src', audioElementSrc);
$.get();
audioElement.addEventListener("loadeddata", function(){
audioElement.play();
}, true);
audioElement.addEventListener("ended", function() {
isPlaying = false;
});
$(this).addClass('pause');
$(this).removeClass('play');
$('.pause').click(function() {
audioElement.pause();
isPlaying = false
$(this).removeClass('pause');
$(this).addClass('play');
});
}
else
{
return false;
}
}
$(function(e){
$('.play').click(playMusic);
});
jsfiddle Here with everything. You can see the stop appear, then click it and when you play again it doesn't swap the classes.