我正在编写一个基于演示活动的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());
}
});
}
我问题的示例情况:
感谢您的帮助。
答案 0 :(得分:0)
经过一番研究后,我想我已经研究出如何保存必要的播放状态信息。 在我的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);
}
我的setPlayer:
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());
}
});
}
使用此代码,通过翻转我的设备生成的新活动可以访问上一个活动的播放状态和元数据信息。然而,音乐曲目仍然不能继续在新活动中播放,因为当旧活动被破坏时,它也会摧毁玩家:
@Override
protected void onDestroy() {
Spotify.destroyPlayer(this);
super.onDestroy();
}
我无法访问此经过身份验证的播放器,即使我使用用于创建原始播放器的相同访问令牌创建新播放器(错误:无法初始化播放器:播放器已经关掉)。如何实现这是一个单独的问题。