sync/player/youtube.coffee

97 lines
2.9 KiB
CoffeeScript
Raw Normal View History

2015-05-02 16:45:35 +00:00
window.YouTubePlayer = class YouTubePlayer extends Player
2015-04-24 02:40:08 +00:00
constructor: (data) ->
2015-05-02 16:45:35 +00:00
if not (this instanceof YouTubePlayer)
return new YouTubePlayer(data)
2015-04-24 03:24:43 +00:00
@setMediaProperties(data)
2015-04-30 20:26:09 +00:00
@pauseSeekRaceCondition = false
2015-04-24 03:24:43 +00:00
2015-04-24 02:40:08 +00:00
waitUntilDefined(window, 'YT', =>
2015-05-02 22:55:00 +00:00
# Even after window.YT is defined, YT.Player may not be, which causes a
2015-05-05 20:06:37 +00:00
# 'YT.Player is not a constructor' error occasionally
2015-05-02 22:55:00 +00:00
waitUntilDefined(YT, 'Player', =>
removeOld()
@yt = new YT.Player('ytapiplayer',
videoId: data.id
playerVars:
autohide: 1
autoplay: 1
2015-05-02 22:55:00 +00:00
controls: 1
iv_load_policy: 3 # iv_load_policy 3 indicates no annotations
rel: 0
events:
onReady: @onReady.bind(this)
onStateChange: @onStateChange.bind(this)
)
2015-04-24 02:40:08 +00:00
)
)
2015-04-24 03:24:43 +00:00
load: (data) ->
2015-05-02 16:45:35 +00:00
@setMediaProperties(data)
2015-05-02 22:37:09 +00:00
if @yt and @yt.ready
2015-04-24 03:24:43 +00:00
@yt.loadVideoById(data.id, data.currentTime)
2015-05-02 22:37:09 +00:00
else
console.error('WTF? YouTubePlayer::load() called but yt is not ready')
2015-04-24 03:24:43 +00:00
2015-04-24 02:40:08 +00:00
onReady: ->
2015-05-02 22:37:09 +00:00
@yt.ready = true
2015-05-02 16:45:35 +00:00
@setVolume(VOLUME)
2015-04-24 02:40:08 +00:00
onStateChange: (ev) ->
# If you pause the video before the first PLAYING
# event is emitted, weird things happen (or at least that was true
# whenever this comment was authored in 2015).
2015-04-24 03:24:43 +00:00
if ev.data == YT.PlayerState.PLAYING and @pauseSeekRaceCondition
@pause()
@pauseSeekRaceCondition = false
if (ev.data == YT.PlayerState.PAUSED and not @paused) or
(ev.data == YT.PlayerState.PLAYING and @paused)
@paused = (ev.data == YT.PlayerState.PAUSED)
if CLIENT.leader
sendVideoUpdate()
if ev.data == YT.PlayerState.ENDED and CLIENT.leader
socket.emit('playNext')
play: ->
2015-04-30 20:26:09 +00:00
@paused = false
2015-05-02 22:37:09 +00:00
if @yt and @yt.ready
2015-04-24 03:24:43 +00:00
@yt.playVideo()
pause: ->
2015-04-30 20:26:09 +00:00
@paused = true
2015-05-02 22:37:09 +00:00
if @yt and @yt.ready
2015-04-24 03:24:43 +00:00
@yt.pauseVideo()
seekTo: (time) ->
2015-05-02 22:37:09 +00:00
if @yt and @yt.ready
2015-04-24 03:24:43 +00:00
@yt.seekTo(time, true)
setVolume: (volume) ->
2015-05-02 22:37:09 +00:00
if @yt and @yt.ready
2015-04-24 03:24:43 +00:00
if volume > 0
# If the player is muted, even if the volume is set,
# the player remains muted
@yt.unMute()
@yt.setVolume(volume * 100)
2015-05-02 22:55:00 +00:00
setQuality: (quality) ->
# https://github.com/calzoneman/sync/issues/726
2015-05-02 22:55:00 +00:00
2015-04-24 03:24:43 +00:00
getTime: (cb) ->
2015-05-02 22:37:09 +00:00
if @yt and @yt.ready
2015-04-24 03:24:43 +00:00
cb(@yt.getCurrentTime())
2015-04-30 20:26:09 +00:00
else
cb(0)
2015-04-24 02:40:08 +00:00
2015-04-24 03:24:43 +00:00
getVolume: (cb) ->
2015-05-02 22:37:09 +00:00
if @yt and @yt.ready
2015-04-24 03:24:43 +00:00
if @yt.isMuted()
2015-04-30 20:26:09 +00:00
cb(0)
2015-04-24 03:24:43 +00:00
else
2015-04-30 20:26:09 +00:00
cb(@yt.getVolume() / 100)
else
cb(VOLUME)