Skip to content

Instantly share code, notes, and snippets.

@da1nonly
Created January 31, 2015 11:22
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 da1nonly/c53160a5f091dbc33c39 to your computer and use it in GitHub Desktop.
Save da1nonly/c53160a5f091dbc33c39 to your computer and use it in GitHub Desktop.
youtube
(function () {
videojs.plugin('playlist', function (options) {
var id = this.el().id;
var tracks = document.querySelectorAll("#" + id + "-vjs-playlist .vjs-track"),
trackCount = tracks.length,
player = this,
currentTrack = tracks[0],
index = 0,
play = true,
onTrackSelected = options.onTrackSelected;
//manually selecting track
for (var i = 0; i < trackCount; i++) {
tracks[i].onclick = function () {
trackSelect(this);
}
}
// for continuous play
if (typeof options.continuous == 'undefined' || options.continuous == true) {
player.on("ended", function () {
index++;
if (index >= trackCount) {
index = 0;
} else; // console.log('trigger click next track');
tracks[index].click();
}); // on ended
} else; // console.log('dont play next!');
//track select function for onended and manual selecting tracks
var trackSelect = function (track) {
//get new src
var src = track.getAttribute('data-src');
index = parseInt(track.getAttribute('data-index')) || index;
//console.log('track select click src:'+src);
if (player.techName == 'youtube') {
player.src([{
type: type = "video/youtube",
src: src
}]);
} else {
if (player.el().firstChild.tagName == "AUDIO" || (typeof options.mediaType != 'undefined' && options.mediaType == "audio")) {
player.src([{
type: "audio/mp4",
src: src
}, {
type: "audio/webm",
src: src
}, {
type: type = "video/youtube",
src: src
}, {
type: "audio/ogg",
src: src
}, {
type: "audio/mpeg",
src: src
}]);
} else {
//console.log("video");
player.src([{
type: "video/mp4",
src: src
}, {
type: "video/webm",
src: src
}, {
type: type = "video/youtube",
src: src
}]);
}
}
if (play) player.play();
//remove 'currentTrack' CSS class
for (var i = 0; i < trackCount; i++) {
if (tracks[i].classList.contains('currentTrack')) {
tracks[i].className = tracks[i].className.replace(/\bcurrentTrack\b/, 'nonPlayingTrack');
}
}
//add 'currentTrack' CSS class
track.className = track.className + " currentTrack";
if (typeof onTrackSelected === 'function') onTrackSelected.apply(track);
}
//if want to start at track other than 1st track
if (typeof options.setTrack != 'undefined') {
options.setTrack = parseInt(options.setTrack);
currentTrack = tracks[options.setTrack];
index = options.setTrack;
play = false;
trackSelect(tracks[index]);
play = true;
}
if (window.location.hash) {
var hash = window.location.hash.substring(9);
play = false;
trackSelect(tracks[hash]);
}
var data = {
tracks: tracks,
trackCount: trackCount,
play: function () {
return play;
},
index: function () {
return index;
},
prev: function () {
var j = index - 1;
//console.log('j'+j);
if (j < 0 || j > trackCount) j = 0;
trackSelect(tracks[j]);
},
next: function () {
var j = index + 1;
//console.log('j'+j);
if (j < 0 || j > trackCount) j = 0;
trackSelect(tracks[j]);
}
};
return data;
});
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment