Last active
June 20, 2017 11:32
-
-
Save rafa8626/2ce220b30b7e2ef2dc2e277df6a697ce to your computer and use it in GitHub Desktop.
MediaElement.js: Skip to next source if previous cannot be played
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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