Fix video play on google chrome

pull/281/head
Chocobozzz 2018-02-07 10:01:37 +01:00
parent 0bc22f8d7e
commit 481d35966f
No known key found for this signature in database
GPG Key ID: 583A612D890159BE
2 changed files with 10 additions and 16 deletions

View File

@ -217,10 +217,15 @@ class PeerTubePlugin extends Plugin {
private playerElement: HTMLVideoElement private playerElement: HTMLVideoElement
private videoFiles: VideoFile[] private videoFiles: VideoFile[]
private torrent: WebTorrent.Torrent private torrent: WebTorrent.Torrent
private autoplay = false
constructor (player: videojs.Player, options: PeertubePluginOptions) { constructor (player: videojs.Player, options: PeertubePluginOptions) {
super(player, options) super(player, options)
// Fix canplay event on google chrome by disabling default videojs autoplay
this.autoplay = this.player.options_.autoplay
this.player.options_.autoplay = false
this.videoFiles = options.videoFiles this.videoFiles = options.videoFiles
// Hack to "simulate" src link in video.js >= 6 // Hack to "simulate" src link in video.js >= 6
@ -281,6 +286,7 @@ class PeerTubePlugin extends Plugin {
this.renderer = renderer this.renderer = renderer
if (!this.player.paused()) this.player.play().then(done) if (!this.player.paused()) this.player.play().then(done)
else done()
}) })
}) })
@ -349,18 +355,12 @@ class PeerTubePlugin extends Plugin {
const webTorrentButton = new WebTorrentButton(this.player) const webTorrentButton = new WebTorrentButton(this.player)
controlBar.webTorrent = controlBar.el().insertBefore(webTorrentButton.el(), controlBar.progressControl.el()) controlBar.webTorrent = controlBar.el().insertBefore(webTorrentButton.el(), controlBar.progressControl.el())
if (this.player.options_.autoplay === true) { if (this.autoplay === true) {
this.updateVideoFile() this.updateVideoFile(undefined, () => this.player.play())
} else { } else {
this.player.one('play', () => { this.player.one('play', () => {
// On firefox, we need to wait to load the video before playing
if (navigator.userAgent.toLowerCase().indexOf('firefox') !== -1) {
this.player.pause() this.player.pause()
this.updateVideoFile(undefined, () => this.player.play()) this.updateVideoFile(undefined, () => this.player.play())
return
}
this.updateVideoFile(undefined)
}) })
} }
} }

View File

@ -43,7 +43,6 @@ function renderMedia (file, elem: HTMLVideoElement, opts: RenderMediaOptions, ca
prepareElem() prepareElem()
preparedElem.addEventListener('error', fallbackToMediaSource) preparedElem.addEventListener('error', fallbackToMediaSource)
preparedElem.addEventListener('loadstart', onLoadStart) preparedElem.addEventListener('loadstart', onLoadStart)
preparedElem.addEventListener('canplay', onCanPlay)
return videostream(file, preparedElem) return videostream(file, preparedElem)
} }
@ -51,7 +50,6 @@ function renderMedia (file, elem: HTMLVideoElement, opts: RenderMediaOptions, ca
prepareElem() prepareElem()
preparedElem.addEventListener('error', callback) preparedElem.addEventListener('error', callback)
preparedElem.addEventListener('loadstart', onLoadStart) preparedElem.addEventListener('loadstart', onLoadStart)
preparedElem.addEventListener('canplay', onCanPlay)
const wrapper = new MediaElementWrapper(preparedElem) const wrapper = new MediaElementWrapper(preparedElem)
const writable = wrapper.createWriteStream(getCodec(file.name)) const writable = wrapper.createWriteStream(getCodec(file.name))
@ -64,7 +62,6 @@ function renderMedia (file, elem: HTMLVideoElement, opts: RenderMediaOptions, ca
function fallbackToMediaSource () { function fallbackToMediaSource () {
preparedElem.removeEventListener('error', fallbackToMediaSource) preparedElem.removeEventListener('error', fallbackToMediaSource)
preparedElem.removeEventListener('canplay', onCanPlay)
useMediaSource() useMediaSource()
} }
@ -82,10 +79,7 @@ function renderMedia (file, elem: HTMLVideoElement, opts: RenderMediaOptions, ca
function onLoadStart () { function onLoadStart () {
preparedElem.removeEventListener('loadstart', onLoadStart) preparedElem.removeEventListener('loadstart', onLoadStart)
if (opts.autoplay) preparedElem.play() if (opts.autoplay) preparedElem.play()
}
function onCanPlay () {
preparedElem.removeEventListener('canplay', onCanPlay)
callback(null, renderer) callback(null, renderer)
} }
} }