1 window.YouTubePlayer = class YouTubePlayer extends Player
3 if not (this instanceof YouTubePlayer)
4 return new YouTubePlayer(data)
6 @setMediaProperties(data)
7 @qualityRaceCondition = true
8 @pauseSeekRaceCondition = false
10 waitUntilDefined(window, 'YT', =>
11 # Even after window.YT is defined, YT.Player may not be, which causes a
12 # 'YT.Player is not a constructor' error occasionally
13 waitUntilDefined(YT, 'Player', =>
16 wmode = if USEROPTS.wmode_transparent then 'transparent' else 'opaque'
17 @yt = new YT.Player('ytapiplayer',
23 iv_load_policy: 3 # iv_load_policy 3 indicates no annotations
27 onReady: @onReady.bind(this)
28 onStateChange: @onStateChange.bind(this)
34 @setMediaProperties(data)
36 @yt.loadVideoById(data.id, data.currentTime)
37 @qualityRaceCondition = true
38 if USEROPTS.default_quality
39 @setQuality(USEROPTS.default_quality)
41 console.error('WTF? YouTubePlayer::load() called but yt is not ready')
47 onStateChange: (ev) ->
48 # For some reason setting the quality doesn't work
49 # until the first event has fired.
50 if @qualityRaceCondition
51 @qualityRaceCondition = false
52 if USEROPTS.default_quality
53 @setQuality(USEROPTS.default_quality)
55 # Similar to above, if you pause the video before the first PLAYING
56 # event is emitted, weird things happen.
57 if ev.data == YT.PlayerState.PLAYING and @pauseSeekRaceCondition
59 @pauseSeekRaceCondition = false
61 if (ev.data == YT.PlayerState.PAUSED and not @paused) or
62 (ev.data == YT.PlayerState.PLAYING and @paused)
63 @paused = (ev.data == YT.PlayerState.PAUSED)
67 if ev.data == YT.PlayerState.ENDED and CLIENT.leader
68 socket.emit('playNext')
82 @yt.seekTo(time, true)
84 setVolume: (volume) ->
87 # If the player is muted, even if the volume is set,
88 # the player remains muted
90 @yt.setVolume(volume * 100)
92 setQuality: (quality) ->
93 if not @yt or not @yt.ready
96 ytQuality = switch String(quality)
97 when '240' then 'small'
98 when '360' then 'medium'
99 when '480' then 'large'
100 when '720' then 'hd720'
101 when '1080' then 'hd1080'
102 when 'best' then 'highres'
105 if ytQuality != 'auto'
106 @yt.setPlaybackQuality(ytQuality)
110 cb(@yt.getCurrentTime())
119 cb(@yt.getVolume() / 100)