2016-08-25 03:21:49 +00:00
|
|
|
window.TwitchPlayer = class TwitchPlayer extends Player
|
2015-06-16 20:44:14 +00:00
|
|
|
constructor: (data) ->
|
|
|
|
if not (this instanceof TwitchPlayer)
|
|
|
|
return new TwitchPlayer(data)
|
|
|
|
|
2016-08-25 03:21:49 +00:00
|
|
|
@setMediaProperties(data)
|
|
|
|
waitUntilDefined(window, 'Twitch', =>
|
|
|
|
waitUntilDefined(Twitch, 'Player', =>
|
|
|
|
@init(data)
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
init: (data) ->
|
|
|
|
removeOld()
|
2016-09-05 01:53:38 +00:00
|
|
|
if data.type is 'tv'
|
|
|
|
# VOD
|
|
|
|
options =
|
|
|
|
video: data.id
|
|
|
|
else
|
|
|
|
# Livestream
|
|
|
|
options =
|
|
|
|
channel: data.id
|
2016-08-25 03:21:49 +00:00
|
|
|
@twitch = new Twitch.Player('ytapiplayer', options)
|
|
|
|
@twitch.addEventListener(Twitch.Player.READY, =>
|
|
|
|
@setVolume(VOLUME)
|
|
|
|
@twitch.setQuality(@mapQuality(USEROPTS.default_quality))
|
|
|
|
@twitch.addEventListener(Twitch.Player.PLAY, =>
|
2016-09-05 01:53:38 +00:00
|
|
|
@paused = false
|
2016-08-25 03:21:49 +00:00
|
|
|
if CLIENT.leader
|
|
|
|
sendVideoUpdate()
|
|
|
|
)
|
|
|
|
@twitch.addEventListener(Twitch.Player.PAUSE, =>
|
2016-09-05 01:53:38 +00:00
|
|
|
@paused = true
|
2016-08-25 03:21:49 +00:00
|
|
|
if CLIENT.leader
|
|
|
|
sendVideoUpdate()
|
|
|
|
)
|
|
|
|
@twitch.addEventListener(Twitch.Player.ENDED, =>
|
|
|
|
if CLIENT.leader
|
|
|
|
socket.emit('playNext')
|
|
|
|
)
|
|
|
|
)
|
2015-06-16 20:44:14 +00:00
|
|
|
|
|
|
|
load: (data) ->
|
2016-08-25 03:21:49 +00:00
|
|
|
@setMediaProperties(data)
|
|
|
|
try
|
2016-09-05 01:53:38 +00:00
|
|
|
if data.type is 'tv'
|
|
|
|
# VOD
|
|
|
|
@twitch.setVideo(data.id)
|
|
|
|
else
|
|
|
|
# Livestream
|
|
|
|
@twitch.setChannel(data.id)
|
2016-08-25 03:21:49 +00:00
|
|
|
catch error
|
|
|
|
console.error(error)
|
|
|
|
|
|
|
|
pause: ->
|
|
|
|
try
|
|
|
|
@twitch.pause()
|
2016-09-05 01:53:38 +00:00
|
|
|
@paused = true
|
2016-08-25 03:21:49 +00:00
|
|
|
catch error
|
|
|
|
console.error(error)
|
|
|
|
|
|
|
|
play: ->
|
|
|
|
try
|
|
|
|
@twitch.play()
|
2016-09-05 01:53:38 +00:00
|
|
|
@paused = false
|
2016-08-25 03:21:49 +00:00
|
|
|
catch error
|
|
|
|
console.error(error)
|
|
|
|
|
|
|
|
seekTo: (time) ->
|
|
|
|
try
|
|
|
|
@twitch.seek(time)
|
|
|
|
catch error
|
|
|
|
console.error(error)
|
|
|
|
|
|
|
|
getTime: (cb) ->
|
|
|
|
try
|
2016-09-05 01:53:38 +00:00
|
|
|
cb(@twitch.getCurrentTime())
|
2016-08-25 03:21:49 +00:00
|
|
|
catch error
|
|
|
|
console.error(error)
|
|
|
|
|
|
|
|
setVolume: (volume) ->
|
|
|
|
try
|
|
|
|
@twitch.setVolume(volume)
|
|
|
|
if volume > 0
|
|
|
|
@twitch.setMuted(false)
|
|
|
|
catch error
|
|
|
|
console.error(error)
|
|
|
|
|
|
|
|
getVolume: (cb) ->
|
|
|
|
try
|
|
|
|
if @twitch.isPaused()
|
|
|
|
cb(0)
|
|
|
|
else
|
|
|
|
cb(@twitch.getVolume())
|
|
|
|
catch error
|
|
|
|
console.error(error)
|
|
|
|
|
|
|
|
mapQuality: (quality) ->
|
|
|
|
switch String(quality)
|
|
|
|
when '1080' then 'chunked'
|
|
|
|
when '720' then 'high'
|
|
|
|
when '480' then 'medium'
|
|
|
|
when '360' then 'low'
|
|
|
|
when '240' then 'mobile'
|
|
|
|
when 'best' then 'chunked'
|
|
|
|
else ''
|