Search code examples
spotifyspotify-desktop

Spotify API-Net ResumePlayback - Error With Code Example From Documentation


The documentation at Spotiy API_NET for ResumePlayback

gives the following example:

ErrorResponse error = _spotify.ResumePlayback(uris: new List<string> { "spotify:track:4iV5W9uYEdYUVa79Axb7Rh" });

When I try that code in C#, I get the following code error which prevents me building:

Error CS0121 The call is ambiguous between the following methods or properties: 'SpotifyWebAPI.ResumePlayback(string, string, List, int?)' and 'SpotifyWebAPI.ResumePlayback(string, string, List, string)' Can anyone tell me what is wrong with this?

Also, what is the simplest way to simply resume the existing player at the point where it was paused?

Edit

@rene answered the first part of my question.

In regard to the second part, how to resume the existing player at the point where it was paused, I got the answer through the library's Github site, it's simply:

_spotify.ResumePlayback(offset: "")

Solution

  • The ResumePlayback method has two overloads that take these parameters:

    ErrorResponse ResumePlayback(string deviceId = "", 
                                string contextUri = "", 
                                List<string> uris = null,
                                int? offset = null)
    

    and

    ErrorResponse ResumePlayback(string deviceId = "", 
                                 string contextUri = "", 
                                 List<string> uris = null,
                                 string offset = "")
    

    When the compiler comes across this line

    ErrorResponse error = _spotify.ResumePlayback(
                                uris: new List<string> { "spotify:track:4iV5W9uYEdYUVa79Axb7Rh" });
    

    it has to decide which ResumePlayback it is going to call and it doesn't want to take a guess or roll a dice.

    It looks at which parameters are going to be provided and you only give it uris (that is the third parameter). It will assume the defaults for the other parameters. For both methods these defaults (null for strings or for the Nullable<int> (int?)) apply so the compiler can't decide which method it should bind to. It shows you an error.

    Provide more parameters so the compiler can pick an unique overload.

    ErrorResponse error = _spotify.ResumePlayback(
                                uris: new List<string> { "spotify:track:4iV5W9uYEdYUVa79Axb7Rh" }
                                , 
                                offset: 0
                           );
    

    Adding that named parameter offset and setting it to the int value of 0 is enough for the compiler to pick this overload to bind to:

    ResumePlayback(string deviceId, string contextUri, List<string> uris, int? offset)