使用 Android 的 Spotify SDK 播放播放列表和曲目

Playing playlists and tracks with Spotify SDK for Android

我在使用 Beta Spotify SDK 播放 Spotify 播放列表时遇到了一些问题。我有点关注 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。收集了我需要的数据后,我尝试构建 Spotify 播放器(似乎构建良好),然后通过单独的方法尝试播放曲目或播放列表。有趣的是,只有在构建过程中调用它(就像在教程中一样),但不是从其他方法调用时,我才能获得要播放的曲目,而且我根本无法播放播放列表。我在尝试播放播放列表时收到一条错误消息:

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

我传递给 play 方法的播放列表语法如下所示:

spotify:playlist:4vDeSXEwfHMYOuQaTtwlUK

这与曲目相似:

spotify:track:2TpxZ7JUBn3uw46aR7qd6V

当然,这可能是不正确的,但很难在网上找到有关如何在 Android SDK 上播放播放列表的示例。那么谁能告诉我如何播放播放列表以及如何在 Spotify Player 对象上播放内容,尤其是远离 Player 构建器?我将在下面提供我工作的一些相关部分。 FWIW,我正在处理来自另一个 activity 的身份验证,它还没有给我带来太多问题......

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();
    }
}

class 中还有更多内容,但 none 现在应该会受到影响。我期待听到您的建议。

您收到该错误是因为您使用的 Spotify 播放列表的 URI 不正确。正确的格式是:

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

例如:

spotify:user:spotify:playlist:4hOKQuZbraPDIfaGbM3lKI

另一个问题是由于您仅在触发回调 onInitialized 时才分配字段 player,可能是在您调用 test 字段 [=13= 时] 仍然是空的,你得到一个 NullPointerException

像这样改变你的方法buildPlayer

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());
        }
    });
}