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()); } }
@Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); /* Test To See if The Result is coming from the login Activity */ if (requestCode == LOGIN_REQUEST_CODE) { AuthenticationResponse authenticationResponse = AuthenticationClient.getResponse(resultCode, data); /* If a token is returned, the user logged in and we can now Initialize the Player. */ if (authenticationResponse.getType() == AuthenticationResponse.Type.TOKEN) { /* Store Access Token in Shared Preferences */ SharedPreferences sharedPreferences = getSharedPreferences(SESSION_PREFS, MODE_PRIVATE); SharedPreferences.Editor editor = sharedPreferences.edit(); editor.putString(LAST_VALID_SESSION_TOKEN, authenticationResponse.getAccessToken()); editor.commit(); /* Initialize Player */ PlayerUtils.init(this, authenticationResponse.getAccessToken()); } /* If Login does not Return Token.... Try Again */ else { LoginManager.startLoginActivity(this, LOGIN_REQUEST_CODE); } } }
@Override protected void onActivityResult(int requestCode, int resultCode, Intent intent) { super.onActivityResult(requestCode, resultCode, intent); // 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); mPlayer = Spotify.getPlayer( playerConfig, this, new Player.InitializationObserver() { @Override public void onInitialized(Player player) { // Default song shoreline Log.w("Initialized", "Init"); mPlayer.addConnectionStateCallback(MainActivity.this); mPlayer.addPlayerNotificationCallback(MainActivity.this); mPlayer.play("spotify:track:2nK30KDjrDHRedyIcHTOQS"); } @Override public void onError(Throwable throwable) { Log.e("MainActivity", "Could not initialize player: " + throwable.getMessage()); } }); } } }