amueller-tech / spotifycontroller

Simple API Wrapper to use the Sonos-Kids-Controller with Spotify Connect
MIT License
9 stars 3 forks source link

play issue after startup of the Sonos-Kids-Controller box #6

Open HeikoGr opened 3 years ago

HeikoGr commented 3 years ago

When using spotify on another client (e.g. smartphone) and starting Sonos-Kids-Controller the Box starts resuming the old track - not the selected. I have to leave the selected album (player) and return to the overview (medialist) and select the album again.

I think i found the reason in https://github.com/amueller-tech/spotifycontroller/blob/c820f5564b118585e8fb75709e668b66c6ba811f/spotify-control.js#L99 the play function throws an error and starts the function "handleSpotifyError" instead of starting to play.

amueller-tech commented 3 years ago

Hi,

I remember seeing a similar issue a while ago, but cannot reproduce it with the latest sonos-kids-controller version. Here's what I did: 1 started spotifycontroller 2 used spotify on my phone and stopped it (closed it) 3 started sonos-kids-controller and played an album, it directly switched to the correct album 4 returned to overview (medialist) which paused the album 5 used spotify on my phone again playing something different 6 started another album on the sonos-kids-controller -> it directly switched to the correct album.

What doesn't work:

HeikoGr commented 3 years ago

i'll try to give my best. This ist my problem-case:

  1. sonos-kids-controller (skc) box is off
  2. start spotify on smartphone and play something stupid
  3. stop playback on smartphone
  4. start skc box
  5. select audiobook
  6. skc will resume stupid track from step 2
  7. on skc go back to medialist
  8. select audiobook
  9. everything is working now. correct audiobook is playing

i am using the latest version(s)

amueller-tech commented 3 years ago

I was now able to reproduce it and to create a quick hack that fixes it on my side. Would you be able to try it out and give feedback if this also works for you so I can create a more sustainable fix.

I created a branch for it here: https://github.com/amueller-tech/spotifycontroller/tree/playback-fix

HeikoGr commented 3 years ago

Thank yo for this very fast respond.

I did a few quick tests on the breakfast table by checking out the new branch and rebooting the system ("pm2 restart all" with and without restarting the system) - it's even worse :-(

Edit: Did you test it with your forked version? Because i am using the original version at the time of testing.

If the smartphone is still the active playback device the formerly played song is resumed - on the smartphone. I even cannot switch to a selected audiobook by going back to medialist.

If i close Spotify on my smartphone and try to overtake on the box it will resume the formerly played song - on the box. I either cannot switch to a selected audiobook by going back to medialist.

spotify- | [Spotify Control] Webserver is running on port: 5005
0|spotify- | The access token has been refreshed!

0|spotify-control  | [Spotify Control] got request 
0|spotify-control  | {
0|spotify-control  |   root: '/',
0|spotify-control  |   dir: '/<device-id>',
0|spotify-control  |   base: 'clearqueue',
0|spotify-control  |   ext: '',
0|spotify-control  |   name: 'clearqueue'
0|spotify-control  | }
0|spotify-control  | [Spotify Control] got request 
0|spotify-control  | {
0|spotify-control  |   root: '/',
0|spotify-control  |   dir: '/<device-id>',
0|spotify-control  |   base: 'pause',
0|spotify-control  |   ext: '',
0|spotify-control  |   name: 'pause'
0|spotify-control  | }
0|spotify-control  | [Spotify Control] got request 
0|spotify-control  | {
0|spotify-control  |   root: '/',
0|spotify-control  |   dir: '/<device-id>/spotify/now',
0|spotify-control  |   base: 'spotify:album:0G32EFyQTN50PitqEXzon7',
0|spotify-control  |   ext: '',
0|spotify-control  |   name: 'spotify:album:0G32EFyQTN50PitqEXzon7'
0|spotify-control  | }
0|spotify-control  | [Spotify Control] got request 
0|spotify-control  | {
0|spotify-control  |   root: '/',
0|spotify-control  |   dir: '/<device-id>',
0|spotify-control  |   base: 'play',
0|spotify-control  |   ext: '',
0|spotify-control  |   name: 'play'
0|spotify-control  | }
0|spotify-control  | [Spotify Control] Getting available devices...
0|spotify-control  | no active device, setting the first one found to active
0|spotify-control  | no active device, setting the first one found to active
0|spotify-control  | [Spotify Control] Getting available devices...
0|spotify-control  | [Spotify Control] Getting available devices...
0|spotify-control  | [Spotify Control] Transfering playback to raspotify
0|spotify-control  | [Spotify Control] Transfering playback to raspotify
0|spotify-control  | [Spotify Control] got request 
0|spotify-control  | {
0|spotify-control  |   root: '/',
0|spotify-control  |   dir: '/<device-id>'‚
0|spotify-control  |   base: 'pause',
0|spotify-control  |   ext: '',
0|spotify-control  |   name: 'pause'
0|spotify-control  | }
0|spotify-control  | [Spotify Control] Playback paused

Album 0G32EFyQTN50PitqEXzon7 isn't played.

Going back to a complete different music album (1SD6ODpbAOYA94wEOkA2Ke) doesn't change the played track at all:


[Spotify Control] got request 
{
  root: '/',
  dir: '/<device-id>',
  base: 'pause',
  ext: '',
  name: 'pause'
}
[Spotify Control] got request 
{
  root: '/',
  dir: '/<device-id>/spotify/now',
  base: 'spotify:album: 1SD6ODpbAOYA94wEOkA2Ke',
  ext: '',
  name: 'spotify:album:1SD6ODpbAOYA94wEOkA2Ke'
}
[Spotify Control] got request 
{
  root: '/',
  dir: '/<device-id>',
  base: 'play',
  ext: '',
  name: 'play'
}
[Spotify Control] Playback paused
[Spotify Control] Getting available devices...
[Spotify Control] Playback started
[Spotify Control] got request 
{
  root: '/',
  dir: '/<device-id>',
  base: 'pause',
  ext: '',
  name: 'pause'
}
[Spotify Control] Playback paused

I'll try to go in the deep this evening.

Edit: i need to clarify that the "old" behaviour (origin/main branch) also resumes to play on the smartphone if the smartphone is still operating as spotify device.

HeikoGr commented 3 years ago

thanks to a few dozen debug.log entries i found my problem: the device id from my raspotify instance has changed somehow.

I changed the device (room) definition in /home/pi/Sonos-Kids/controller/server/config/config.json and the hotfix worked perfectly.

Is this a personal problem, or something the code should handle?

amueller-tech commented 3 years ago

Interesting! Did the raspotify ID change without any update/new installation? I've never seen an ID of a device changing

Spotifycontroller simply takes the ID as provided by the Sonos-Kids-Controller and transfers the playback to this ID. If the provided device doesn't exist, it transfers the playback to the very first one in the list from getDevices.

The code can only handle it in combination with the modified sonos-kids-controller as it dynamically provides a list of devices that you can select from when playing (https://github.com/amueller-tech/Sonos-Kids-Controller).

HeikoGr commented 3 years ago

I have no idea...

Maybe i copied the config.json from my first setup/sdcard attempt and didn't change the device id.

The select menu is no option, as i put everything (including audio HAT and speakers) in the box and don't want to confuse my kids.

amueller-tech commented 3 years ago

Ok, let's watch out for it if it happens again.

I'll leave the issue open until I manage to merge the hotfix into master, need to refactor a bit.

HeikoGr commented 3 years ago

Sounds good.

I got a deeper understanding of my former problem:

Solution: I don't know. Maybe connect to the first found device in "app.use" function and call playMe, or don't connect to any first found device in the error handling routine.

Maybe i will be able to make a proper pull request in the evening.