alxhotel / chromecast-api

:tv: Chromecast Node.js module
MIT License
155 stars 48 forks source link

Status does not work #77

Open hgghyxo opened 2 years ago

hgghyxo commented 2 years ago

I was able to setup the API, but status reporting does not seem to work for me.

Is it possible google changed something in the background or it never worked like this?

const ChromecastAPI = require('chromecast-api')
const client = new ChromecastAPI()

client.on('device', (device) => {
  console.log(device.friendlyName, 'connected')

  device.getStatus((status) => {
    console.log(device.friendlyName, status)
  })
  // ==== Throws error: ====
  // Bedroom speaker Error: no session started
  //   at C:\Users\hgghy\Documents\Codes\castApi\node_modules\chromecast-api\lib\device.js:77:18
  //   at C:\Users\hgghy\Documents\Codes\castApi\node_modules\castv2-client\lib\controllers\receiver.js:86:5
  //   at C:\Users\hgghy\Documents\Codes\castApi\node_modules\castv2-client\lib\controllers\receiver.js:31:5
  //   at fn.onmessage (C:\Users\hgghy\Documents\Codes\castApi\node_modules\castv2-client\lib\controllers\request-response.js:27:7)
  //   at fn.emit (node:events:402:35)
  //   at Channel.onmessage (C:\Users\hgghy\Documents\Codes\castApi\node_modules\castv2-client\lib\controllers\controller.js:16:10)
  //   at Channel.emit (node:events:390:28)
  //   at Client.onmessage (C:\Users\hgghy\Documents\Codes\castApi\node_modules\castv2\lib\channel.js:23:10)
  //   at Client.emit (node:events:402:35)
  //   at PacketStreamWrapper.onpacket (C:\Users\hgghy\Documents\Codes\castApi\node_modules\castv2\lib\client.js:81:10)
  //   at PacketStreamWrapper.emit (node:events:390:28)
  //   at TLSSocket.<anonymous> (C:\Users\hgghy\Documents\Codes\castApi\node_modules\castv2\lib\packet-stream-wrapper.j    at TLSSocket.emit (node:events:390:28)
  //   at emitReadable_ (node:internal/streams/readable:601:12)
  //   at processTicksAndRejections (node:internal/process/task_queues:82:21)

  // ==== Does nothing: ====
  device.on('status', (status) => {
    console.log(device.friendlyName, status)
  })
})
Paul243-gif commented 6 months ago

It probably is because you never actually connected to the device. The getStatus and the on('status') functions, listen for a change in the status state, if it doesn't change, nothing happens. Try doing the calls after you have connected to a chromecast device (By doing device.play(mediaURL, [options], callback(error) ) ).