Skip to content

Commit

Permalink
append-to: resume from currentTime on fallback
Browse files Browse the repository at this point in the history
  • Loading branch information
feross committed Jul 29, 2015
1 parent 24a85c4 commit 2b28312
Showing 1 changed file with 16 additions and 13 deletions.
29 changes: 16 additions & 13 deletions lib/append-to.js
Expand Up @@ -20,6 +20,7 @@ module.exports = function appendTo (file, rootElem, cb) {
cb = dezalgo(cb || function () {})
var elem
var extname = path.extname(file.name).toLowerCase()
var currentTime = 0

if (rootElem && (rootElem.nodeName === 'VIDEO' || rootElem.nodeName === 'AUDIO')) {
throw new Error(
Expand All @@ -34,7 +35,6 @@ module.exports = function appendTo (file, rootElem, cb) {
else if (IFRAME_EXTS.indexOf(extname) >= 0) appendToIframe()
else cb(new Error('Unsupported file type "' + extname + '": Cannot append to DOM'))

// TODO: re-use the same video tag
function appendToMediaSource () {
if (!MediaSource) {
return cb(new Error(
Expand All @@ -55,27 +55,27 @@ module.exports = function appendTo (file, rootElem, cb) {
elem.addEventListener('error', fallbackToMediaSource)
elem.addEventListener('playing', onPlaying)
videostream(file, elem)
appendElem()
}

function useMediaSource () {
debug('Use MediaSource API for ' + file.name)
createElem()
elem.addEventListener('error', fallbackToBlobURL)
elem.addEventListener('playing', onPlaying)

file.createReadStream().pipe(new MediaSourceStream(elem, { extname: extname }))
appendElem()
if (currentTime) elem.currentTime = currentTime
}

function useBlobURL () {
debug('Use Blob URL for ' + file.name)
createElem()
elem.addEventListener('error', fatalError)
elem.addEventListener('playing', onPlaying)
appendElem()
file.getBlobURL(function (err, url) {
if (err) return fatalError(err)
elem.src = url
if (currentTime) elem.currentTime = currentTime
})
}

Expand All @@ -95,16 +95,19 @@ module.exports = function appendTo (file, rootElem, cb) {
useBlobURL()
}

function createElem () {
if (elem) elem.remove()
elem = window.elem = document.createElement(tagName)
elem.controls = true
elem.autoplay = true
}
function createElem (time) {
if (!elem) {
elem = document.createElement(tagName)
elem.controls = true
elem.autoplay = true // for chrome
elem.play() // for firefox

function appendElem () {
rootElem.appendChild(elem)
elem.play()
elem.addEventListener('progress', function () {
currentTime = elem.currentTime
})

rootElem.appendChild(elem)
}
}
}

Expand Down

0 comments on commit 2b28312

Please sign in to comment.