Working on source() api method

This commit is contained in:
Sam Potts 2015-03-06 01:51:12 +11:00
parent c6e5937deb
commit 0227dfa7d8
2 changed files with 95 additions and 7 deletions

2
dist/plyr.js vendored

File diff suppressed because one or more lines are too long

View File

@ -264,6 +264,18 @@
} }
} }
// Set attributes
function _setAttributes(element, attributes) {
for(var key in attributes) {
element.setAttribute(key, attributes[key]);
}
}
// Prepend child
function _prependChild(parent, element) {
parent.insertBefore(element, parent.firstChild);
}
// Bind event // Bind event
function _on(element, events, callback) { function _on(element, events, callback) {
_toggleHandler(element, events, callback, true); _toggleHandler(element, events, callback, true);
@ -571,6 +583,11 @@
// Cache the container // Cache the container
player.videoContainer = wrapper; player.videoContainer = wrapper;
} }
// Autoplay
if(player.media.getAttribute("autoplay") !== null) {
_play();
}
} }
// Setup captions // Setup captions
@ -772,9 +789,6 @@
if (!player.isTextTracks) { if (!player.isTextTracks) {
player.subcount = 0; player.subcount = 0;
} }
// Play and ensure the play button is in correct state
_play();
} }
// Rewind // Rewind
@ -991,8 +1005,6 @@
progress.value = value; progress.value = value;
text.innerHTML = value; text.innerHTML = value;
} }
//_log(event);
} }
// Update the displayed play time // Update the displayed play time
@ -1008,6 +1020,7 @@
player.duration.innerHTML = player.mins + ":" + player.secs; player.duration.innerHTML = player.mins + ":" + player.secs;
} }
// Handle time change event
function _timeUpdate(event) { function _timeUpdate(event) {
// Duration // Duration
_updateTimeDisplay(); _updateTimeDisplay();
@ -1015,6 +1028,78 @@
_updateProgress(event); _updateProgress(event);
} }
// Remove an element
function _remove(element) {
element.parentNode.removeChild(element);
}
// Remove sources
function _removeSources() {
// Remove child <source> elements
var sources = player.media.querySelectorAll("source");
for (var i = sources.length - 1; i >= 0; i--) {
_remove(sources[i]);
}
// Remove src attribute
player.media.removeAttribute("src");
}
// Inject a source
function _addSource(attributes) {
// Create a new <source>
var element = document.createElement("source");
// Set all passed attributes
_setAttributes(element, attributes);
// Inject the new source
_prependChild(player.media, element);
}
// Update source
function _updateSource(sources) {
// Pause on update
// Play automatically if autoplay set or already playing
// Remove current sources
_removeSources();
// If a single source is provided
// ("path/to/src.mp4")
if(typeof sources === "string") {
// Set src attribute on the element
player.media.setAttribute("src", sources);
}
// Single source but using object to pass attributes
// ({ src: "path/to/src.mp4", type: "video/mp4" })
else if (typeof sources === "object") {
_addSource(sources);
}
// Array of source objects to pass attributes
// ([{ src: "path/to/src.mp4", type: "video/mp4" },{ src: "path/to/src.webm", type: "video/webm" }])
else if (sources.constructor === Array) {
for (var key in sources) {
_addSource(sources[key]);
}
}
// Restart
_restart();
// Play if autoplay attribute is present
if(player.media.getAttribute("autoplay") !== null) {
_play();
}
}
// Update poster
function _updatePoster(source) {
if(player.type === "video") {
player.media.setAttribute("poster", source);
}
}
// Listen for events // Listen for events
function _listeners() { function _listeners() {
// Play // Play
@ -1063,6 +1148,7 @@
} }
else if(player.media.ended) { else if(player.media.ended) {
_restart(); _restart();
_play();
} }
else { else {
_pause(); _pause();
@ -1163,7 +1249,9 @@
seek: _seek, seek: _seek,
setVolume: _setVolume, setVolume: _setVolume,
toggleMute: _toggleMute, toggleMute: _toggleMute,
toggleCaptions: _toggleCaptions toggleCaptions: _toggleCaptions,
source: _updateSource,
poster: _updatePoster
} }
} }