Skip to content

Instantly share code, notes, and snippets.

@rafa8626
Last active June 20, 2017 11:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rafa8626/2ce220b30b7e2ef2dc2e277df6a697ce to your computer and use it in GitHub Desktop.
Save rafa8626/2ce220b30b7e2ef2dc2e277df6a697ce to your computer and use it in GitHub Desktop.
MediaElement.js: Skip to next source if previous cannot be played
new MediaElementPlayer('player-id', {
// More MEJS config
success: function (media, node, player) {
// Your code when successful
// Gather list of children
var mediaFiles = player.mediaFiles ? player.mediaFiles : node.children(), tmpFiles = [];
media.addEventListener('error', function () {
for (var i = 1, total = mediaFiles.length; i < total; i++) {
// this is a basic example, so a more complex condition will be required in different scenarios
if (media.getSrc() !== mediaFiles[i].src) {
media.pause();
media.setSrc(mediaFiles[i].src);
media.load();
media.play();
// In case markup needs to be restored later, save element in temporary array
tmpFiles.push(mediaFiles[i]);
// Remove it from the main list so `mediaFiles` reindexes its length
mediaFiles[i].remove();
break;
}
}
});
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment