public void onPlayButtonClicked(View view) { if (view.getId() == R.id.play_album_button) { logStatus("Starting playback the list of tracks"); mPlayer.play(TEST_ALBUM_TRACKS); } else { String uri; switch (view.getId()) { case R.id.play_track_button: uri = TEST_SONG_URI; break; case R.id.play_mono_track_button: uri = TEST_SONG_MONO_URI; break; case R.id.play_48khz_track_button: uri = TEST_SONG_48kHz_URI; break; case R.id.play_playlist_button: uri = TEST_PLAYLIST_URI; break; default: throw new IllegalArgumentException("View ID does not have an associated URI to play"); } logStatus("Starting playback for " + uri); mPlayer.play(uri); } }
public void onPauseButtonClicked(View view) { if (mCurrentPlayerState.playing) { mPlayer.pause(); } else { mPlayer.resume(); } }
@Override protected void onResume() { super.onResume(); // Set up the broadcast receiver for network events. Note that we also unregister // this receiver again in onPause(). mNetworkStateReceiver = new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { if (mPlayer != null) { Connectivity connectivity = getNetworkConnectivity(getBaseContext()); logStatus("Network state changed: " + connectivity.toString()); mPlayer.setConnectivityStatus(connectivity); } } }; IntentFilter filter = new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION); registerReceiver(mNetworkStateReceiver, filter); if (mPlayer != null) { mPlayer.addPlayerNotificationCallback(DemoActivity.this); mPlayer.addConnectionStateCallback(DemoActivity.this); } }
@Override public void onClick(View view) { if (mPlayer != null && mPlayer.isInitialized() && mPlayer.isLoggedIn()) { if (mPlayerStatusTask.getStatus().equals(AsyncTask.Status.PENDING)) { mPlayerStatusTask.execute(); } mPlayer.getPlayerState(playPauseButtonPressedPlayerStateCallback); } }
@Override protected void onPause() { super.onPause(); unregisterReceiver(mNetworkStateReceiver); // Note that calling Spotify.destroyPlayer() will also remove any callbacks on whatever // instance was passed as the refcounted owner. So in the case of this particular example, // it's not strictly necessary to call these methods, however it is generally good practice // and also will prevent your application from doing extra work in the background when // paused. if (mPlayer != null) { mPlayer.removePlayerNotificationCallback(DemoActivity.this); mPlayer.removeConnectionStateCallback(DemoActivity.this); } }
private void onAuthenticationComplete(AuthenticationResponse authResponse) { // Once we have obtained an authorization token, we can proceed with creating a Player. logStatus("Got authentication token"); if (mPlayer == null) { Config playerConfig = new Config(getApplicationContext(), authResponse.getAccessToken(), CLIENT_ID); // Since the Player is a static singleton owned by the Spotify class, we pass "this" as // the second argument in order to refcount it properly. Note that the method // Spotify.destroyPlayer() also takes an Object argument, which must be the same as the // one passed in here. If you pass different instances to Spotify.getPlayer() and // Spotify.destroyPlayer(), that will definitely result in resource leaks. mPlayer = Spotify.getPlayer( playerConfig, this, new Player.InitializationObserver() { @Override public void onInitialized(Player player) { logStatus("-- Player initialized --"); mPlayer.setConnectivityStatus(getNetworkConnectivity(DemoActivity.this)); mPlayer.addPlayerNotificationCallback(DemoActivity.this); mPlayer.addConnectionStateCallback(DemoActivity.this); // Trigger UI refresh updateButtons(); } @Override public void onError(Throwable error) { logStatus("Error in initialization: " + error.getMessage()); } }); } else { mPlayer.login(authResponse.getAccessToken()); } }
public void onLoginButtonClicked(View view) { if (!isLoggedIn()) { logStatus("Logging in"); openLoginWindow(); } else { mPlayer.logout(); } }
void updateStatusBar() { try { mPlayer.getPlayerState(updateStatusBarPlayerStateCallback); } catch (Exception e) { e.printStackTrace(); mPlayerStatusTask.cancel(true); } }
@Override protected void onResume() { Log.d("DEBUG_JRM", "ONRESUME"); super.onResume(); slidingUpPanelLayout.setPanelState(SlidingUpPanelLayout.PanelState.COLLAPSED); try { if (mPlayer.isInitialized()) { mPlayer.getPlayerState(setPlayPauseButtonIconPlayerStateCallback); Log.d("DEBUG_JRM", "RESUME PLAYER INIT"); if (mPlayerStatusTask.getStatus().equals(AsyncTask.Status.PENDING)) { Log.d("DEBUG_JRM", "RESUME STAT PENDING"); mPlayerStatusTask.execute(); } } } catch (NullPointerException e) { } }
@Override public void onPlayerState(PlayerState playerState) { /* Determine Plater State and play, Pause, or resume track */ SharedPreferences playerPreferences = getSharedPreferences(PLAYER_PREFS, MODE_PRIVATE); if (playerState.playing) { mPlayer.pause(); playPauseFloatingActionButton.setImageBitmap( BitmapFactory.decodeResource(getResources(), R.mipmap.ic_play_arrow_white)); } else if (playerState.trackUri != null && !playerState.trackUri.equals("")) { mPlayer.resume(); playPauseFloatingActionButton.setImageBitmap( BitmapFactory.decodeResource(getResources(), R.mipmap.ic_pause_white)); } else { /* TODO: Implement an solution for storing */ String trackUri = "spotify:track:6QPKYGnAW9QozVz2dSWqRg"; mPlayer.play(trackUri); playPauseFloatingActionButton.setImageBitmap( BitmapFactory.decodeResource(getResources(), R.mipmap.ic_pause_white)); } }
public void onShowPlayerStateButtonClicked(View view) { mPlayer.getPlayerState( new PlayerStateCallback() { @Override public void onPlayerState(PlayerState playerState) { logStatus("-- Current player state --"); logStatus("Playing? " + playerState.playing); logStatus("Position: " + playerState.positionInMs + "ms"); logStatus("Shuffling? " + playerState.shuffling); logStatus("Repeating? " + playerState.repeating); logStatus("Active device? " + playerState.activeDevice); logStatus("Track uri: " + playerState.trackUri); logStatus("Track duration: " + playerState.durationInMs); } }); }
public void onToggleShuffleButtonClicked(View view) { mPlayer.setShuffle(!mCurrentPlayerState.shuffling); }
private boolean isLoggedIn() { return mPlayer != null && mPlayer.isLoggedIn(); }
public void onSkipToPreviousButtonClicked(View view) { mPlayer.skipToPrevious(); }
public void onHighBitrateButtonPressed(View view) { mPlayer.setPlaybackBitrate(PlaybackBitrate.BITRATE_HIGH); }
public void onSkipToNextButtonClicked(View view) { mPlayer.skipToNext(); }
public void onNormalBitrateButtonPressed(View view) { mPlayer.setPlaybackBitrate(PlaybackBitrate.BITRATE_NORMAL); }
public void onLowBitrateButtonPressed(View view) { mPlayer.setPlaybackBitrate(PlaybackBitrate.BITRATE_LOW); }
public void onSeekButtonClicked(View view) { // Skip to 10 seconds in the current song mPlayer.seekToPosition(10000); }
public void onToggleRepeatButtonClicked(View view) { mPlayer.setRepeat(!mCurrentPlayerState.repeating); }
public void onQueueSongButtonClicked(View view) { mPlayer.queue(TEST_QUEUE_SONG_URI); Toast toast = Toast.makeText(this, R.string.song_queued_toast, Toast.LENGTH_SHORT); toast.show(); }