Skip to content

Instantly share code, notes, and snippets.

@shinnn
Created July 20, 2013 15:39
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 shinnn/6045485 to your computer and use it in GitHub Desktop.
Save shinnn/6045485 to your computer and use it in GitHub Desktop.
video要素のループを滑らかにする ref: http://qiita.com/shinnn/items/83c2493ab3bbf49b1949
$ ->
video = document.getElementsByTagName('video')[0]
$video = null
rewind = (videoElm) ->
$(videoElm).one 'play', null, ->
videoElm.pause()
# この条件分岐が無いと DOM Exception 11 Error が発生
# TODO: 原因の発見
if videoElm.currentTime isnt 0
videoElm.currentTime = 0
return
videoElm.play()
return
startVideo = ->
startVideo = null
# 子要素に source 要素があるため、true を指定してクローン
videoClone = video.cloneNode true
video.parentNode.insertBefore videoClone, video
$video = $ [videoClone, video]
$video[1].play()
rewind $video[0]
$video.each (index) ->
$(this).on 'ended', null, ->
$video[1].style.visibility = if index is 1 then 'hidden' else ''
$video[1-index].play()
rewind this
return
if visibilityChange
$(document).on visibilityChange, null, ->
if document.hidden
if $video[1].style.visibility is 'hidden'
$video[0].pause()
else
$video[1].pause()
else
if $video[1].style.visibility is 'hidden'
$video[0].play()
else
$video[1].play()
return
$(video).one 'canplaythrough', null, ->
if startVideo then startVideo()
return
$w.on 'load', null, ->
if startVideo
console.log 'Video loaded from cache'
startVideo()
return
return
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment