2017-12-20 9 views
1

성공적인 인증시, 하드 코딩 된 트랙을 재생할 수 있지만, 플레이어도 아니며, 일시 중지, 중지, 재생 버튼도 아닙니다.안드로이드에 자리 잡는 방법 플레이어를 얻는 방법

나는 this 자습서를 따라 갔고 거기에 언급 된 모든 사항을 수행했습니다.

이제는 플레이어를 추가해야하지만 이에 관련된 문서를 찾을 수 없습니다. 플레이어 SDK가 다른 버튼 콜백을 제공하기 때문에 플레이어 레이아웃을 직접 만들어야한다고 생각하지만 플레이어에게 미리 정의 된 레이아웃을 사용하는 것과 같은 것은 없습니다.

이것은 성공적인 승인 후에 수행합니다. 당신은 당신이 당신이 트랙을 재생할 수 있다면 그것이 작동하는 것 같군 말을하는 튜토리얼을 따라하면

 Spotify.getPlayer(playerConfig, this, new SpotifyPlayer.InitializationObserver() { 
       @Override 
       public void onInitialized(SpotifyPlayer spotifyPlayer) { 
        mPlayer = spotifyPlayer; 
        mPlayer.addConnectionStateCallback(MainActivity.this); 
        mPlayer.addNotificationCallback(MainActivity.this); 
       } 


@Override 
public void onLoggedIn() { 
    Log.d("MainActivity", "User logged in"); 

    mPlayer.playUri(null, "spotify:track:2TpxZ7JUBn3uw46aR7qd6V", 0, 0); 
} 

답변

3

는, 당신은 SpotifyPlayer 참조가 있습니다. 그 getPlayer 방법은 당신에게 그것을 제공합니다.

SpotifyPlayer은 UI 구성 요소가 아닙니다. 해당 플레이어 (https://spotify.github.io/android-sdk/player/)를 가지고 나면 자신 만의 UI를 만들고 버튼 등을 플레이어 동작 (방법) 및 이벤트에 연결합니다.

public class PlayerActivity extends BaseActivity { 

private ImageView buttonPlayPause; 
private TextView labelNowPlaying; 

private RecyclerView trackListRecyclerView; 
private TrackListAdapter trackListAdapter; 
private RecyclerView.LayoutManager trackListLayoutManager; 

private Player spotifyPlayer; 
private ConnectionStateCallback spotifyConnectionStateCallback; 
private Player.NotificationCallback spotifyPlayerNotificationCallback; 
private Player.OperationCallback spotifyPlayerOperationCallback; 
private String spotifyAccessToken; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_player); 

    buttonPlayPause = (ImageView) findViewById(R.id.button_play_pause); 
    labelNowPlaying = (TextView) findViewById(R.id.label_now_playing); 

    buttonPlayPause.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      // toggle spot pause/play 
      if (spotifyPlayer == null) { 
       Toast.makeText(PlayerActivity.this, "Spotify player not ready", Toast.LENGTH_SHORT).show(); 
      } else { 
       // SpotifyPlayer provides init and connect methods; direct controls (play uri, pause, seek, skip, resume); and state (metadata and playbackstate) 
       PlaybackState playbackState = spotifyPlayer.getPlaybackState(); 
       Log.d(Constants.TAG, "playPause click, playbackState:" + playbackState); 
       Metadata metadata = spotifyPlayer.getMetadata(); 

       if (!playbackState.isPlaying && playbackState.positionMs == 0) { 
        // nothing has been started yet play track 1 
        Track track = trackListAdapter.getFirstTrack(); 
        labelNowPlaying.setText(track.getName()); 
        spotifyPlayer.playUri(null, track.getUri(), 0, 0); 
        return; 
       } 

       if (playbackState.isPlaying) { 
        spotifyPlayer.pause(spotifyPlayerOperationCallback); 
        return; 
       } 

       if (!playbackState.isPlaying && playbackState.positionMs > 0) { 
        // TODO how to tell if player is paused, idPlaying false and just position != 0? or is there an actual pause state, weird? 
        spotifyPlayer.resume(spotifyPlayerOperationCallback); 
        return; 
       } 

       // get here it's weird 
       Log.d(Constants.TAG, "error unexepected playback state:" + playbackState); 
       Toast.makeText(PlayerActivity.this, "Spotify playback state weird:" + playbackState, Toast.LENGTH_LONG).show();      
      } 
     } 
    }); 

    spotifyPlayerNotificationCallback = new Player.NotificationCallback() { 
     @Override 
     public void onPlaybackEvent(PlayerEvent playerEvent) { 
      Log.d(Constants.TAG, "Spotify player notif callback: playback event: " + playerEvent.name()); 
      handleSpotifyEvent(playerEvent); 
     } 

     @Override 
     public void onPlaybackError(Error error) { 
      Log.d(Constants.TAG, "Spotify player notif callback: playback error: " + error.name()); 
      handleSpotifyError(error); 
     } 
    }; 

    // recycler view 
    List<Track> playList = application.getPlaylist(); 
    trackListRecyclerView = (RecyclerView) findViewById(R.id.track_list_recycler); 
    trackListRecyclerView.setHasFixedSize(true); 
    trackListLayoutManager = new LinearLayoutManager(this); 
    trackListRecyclerView.setLayoutManager(trackListLayoutManager); 
    trackListAdapter = new TrackListAdapter(PlayerActivity.this, playList); 
    trackListRecyclerView.setAdapter(trackListAdapter); 

    // get track list click events as observable 
    trackListAdapter.asObservable().subscribe(new Action1<Track>() { 
     @Override 
     public void call(Track track) { 
      // TODO make other adapter list items not clickable until one is processed? 
      labelNowPlaying.setText(track.getName()); 
      spotifyPlayer.playUri(null, track.getUri(), 0, 0); 
     } 
    }); 

    // spotify player callback 
    spotifyConnectionStateCallback = new ConnectionStateCallback() { 

     @Override 
     public void onLoggedIn() { 
      Log.d(Constants.TAG, "Spotify connection callback: User logged in"); 
     } 

     @Override 
     public void onLoggedOut() { 
      Log.d(Constants.TAG, "Spotify connection callback: user logged out"); 
     } 

     @Override 
     public void onLoginFailed(Error e) { 
      Log.d(Constants.TAG, "Spotify connection callback: login failed: " + e.toString()); 
     } 

     @Override 
     public void onTemporaryError() { 
      Log.d(Constants.TAG, "Spotify connection callback: temp error occurred"); 
     } 

     @Override 
     public void onConnectionMessage(String message) { 
      Log.d(Constants.TAG, "Spotify connection callback: connection message: " + message); 
     } 
    }; 

    spotifyPlayerOperationCallback = new Player.OperationCallback() { 
     @Override 
     public void onSuccess() { 
      Log.d(Constants.TAG, "Spotify operation callback: success"); 
     } 

     @Override 
     public void onError(Error error) { 
      Log.d(Constants.TAG, "Spotify operation callback: error " + error.name()); 
      Toast.makeText(PlayerActivity.this, "Spotify op error: " + error.name(), Toast.LENGTH_SHORT).show(); 
     } 
    }; 

    spotifyAccessToken = application.getSpotifyAccessToken(); 
    initSpotifyPlayer(); 
} 

@Override 
protected void onDestroy() { 
    Spotify.destroyPlayer(this); 
    super.onDestroy(); 
} 

// 
// private 
// 

// 
// SPOT 
// 

private void initSpotifyPlayer() { 

    Log.d(Constants.TAG, "initSpotifyPlayer"); 

    if (spotifyAccessToken == null) { 
     Toast.makeText(this, "Spotify access token not present, cannot continue", Toast.LENGTH_LONG).show(); 
     return; 
    } 

    Config playerConfig = new Config(this, spotifyAccessToken, application.getSpotifyClientId()); 
    Spotify.getPlayer(playerConfig, this, new SpotifyPlayer.InitializationObserver() { 
     @Override 
     public void onInitialized(SpotifyPlayer player) { 
      spotifyPlayer = player; 
      spotifyPlayer.addConnectionStateCallback(spotifyConnectionStateCallback); 
      spotifyPlayer.addNotificationCallback(spotifyPlayerNotificationCallback); 
     } 

     @Override 
     public void onError(Throwable throwable) { 
      Log.e(Constants.TAG, "Could not initialize player: " + throwable.getMessage()); 
     } 
    }); 
} 

private void handleSpotifyEvent(final PlayerEvent playerEvent) { 
    Log.d(Constants.TAG, "Spotify playerEvent:" + playerEvent.name()); 

    switch (playerEvent) { 

     case kSpPlaybackEventAudioFlush: 
      break; 
     case kSpPlaybackNotifyAudioDeliveryDone: 
      break; 
     case kSpPlaybackNotifyBecameActive: 
      break; 
     case kSpPlaybackNotifyBecameInactive: 
      break; 
     case kSpPlaybackNotifyContextChanged: 
      break; 
     case kSpPlaybackNotifyLostPermission: 
      Toast.makeText(PlayerActivity.this, "Spotify player perms lost (logged in elsewhere?)", Toast.LENGTH_LONG).show(); 
      buttonPlayPause.setImageResource(android.R.drawable.ic_media_play); 
      startActivity(new Intent(PlayerActivity.this, MainActivity.class)); 
      break; 
     case kSpPlaybackNotifyMetadataChanged: 
      break; 
     case kSpPlaybackNotifyNext: 
      break; 
     case kSpPlaybackNotifyPause: 
      buttonPlayPause.setImageResource(android.R.drawable.ic_media_play);     
      break; 
     case kSpPlaybackNotifyPlay: 
      buttonPlayPause.setImageResource(android.R.drawable.ic_media_pause); 
      // TODO get current playing track here? 
      //labelNowPlaying.setText(spotifyPlayer.getMetadata().currentTrack. 
      break; 
     case kSpPlaybackNotifyPrev: 
      break; 
     case kSpPlaybackNotifyRepeatOff: 
      break; 
     case kSpPlaybackNotifyRepeatOn: 
      break; 
     case kSpPlaybackNotifyShuffleOff: 
      break; 
     case kSpPlaybackNotifyShuffleOn: 
      break; 
     case kSpPlaybackNotifyTrackChanged: 
      break; 
     case kSpPlaybackNotifyTrackDelivered: 
      break; 

     default: 
      break; 
    } 
} 

private void handleSpotifyError(final Error error) { 
    Log.e(Constants.TAG, "Spotify Error:" + error.name()); 

    switch (error) { 
     // corrupt track, add is playing, need perms, travel restriction, etc 
     default: 
      break; 
    } 
} 

} 
+0

이를 삭제 해 주셔서 너무 감사합니다 여기에

은 (완전하거나 완벽하게! 포괄적이지,하지만 시작하는 데 도움이 될 하나 개의 샘플 프로젝트에서 간단한 예되지 않음) 도움이 될 예입니다 위로 :) – Kirmani88