CyTube/player/youtube.coffee

99 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)
@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', =>
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) ->
2015-05-02 16:45:35 +00:00
@setMediaProperties(data)
2015-04-24 03:24:43 +00:00
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: ->
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
@yt.setPlaybackQuality(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-04-24 03:24:43 +00:00
if @yt
@yt.playVideo()
pause: ->
2015-04-30 20:26:09 +00:00
@paused = true
2015-04-24 03:24:43 +00:00
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-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) ->
if @yt
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)