sync/player/youtube.coffee

91 lines
2.6 KiB
CoffeeScript
Raw Normal View History

2015-04-24 02:40:08 +00:00
class YouTubePlayer extends Player
constructor: (data) ->
2015-04-24 03:24:43 +00:00
@setMediaProperties(data)
@qualityRaceCondition = true
@pauseSeekRaceCondition = true
2015-04-24 02:40:08 +00:00
waitUntilDefined(window, 'YT', =>
removeOld()
wmode = if USEROPTS.wmode_transparent then 'transparent' else 'opaque'
@yt = new YT.Player('ytapiplayer',
videoId: data.id
playerVars:
autohide: 1
autoplay: 1
controls: 1
2015-04-24 03:24:43 +00:00
iv_load_policy: 3 # iv_load_policy 3 indicates no annotations
2015-04-24 02:40:08 +00:00
rel: 0
wmode: wmode
events:
onReady: @onReady.bind(this)
onStateChange: @onStateChange.bind(this)
)
)
2015-04-24 03:24:43 +00:00
load: (data) ->
super(data)
if @yt
@yt.loadVideoById(data.id, data.currentTime)
@qualityRaceCondition = true
if USEROPTS.default_quality
@yt.setPlaybackQuality(USEROPTS.default_quality)
2015-04-24 02:40:08 +00:00
onReady: ->
@yt.setVolume(VOLUME)
onStateChange: (ev) ->
2015-04-24 03:24:43 +00:00
# For some reason setting the quality doesn't work
# until the first event has fired.
if @qualityRaceCondition
@qualityRaceCondition = false
@yt.setPlaybackQuality(USEROPTS.default_quality)
# Similar to above, if you pause the video before the first PLAYING
# event is emitted, weird things happen.
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: ->
super()
if @yt
@yt.playVideo()
pause: ->
super()
if @yt
@yt.pauseVideo()
seekTo: (time) ->
if @yt
@yt.seekTo(time, true)
setVolume: (volume) ->
if @yt
if volume > 0
# If the player is muted, even if the volume is set,
# the player remains muted
@yt.unMute()
@yt.setVolume(volume * 100)
getTime: (cb) ->
if @yt
cb(@yt.getCurrentTime())
2015-04-24 02:40:08 +00:00
2015-04-24 03:24:43 +00:00
getVolume: (cb) ->
if @yt
if @yt.isMuted()
return 0
else
return @yt.getVolume() / 100.0