我在使用Spotify播放列表玩Spotify SDK时遇到了一些麻烦。我有点在使用Spotify Player类播放this tutorial之后。但是,与示例不同,我想播放用户提供的曲目和播放列表,因此我必须收集用户曲目和播放列表数据,这是我用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
与track的类似:
spotify:track:2TpxZ7JUBn3uw46aR7qd6V
当然,这可能是不正确的,但很难在网上找到如何在Android SDK上播放播放列表的示例。那么有人可以告诉我如何播放播放列表以及如何在Spotify播放器对象上播放内容,尤其是远离播放器构建器吗?我将在下面提供我的工作的一些相关部分。 FWIW,我正在处理来自另一项活动的身份验证,但它还没有给我太多问题......
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();
}
}
这堂课还有更多的东西,但现在都不应该干涉。我期待听到你的建议。
答案 0 :(得分:0)
您收到该错误,因为您使用的Spotify播放列表的URI不正确。正确的格式是:
spotify:user:{user_id}:playlist:{playlist_id}
例如:
spotify:user:spotify:playlist:4hOKQuZbraPDIfaGbM3lKI
另一个问题是由于您仅在回拨player
时触发了onInitialized
字段,可能是在您致电test
字段player
时仍为null,你得到一个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());
}
});
}