svrooij / node-sonos-ts

:speaker: Sonos control library, use this library in your own appliction.
https://sonos-ts.svrooij.io/
MIT License
84 stars 18 forks source link

Questions about Spotify and tunein #52

Closed tjoskar closed 4 years ago

tjoskar commented 4 years ago

Hi, this is probably not an issue with this lib per se but I have some issues regarding Spotify and tunein, and wonder if any of you could point me in the right direction.

Play Spotify playlists.

Let's take the following code:

const device = new SonosDevice(host, port);
device.SetAVTransportURI(spotifyUri);
device.Play();

This works fine as long as spotifyUri is a single track like: spotify:track:3b9xTm2eiaCRTGqUEWuzxc but if I try a playlist: spotify:playlist:37i9dQZEVXbLoATJ81JYXz I get back Sonos error on SetAVTransportURI UPnPError 714. I get the same error if I try an artist (spotify:artist:1Xyo4u8uXC1ZmMpatF05PJ) or an album (spotify:album:6YlDIxqEjvY63ffH6AwCjd). Is this expected? Do I need to include more meta data of some sort?

Play tunein radio stations

If I run the example code from this example: https://github.com/svrooij/node-sonos-ts/blob/master/examples/music-services.js

device.MusicServicesList().then(services => {
  services.forEach(s => {
    const cap = parseInt(s.Capabilities);
    console.log(
      '%s%s\t%s\t%s\t%s',
      String(s.Id).padEnd(5, ' '),
      s.Name.padEnd(20, ' '),
      s.Policy.Auth.padEnd(9, ' '),
      s.Capabilities.toString().padStart(9, ' '),
      cap.toString(2).padStart(25, ' ')
    );
  });
}).catch(console.error);

I get for example:

277  NRK Radio              Anonymous       37377            1001001000000001
235  Sveriges Radio         Anonymous       70145           10001001000000001

If I then run the following code (from https://github.com/svrooij/node-sonos-ts/blob/master/examples/music-services.js):

device.MusicServicesClient('NRK Radio').then(musicClient => {
  return musicClient.Search({
    id: 'A:STATION',
    term: 'P1',
    index: 0,
    count: 10,
  });
}).then(searchResult => {
  console.log(searchResult.mediaMetadata?.[0].trackUri); // x-sonosapi-stream:live:p1?sid=277
  if (searchResult.mediaMetadata?.[0].trackUri) {
    return device.SetAVTransportURI(searchResult.mediaMetadata[0].trackUri).then(success => device.Play());
  }
}).catch(console.error);

I get the following error: Sonos error on SetAVTransportURI UPnPError 402

And if I try to search for any other station:

device.MusicServicesClient('Sveriges Radio').then(musicClient => {
  return musicClient.Search({
    id: 'A:STATION',
    term: 'P1',
    index: 0,
    count: 10,
  });
})

I get the error: Trying to access Search via searchA. How do I know what id I should pass? If I take a look at the documentation for Soson I see that I can use getMetadata. But that function does also require an id? So whatever id I try I get an error message like this back: Trying to access GetMediaMetadata via getMediaMetadataStations.

I understand that this is not an issue with this library per se but if you can guide me in the right direction I can update the documentation. Thanks.

tjoskar commented 4 years ago

I have seen that some work has been done in https://github.com/bencevans/node-sonos/issues/186 for spotify but I get simular issues with node-sonos as well.

svrooij commented 4 years ago

First the MusicServiceClient is just a work-in-progress, it requires some reverse engineering .

The SetAVTransportUri command used the metadata helper. To guess the correct track uri and metadata based on the inputted track uri. I’m not sure what is supports, but it should support the Spotify artist uri.

Please start the library with DEBUG=sonos:* that should display what’s wrong with the metadata generation.

As of the latest version you can play tunein by sending SetAVTransportUri('radio:s12345') change your station Id.

svrooij commented 4 years ago

:tada: This issue has been resolved in version 2.0.3 :tada:

The release is available on:

Your semantic-release bot :package::rocket:

svrooij commented 4 years ago

I've improved the metadata helper and added a special section to the documentation about metadata. It seemed that you were trying to SetAVTransportUri with a container. Containers (playlist/album/more then 1 songs) should be added to the queue. Since Sonos needs a queue to play multiple songs.

If you got any other issues please create a new issue.

This library took a lot off time for me to build so any help is greatly appreciated.