Example #1
0
  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);
    }
  }
Example #2
0
 public void onPauseButtonClicked(View view) {
   if (mCurrentPlayerState.playing) {
     mPlayer.pause();
   } else {
     mPlayer.resume();
   }
 }
Example #3
0
  @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);
    }
  }
Example #4
0
 @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);
   }
 }
Example #5
0
  @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);
    }
  }
Example #6
0
  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());
    }
  }
Example #7
0
 public void onLoginButtonClicked(View view) {
   if (!isLoggedIn()) {
     logStatus("Logging in");
     openLoginWindow();
   } else {
     mPlayer.logout();
   }
 }
Example #8
0
 void updateStatusBar() {
   try {
     mPlayer.getPlayerState(updateStatusBarPlayerStateCallback);
   } catch (Exception e) {
     e.printStackTrace();
     mPlayerStatusTask.cancel(true);
   }
 }
Example #9
0
  @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) {

    }
  }
Example #10
0
 @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));
   }
 }
Example #11
0
 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);
         }
       });
 }
Example #12
0
 public void onToggleShuffleButtonClicked(View view) {
   mPlayer.setShuffle(!mCurrentPlayerState.shuffling);
 }
Example #13
0
 private boolean isLoggedIn() {
   return mPlayer != null && mPlayer.isLoggedIn();
 }
Example #14
0
 public void onSkipToPreviousButtonClicked(View view) {
   mPlayer.skipToPrevious();
 }
Example #15
0
 public void onHighBitrateButtonPressed(View view) {
   mPlayer.setPlaybackBitrate(PlaybackBitrate.BITRATE_HIGH);
 }
Example #16
0
 public void onSkipToNextButtonClicked(View view) {
   mPlayer.skipToNext();
 }
Example #17
0
 public void onNormalBitrateButtonPressed(View view) {
   mPlayer.setPlaybackBitrate(PlaybackBitrate.BITRATE_NORMAL);
 }
Example #18
0
 public void onLowBitrateButtonPressed(View view) {
   mPlayer.setPlaybackBitrate(PlaybackBitrate.BITRATE_LOW);
 }
Example #19
0
 public void onSeekButtonClicked(View view) {
   // Skip to 10 seconds in the current song
   mPlayer.seekToPosition(10000);
 }
Example #20
0
 public void onToggleRepeatButtonClicked(View view) {
   mPlayer.setRepeat(!mCurrentPlayerState.repeating);
 }
Example #21
0
 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();
 }