Spotify:保存播放状态信息

Spotify: save playback state information

我正在根据此处的演示 activity 编写 android 应用程序:DemoActivity.java

我希望能够翻转 android 设备的屏幕(纵向到横向,反之亦然)并保持用户登录和播放音乐。目前我可以让用户保持登录状态,但我对如何保存播放状态感到困惑。

在我的 onCreate 中:

if(savedInstanceState != null) {
        String accessToken = savedInstanceState.getString("accesstoken");
        Config player_Config = new Config(this, accessToken, CLIENT_ID);
        setPlayer(player_Config);
        mCurrentPlaybackState = savedInstanceState.getParcelable("playbackstate");
    }

我的 onSaveInstanceState:

@Override
public void onSaveInstanceState(Bundle savedInstanceState) {
    savedInstanceState.putString("accesstoken", accessToken);
    savedInstanceState.putParcelable("playbackstate", mCurrentPlaybackState);
    super.onSaveInstanceState(savedInstanceState);
}

我的 setPlayer(不包括身份验证,因为用户已经登录):

    public void setPlayer(Config player_Config) {
    Spotify.getPlayer(player_Config, this, new SpotifyPlayer.InitializationObserver() {

        @Override
        public void onInitialized(SpotifyPlayer spotifyPlayer) {
            myPlayer = spotifyPlayer;
            myPlayer.addConnectionStateCallback(HostPartyActivity.this);
            myPlayer.addNotificationCallback(HostPartyActivity.this);
            updateUIElements();
        }

        @Override
        public void onError(Throwable throwable) {
            Log.e("HostPartyActivity", "Could not initialize player: " + throwable.getMessage());
        }
    });
}

我的问题示例情况:

感谢您的帮助。

经过一些研究,我想我已经弄清楚了如何保存必要的播放状态信息。 在我的 onCreate:

if(savedInstanceState != null) { //restore instance
        accessToken = savedInstanceState.getString("accesstoken");
        if(accessToken != null) { //user was logged in
            Config player_Config = new Config(this, accessToken, CLIENT_ID);
            setPlayer(player_Config);
            /*
            * player is destroyed when new activity is started, so I need to get a new authentication code
            * for the new activity's player
            */

            myCurrentPlaybackState = savedInstanceState.getParcelable("playbackstate");
            myMetadata = savedInstanceState.getParcelable("metadata");
            if(myCurrentPlaybackState != null && myMetadata != null) { //user has played a song
                myPlayer.playUri(myMetadata.currentTrack.uri, (int)myMetadata.currentTrack.indexInContext, (int)myCurrentPlaybackState.positionMs);
            }
        }
    }

我的 onSaveInstanceState:

@Override
public void onSaveInstanceState(Bundle savedInstanceState) {
    savedInstanceState.putString("accesstoken", accessToken);
    if(myCurrentPlaybackState != null && myMetadata != null) {
        savedInstanceState.putParcelable("playbackstate", myCurrentPlaybackState);
        savedInstanceState.putParcelable("metadata", myMetadata);
    }
    super.onSaveInstanceState(savedInstanceState);
}

我的设置播放器:

    public void setPlayer(Config player_Config) {
    myPlayer = Spotify.getPlayer(player_Config, this, new SpotifyPlayer.InitializationObserver() {

        @Override
        public void onInitialized(SpotifyPlayer spotifyPlayer) {
            myPlayer.addConnectionStateCallback(HostPartyActivity.this);
            myPlayer.addNotificationCallback(HostPartyActivity.this);
            updateUIElements();
        }

        @Override
        public void onError(Throwable throwable) {
            Log.e("HostPartyActivity", "Could not initialize player: " + throwable.getMessage());
        }
    });
}

使用此代码,通过翻转我的设备生成的新 activity 可以访问之前 activity 的播放状态和元数据信息。但是,音乐曲目仍然不会在新的 activity 中恢复播放,因为当旧的 activity 被摧毁时,它也会摧毁播放器:

    @Override
protected void onDestroy() {
    Spotify.destroyPlayer(this);
    super.onDestroy();
}

我无法重新访问这个经过身份验证的播放器,即使我使用与创建原始播放器相同的访问令牌创建一个新播放器(错误:无法初始化播放器: 播放器已经关闭)。如何实现这一点是一个单独的问题。