Listen to events on new cast devices
This commit is contained in:
@@ -33,19 +33,23 @@ function init (appState, callback) {
|
|||||||
state = appState
|
state = appState
|
||||||
update = callback
|
update = callback
|
||||||
|
|
||||||
|
state.devices.chromecast = chromecastPlayer()
|
||||||
|
state.devices.dlna = dlnaPlayer()
|
||||||
|
state.devices.airplay = airplayPlayer(browser)
|
||||||
|
|
||||||
// Listen for devices: Chromecast, DLNA and Airplay
|
// Listen for devices: Chromecast, DLNA and Airplay
|
||||||
chromecasts.on('update', function () {
|
chromecasts.on('update', function (device) {
|
||||||
// TODO: how do we tell if there are *no longer* any Chromecasts available?
|
// TODO: how do we tell if there are *no longer* any Chromecasts available?
|
||||||
// From looking at the code, chromecasts.players only grows, never shrinks
|
// From looking at the code, chromecasts.players only grows, never shrinks
|
||||||
if (!state.devices.chromecast) state.devices.chromecast = chromecastPlayer()
|
state.devices.chromecast.addDevice(device)
|
||||||
})
|
})
|
||||||
|
|
||||||
dlnacasts.on('update', function () {
|
dlnacasts.on('update', function (device) {
|
||||||
if (!state.devices.dlna) state.devices.dlna = dlnaPlayer()
|
state.devices.dlna.addDevice(device)
|
||||||
})
|
})
|
||||||
|
|
||||||
airplayer.on('update', function (player) {
|
airplayer.on('update', function (player) {
|
||||||
if (!state.devices.airplay) state.devices.airplay = airplayPlayer(player)
|
state.devices.airplay.addDevice(player)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -53,7 +57,7 @@ function init (appState, callback) {
|
|||||||
function chromecastPlayer () {
|
function chromecastPlayer () {
|
||||||
var ret = {
|
var ret = {
|
||||||
device: null,
|
device: null,
|
||||||
addEvents,
|
addDevice,
|
||||||
getDevices,
|
getDevices,
|
||||||
open,
|
open,
|
||||||
play,
|
play,
|
||||||
@@ -69,8 +73,9 @@ function chromecastPlayer () {
|
|||||||
return chromecasts.players
|
return chromecasts.players
|
||||||
}
|
}
|
||||||
|
|
||||||
function addEvents () {
|
function addDevice (device) {
|
||||||
ret.device.on('error', function (err) {
|
device.on('error', function (err) {
|
||||||
|
if (device !== ret.device) return
|
||||||
state.playing.location = 'local'
|
state.playing.location = 'local'
|
||||||
state.errors.push({
|
state.errors.push({
|
||||||
time: new Date().getTime(),
|
time: new Date().getTime(),
|
||||||
@@ -78,7 +83,8 @@ function chromecastPlayer () {
|
|||||||
})
|
})
|
||||||
update()
|
update()
|
||||||
})
|
})
|
||||||
ret.device.on('disconnect', function () {
|
device.on('disconnect', function () {
|
||||||
|
if (device !== ret.device) return
|
||||||
state.playing.location = 'local'
|
state.playing.location = 'local'
|
||||||
update()
|
update()
|
||||||
})
|
})
|
||||||
@@ -138,7 +144,7 @@ function chromecastPlayer () {
|
|||||||
function airplayPlayer () {
|
function airplayPlayer () {
|
||||||
var ret = {
|
var ret = {
|
||||||
device: null,
|
device: null,
|
||||||
addEvents,
|
addDevice,
|
||||||
getDevices,
|
getDevices,
|
||||||
open,
|
open,
|
||||||
play,
|
play,
|
||||||
@@ -150,12 +156,8 @@ function airplayPlayer () {
|
|||||||
}
|
}
|
||||||
return ret
|
return ret
|
||||||
|
|
||||||
function getDevices () {
|
function addDevice (player) {
|
||||||
return airplay.players
|
player.on('event', function (event) {
|
||||||
}
|
|
||||||
|
|
||||||
function addEvents () {
|
|
||||||
ret.device.on('event', function (event) {
|
|
||||||
switch (event.state) {
|
switch (event.state) {
|
||||||
case 'loading':
|
case 'loading':
|
||||||
break
|
break
|
||||||
@@ -172,6 +174,10 @@ function airplayPlayer () {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function getDevices () {
|
||||||
|
return airplay.players
|
||||||
|
}
|
||||||
|
|
||||||
function open () {
|
function open () {
|
||||||
ret.device.play(state.server.networkURL, function (err, res) {
|
ret.device.play(state.server.networkURL, function (err, res) {
|
||||||
if (err) {
|
if (err) {
|
||||||
@@ -230,7 +236,7 @@ function airplayPlayer () {
|
|||||||
function dlnaPlayer (player) {
|
function dlnaPlayer (player) {
|
||||||
var ret = {
|
var ret = {
|
||||||
device: null,
|
device: null,
|
||||||
addEvents,
|
addDevice,
|
||||||
getDevices,
|
getDevices,
|
||||||
open,
|
open,
|
||||||
play,
|
play,
|
||||||
@@ -246,8 +252,9 @@ function dlnaPlayer (player) {
|
|||||||
return dlnacasts.players
|
return dlnacasts.players
|
||||||
}
|
}
|
||||||
|
|
||||||
function addEvents () {
|
function addDevice (device) {
|
||||||
ret.device.on('error', function (err) {
|
device.on('error', function (err) {
|
||||||
|
if (device !== ret.device) return
|
||||||
state.playing.location = 'local'
|
state.playing.location = 'local'
|
||||||
state.errors.push({
|
state.errors.push({
|
||||||
time: new Date().getTime(),
|
time: new Date().getTime(),
|
||||||
@@ -255,7 +262,8 @@ function dlnaPlayer (player) {
|
|||||||
})
|
})
|
||||||
update()
|
update()
|
||||||
})
|
})
|
||||||
ret.device.on('disconnect', function () {
|
device.on('disconnect', function () {
|
||||||
|
if (device !== ret.device) return
|
||||||
state.playing.location = 'local'
|
state.playing.location = 'local'
|
||||||
update()
|
update()
|
||||||
})
|
})
|
||||||
|
|||||||
@@ -442,7 +442,7 @@ function renderPlayerControls (state) {
|
|||||||
airplayHandler = dispatcher('startCasting', 'airplay')
|
airplayHandler = dispatcher('startCasting', 'airplay')
|
||||||
dlnaHandler = dispatcher('startCasting', 'dlna')
|
dlnaHandler = dispatcher('startCasting', 'dlna')
|
||||||
}
|
}
|
||||||
if (state.devices.chromecast || isOnChromecast) {
|
if (state.devices.chromecast.getDevices().length > 0 || isOnChromecast) {
|
||||||
var castIcon = isOnChromecast ? 'cast_connected' : 'cast'
|
var castIcon = isOnChromecast ? 'cast_connected' : 'cast'
|
||||||
elements.push(hx`
|
elements.push(hx`
|
||||||
<i.icon.device.float-right
|
<i.icon.device.float-right
|
||||||
@@ -452,7 +452,7 @@ function renderPlayerControls (state) {
|
|||||||
</i>
|
</i>
|
||||||
`)
|
`)
|
||||||
}
|
}
|
||||||
if (state.devices.airplay || isOnAirplay) {
|
if (state.devices.airplay.getDevices().length > 0 || isOnAirplay) {
|
||||||
elements.push(hx`
|
elements.push(hx`
|
||||||
<i.icon.device.float-right
|
<i.icon.device.float-right
|
||||||
class=${airplayClass}
|
class=${airplayClass}
|
||||||
@@ -461,7 +461,7 @@ function renderPlayerControls (state) {
|
|||||||
</i>
|
</i>
|
||||||
`)
|
`)
|
||||||
}
|
}
|
||||||
if (state.devices.dlna || isOnDlna) {
|
if (state.devices.dlna.getDevices().length > 0 || isOnDlna) {
|
||||||
elements.push(hx`
|
elements.push(hx`
|
||||||
<i
|
<i
|
||||||
class='icon device float-right'
|
class='icon device float-right'
|
||||||
|
|||||||
Reference in New Issue
Block a user