CyTube/player/youtube.coffee

122 lines
3.8 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)
@qualityRaceCondition = true
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()
wmode = if USEROPTS.wmode_transparent then 'transparent' else 'opaque'
@yt = new YT.Player('ytapiplayer',
videoId: data.id
playerVars:
autohide: 1
autoplay: 1
controls: 1
iv_load_policy: 3 # iv_load_policy 3 indicates no annotations
rel: 0
wmode: wmode
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)
@qualityRaceCondition = true
if USEROPTS.default_quality
2015-05-02 22:55:00 +00:00
@setQuality(USEROPTS.default_quality)
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) ->
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
2015-04-30 20:26:09 +00:00
if USEROPTS.default_quality
2015-05-02 22:55:00 +00:00
@setQuality(USEROPTS.default_quality)
2015-04-24 03:24:43 +00:00
# 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: ->
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) ->
if not @yt or not @yt.ready
return
ytQuality = switch String(quality)
2015-05-05 20:06:37 +00:00
when '240' then 'small'
when '360' then 'medium'
when '480' then 'large'
when '720' then 'hd720'
when '1080' then 'hd1080'
when 'best' then 'highres'
else 'auto'
if ytQuality != 'auto'
2015-05-02 22:55:00 +00:00
@yt.setPlaybackQuality(ytQuality)
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)