我正在尝试使用我的Android应用程序连接Spotify。我希望能够点击按钮并让用户能够在TabLayout的第4个选项卡上从他们的库开始播放,并且能够在没有音乐停止的情况下从一个选项卡来回滑动。这是我目前的观点:
当我点击开始播放时,我只是将它连接到SpotifyActivity.java,我已经登录并开始播放歌曲。但是,我希望拥有它,以便在我想开始访问我的库并播放音乐时不必调用新活动,以便我可以在标签之间切换并使用应用程序的其余功能播放。我最近了解了Android中的服务,我想知道是否有办法使用它。我基本上希望流程是这样的:
第4个标签显示“开始播放”按钮。一旦命中,您就会登录到Spotify(或者如果您已经登录则继续),并弹出一个新视图(活动?),使Spotify和UI正常工作。一旦音乐开始,我可以切换回前三个标签中的任何一个并在音乐播放时与那些标签进行交互,然后当我回到第一个标签时,活动再次弹出音乐播放器UI和Spotify的内容。这是我的PageFragment代码:
public class PageFragment extends Fragment {
public static final String ARG_PAGE = "ARG_PAGE";
private static User thisUser;
private int mPage;
public static PageFragment newInstance(int page, User localUser) {
Bundle args = new Bundle();
thisUser = localUser;
args.putInt(ARG_PAGE, page);
PageFragment fragment = new PageFragment();
fragment.setArguments(args);
return fragment;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mPage = getArguments().getInt(ARG_PAGE);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = null;
TextView textView;
if (mPage == 1) {
//inflate the fragment_home_page
v = inflater.inflate(R.layout.fragment_home_page, container, false);
//handle profile and username information
}
else if (mPage == 2) {
//inflate the fragment_play_page
v = inflater.inflate(R.layout.fragment_play_page, container, false);
//handle music library information
}
else if(mPage == 3) {
//inflate the fragment_live_page
v = inflater.inflate(R.layout.fragment_live_page, container, false);
//handle live information
}
else if (mPage == 4) {
//inflate the fragment_nowplaying_page
v = inflater.inflate(R.layout.fragment_nowplaying_page, container, false);
Button startBroadcast = (Button) v.findViewById(R.id.start_broadcast);
//Links to SpotifyActivity
startBroadcast.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(getActivity(), SpotifyActivity.class);
startActivity(intent);
}
});
}
else {
Log.e("PageFragment ", "Unknown error occurred");
}
return v;
}
}
以下是我的SpotifyActivity代码:
public class SpotifyActivity extends AppCompatActivity implements SpotifyPlayer.NotificationCallback, ConnectionStateCallback {
//CLIENT_ID=
//Callback
// Request code that will be used to verify if the result comes from correct activity
// Can be any integer
private static final int REQUEST_CODE = 1337;
private Player mPlayer;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_spotify);
AuthenticationRequest.Builder builder = new AuthenticationRequest.Builder(CLIENT_ID,
AuthenticationResponse.Type.TOKEN,
REDIRECT_URI);
builder.setScopes(new String[]{"user-read-private", "streaming"});
AuthenticationRequest request = builder.build();
AuthenticationClient.openLoginActivity(this, REQUEST_CODE, request);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
super.onActivityResult(requestCode, resultCode, intent);
//After the user logs in to authorize the application's scopes
// Check if result comes from the correct activity
if (requestCode == REQUEST_CODE) {
AuthenticationResponse response = AuthenticationClient.getResponse(resultCode, intent);
if (response.getType() == AuthenticationResponse.Type.TOKEN) {
Config playerConfig = new Config(this, response.getAccessToken(), CLIENT_ID);
Spotify.getPlayer(playerConfig, this, new SpotifyPlayer.InitializationObserver() {
@Override
public void onInitialized(SpotifyPlayer spotifyPlayer) {
mPlayer = spotifyPlayer;
mPlayer.addConnectionStateCallback(SpotifyActivity.this);
mPlayer.addNotificationCallback(SpotifyActivity.this);
}
@Override
public void onError(Throwable throwable) {
Log.e("MainActivity", "Could not initialize player: " + throwable.getMessage());
}
});
}
}
}
@Override
protected void onDestroy() {
Spotify.destroyPlayer(this);
super.onDestroy();
}
@Override
public void onPlaybackEvent(PlayerEvent playerEvent) {
Log.d("MainActivity", "Playback event received: " + playerEvent.name());
switch (playerEvent) {
// Handle event type as necessary
default:
break;
}
}
@Override
public void onPlaybackError(Error error) {
Log.d("MainActivity", "Playback error received: " + error.name());
switch (error) {
// Handle error type as necessary
default:
break;
}
}
@Override
public void onLoggedIn() {
Log.d("MainActivity", "User logged in");
mPlayer.playUri(null, "spotify:track:2TpxZ7JUBn3uw46aR7qd6V", 0, 0);
}
@Override
public void onLoggedOut() {
Log.d("MainActivity", "User logged out");
}
@Override
public void onLoginFailed(Error error) {
Log.d("MainActivity", "Login failed");
}
@Override
public void onTemporaryError() {
Log.d("MainActivity", "Temporary error occurred");
}
@Override
public void onConnectionMessage(String s) {
Log.d("MainActivity", "Received connection message: " + s);
}
}
当我切换到其他标签时,有没有办法设置Spotify的音乐播放器而不会丢失它?