Skip to content

Instantly share code, notes, and snippets.

@kaecy
Last active December 30, 2015 12:39
Show Gist options
  • Select an option

  • Save kaecy/7830730 to your computer and use it in GitHub Desktop.

Select an option

Save kaecy/7830730 to your computer and use it in GitHub Desktop.
code demonstrating play on end. i.e, play next track on the ending of the first.
var nil = 1;
function getFirst(eName) {
var firstElm = document.getElementsByTagName(eName)[0];
if (firstElm == undefined)
return nil;
return firstElm;
}
function Answer(bValue) {
if (
bValue == true
|| bValue == 1
|| bValue == "yes"
)
this.isTrue = true;
else this.isTrue = false;
}
DataMediator = new Object();
DataMediator.audio = [];
function endListener() {
var audioTag = getFirst("audio");
var audio = DataMediator.audio.shift();
if (new Answer(confirm("Play next track?\n" + audio)).isTrue ) {
audioTag.src = audio;
audioTag.load();
audioTag.play();
}
if (DataMediator.audio.length == 0)
audioTag.removeEventListener("ended", endListener, false);
}
function playOnEnd(fileParam) {
var audioTag = getFirst("audio");
DataMediator.audio.push(fileParam);
audioTag.addEventListener("ended", endListener, false);
}
// prompts before it plays
playOnEnd("two steps from hell/protectors of earth.mp3");
playOnEnd("two steps from hell/heart of courage.mp3");
<!-- demo -->
<html><body>
<script src="play.js"></script>
<audio src="example.ogg" autoplay></audio>
<script src="x.js"></script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment