0
votes

I am having some trouble getting Spotify playlists to play with the beta Spotify SDK. I am, somewhat, following this tutorial on playback with the Spotify Player class. Unlike in the example, though, I want to play tracks and playlists provided by the user, so I must collect user tracks and playlist data, which I have done with Kaaes Android Web Wrapper. Having collected the data I need, I try to build the Spotify player (which seems to build fine), then from a separate method, attempt to play a track or playlist. Interestingly, I am able to get a track to play back only if I call it during the build process (like it is in tutorial), but not called from another method, and I cannot get playlists to play at all. I get an error while attempting to play playlists that reads:

com.spotify.sdk.android.player.NativeSpotifyException: Failed SpPlayUri with error code 5 (An unexpected argument value was passed to a function)

The syntax for playlist that I pass to the play method looks like:

spotify:playlist:4vDeSXEwfHMYOuQaTtwlUK

which is similar to track's:

spotify:track:2TpxZ7JUBn3uw46aR7qd6V

That could, of course, be incorrect, but it's hard to find examples online of how to play playlists on the Android SDK. So can anyone please show me how to play playlists and how to play content on the Spotify Player object, especially away from the Player builder? I will provide some relevant section of my work below. FWIW, I am handling authentication from another activity, and it hasn't given me too many problems yet...

private PlayerState                         state;
private Player                              player;
...
public void buildPlayer(){
    //start a Spotify player
    Config playerConfig = new Config(this, token, client_id);
    Spotify.getPlayer(playerConfig, this, new Player.InitializationObserver() {
        @Override
        public void onInitialized(Player p) {
            player = p;
            player.addPlayerNotificationCallback(SpotifyPlayer.this);
            player.setPlaybackBitrate(PlaybackBitrate.BITRATE_NORMAL);
            player.getPlayerState(new PlayerStateCallback() {
                @Override
                public void onPlayerState(PlayerState playerState) {
                    state = playerState;
                }
            });
        }
        @Override
        public void onError(Throwable throwable) {
            Log.e(ID, "Could not initialize player: " + throwable.getMessage());
        }
    });
}

...
public void test(){
    try {
        player.play("spotify:track:2TpxZ7JUBn3uw46aR7qd6V");
        //oddly enough, the track fails to play here, but not if I have this line in the builder
    }catch (Exception e){
        System.out.println("player failed to play");
        e.printStackTrace();
    }
}

There is more stuff going on in this class, but none of that should interfere right now. I look forward to hearing your suggestions.

1

1 Answers

0
votes

You receive that error because the URI of the Spotify playlist you use is incorrect. The correct format is:

spotify:user:{user_id}:playlist:{playlist_id}

For example:

spotify:user:spotify:playlist:4hOKQuZbraPDIfaGbM3lKI

The other issue is caused by the fact that you assign the field player only when the callback onInitialized is fired, probably when you call test the field player is still null and you get a NullPointerException.

Change your method buildPlayer like this:

public void buildPlayer(){
    //start a Spotify player
    Config playerConfig = new Config(this, token, client_id);
    player = Spotify.getPlayer(playerConfig, this, new Player.InitializationObserver() {
        @Override
        public void onInitialized(Player p) {
            player.addPlayerNotificationCallback(SpotifyPlayer.this);
            player.setPlaybackBitrate(PlaybackBitrate.BITRATE_NORMAL);
            player.getPlayerState(new PlayerStateCallback() {
                @Override
                public void onPlayerState(PlayerState playerState) {
                    state = playerState;
                }
            });
        }
        @Override
        public void onError(Throwable throwable) {
            Log.e(ID, "Could not initialize player: " + throwable.getMessage());
        }
    });
}