0

을 사용하여 개발중인 Podcast 앱에서 미디어 스타일 알림을 사용할 수 있습니까? 미디어 스타일 알림 예 https://www.binpress.com/tutorial/using-android-media-style-notifications-with-media-session-controls/165을 모두 통과 했으므로 Media Player를 사용하고 있습니다. 내 질문에 Exoplayer를 사용할 수 있습니다.Exoplayer

Exoplayer 앱에서도 동일한 작업을 수행하고 싶습니다. 어떤 아이디어? 여기

내가 지금 뭐하는 거지 무엇

public class ExoPlayerService extends Service { 

private static final String TAG = ExoPlayerService.class.getSimpleName(); 
private final IBinder mIBinder = new LocalBinder(); 

private Handler mHandler = new Handler(); 
private SimpleExoPlayer exoPlayer = null; 
//MyPlayerListener playerListener; 
PlayerListener playerListener; 
private boolean isPlayerInstantiated = false; 

public ExoPlayerService() { 
    super(); 
} 

public void setListener(PlayerListener listener) { 
    this.playerListener = listener; 
    if(!isPlayerInstantiated){ 
     isPlayerInstantiated = true; 
     listener.onPlayerInstatiated(exoPlayer); 
    } 
} 


public interface PlayerListener{ 
    void releasePlayer(SimpleExoPlayer exoPlayer); 
    void onPlayerInstatiated(SimpleExoPlayer exoPlayer); 
} 

public class LocalBinder extends Binder { 
    public ExoPlayerService getInstance() { 
     // Return this instance of LocalService so clients can call public methods 
     return ExoPlayerService.this; 
    } 
} 

@Override 
public int onStartCommand(Intent intent, int flags, int startId) { 
    if (intent.getAction().equals(Constants.ACTION.STARTFOREGROUND_ACTION)) { 
     createNotification(); 
     Toast.makeText(this, "Service Started", Toast.LENGTH_SHORT).show(); 
    } else if (intent.getAction().equals(Constants.ACTION.PREV_ACTION)) { 
     Toast.makeText(this, "Clicked Previous", Toast.LENGTH_SHORT).show(); 
     Log.i(TAG, "Clicked Previous"); 
    } else if (intent.getAction().equals(Constants.ACTION.PLAY_ACTION)) { 
     Toast.makeText(this, "Clicked Play", Toast.LENGTH_SHORT).show(); 
     Log.i(TAG, "Clicked Play"); 
    } else if (intent.getAction().equals(Constants.ACTION.NEXT_ACTION)) { 
     Toast.makeText(this, "Clicked Next", Toast.LENGTH_SHORT).show(); 
     Log.i(TAG, "Clicked Next"); 
    } else if (intent.getAction().equals(
      Constants.ACTION.STOPFOREGROUND_ACTION)) { 
     Log.i(TAG, "Received Stop Foreground Intent"); 
     Toast.makeText(this, "Service Stoped", Toast.LENGTH_SHORT).show(); 
     stopForeground(true); 
     stopSelf(); 
    } 
    return START_STICKY; 
} 


@Override 
public void onCreate() { 
    Log.d(TAG, "Service on create calledddd"); 
    super.onCreate(); 
    BandwidthMeter bandwidthMeter = new DefaultBandwidthMeter(); 
    TrackSelection.Factory videoTrackSelectionFactory = 
      new AdaptiveVideoTrackSelection.Factory(bandwidthMeter); 
    TrackSelector trackSelector = 
      new DefaultTrackSelector(mHandler, videoTrackSelectionFactory); 

    // 2. Create a default LoadControl 
    LoadControl loadControl = new DefaultLoadControl(); 

    // 3. Create the exoPlayer 
    exoPlayer = ExoPlayerFactory.newSimpleInstance(getApplicationContext(), trackSelector, loadControl); 
    Log.d(TAG, "EXO PLAYER CREATED IN SERVICE "); 
    if(playerListener != null){ 
     isPlayerInstantiated = true; 
     playerListener.onPlayerInstatiated(exoPlayer); 
    }else{ 
     isPlayerInstantiated = false; 
    } 
} 


public void setHandler(Handler handler) 
{ 
    mHandler = handler; 
} 


private NotificationCompat.Builder createNotification() { 

    // Using RemoteViews to bind custom layouts into Notification 
    RemoteViews views = new RemoteViews(getPackageName(), R.layout.status_bar); 
    RemoteViews bigViews = new RemoteViews(getPackageName(), R.layout.status_bar_expanded); 

    // showing default album image 
    views.setViewVisibility(R.id.status_bar_icon, View.VISIBLE); 
    views.setViewVisibility(R.id.status_bar_album_art, View.GONE); 
    bigViews.setImageViewBitmap(R.id.status_bar_album_art, Constants.getDefaultAlbumArt(this)); 
    views.setImageViewResource(R.id.status_bar_play, R.drawable.apollo_holo_dark_pause); 
    bigViews.setImageViewResource(R.id.status_bar_play, R.drawable.apollo_holo_dark_pause); 

    Intent previousIntent = new Intent(this, ExoPlayerService.class); 
    previousIntent.setAction(Constants.ACTION.PREV_ACTION); 
    PendingIntent ppreviousIntent = PendingIntent.getService(this, 0, 
      previousIntent, 0); 

    Intent playIntent = new Intent(this, ExoPlayerService.class); 
    playIntent.setAction(Constants.ACTION.PLAY_ACTION); 
    PendingIntent pplayIntent = PendingIntent.getService(this, 0, 
      playIntent, 0); 

    Intent nextIntent = new Intent(this, ExoPlayerService.class); 
    nextIntent.setAction(Constants.ACTION.NEXT_ACTION); 
    PendingIntent pnextIntent = PendingIntent.getService(this, 0, 
      nextIntent, 0); 

    Intent closeIntent = new Intent(this, ExoPlayerService.class); 
    closeIntent.setAction(Constants.ACTION.STOPFOREGROUND_ACTION); 
    PendingIntent pcloseIntent = PendingIntent.getService(this, 0, 
      closeIntent, 0); 


    views.setOnClickPendingIntent(R.id.status_bar_play, pplayIntent); 
    bigViews.setOnClickPendingIntent(R.id.status_bar_play, pplayIntent); 

    views.setOnClickPendingIntent(R.id.status_bar_next, pnextIntent); 
    bigViews.setOnClickPendingIntent(R.id.status_bar_next, pnextIntent); 

    views.setOnClickPendingIntent(R.id.status_bar_prev, ppreviousIntent); 
    bigViews.setOnClickPendingIntent(R.id.status_bar_prev, ppreviousIntent); 

    views.setOnClickPendingIntent(R.id.status_bar_collapse, pcloseIntent); 
    bigViews.setOnClickPendingIntent(R.id.status_bar_collapse, pcloseIntent); 

    views.setImageViewResource(R.id.status_bar_play, R.drawable.apollo_holo_dark_pause); 
    bigViews.setImageViewResource(R.id.status_bar_play, R.drawable.apollo_holo_dark_pause); 

    views.setTextViewText(R.id.status_bar_track_name, "Song Title"); 
    bigViews.setTextViewText(R.id.status_bar_track_name, "Song Title"); 
    views.setTextColor(R.id.status_bar_track_name, getResources().getColor(R.color.black)); 
    bigViews.setTextColor(R.id.status_bar_track_name, getResources().getColor(R.color.black)); 

    views.setTextViewText(R.id.status_bar_artist_name, "Artist Name"); 
    bigViews.setTextViewText(R.id.status_bar_artist_name, "Artist Name"); 
    views.setTextColor(R.id.status_bar_artist_name, getResources().getColor(R.color.black)); 
    bigViews.setTextColor(R.id.status_bar_artist_name, getResources().getColor(R.color.black)); 

    bigViews.setTextViewText(R.id.status_bar_album_name, "Album Name"); 
    bigViews.setTextColor(R.id.status_bar_album_name, getResources().getColor(R.color.black)); 

    Intent intent = new Intent(this, AudioActivity.class); 
    //  intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP); 

    // The stack builder object will contain an artificial back stack for the 
     // started Activity. 
    // This ensures that navigating backward from the Activity leads out of 
    // your application to the Home screen. 
    TaskStackBuilder stackBuilder = TaskStackBuilder.create(this); 
    // Adds the back stack for the Intent (but not the Intent itself) 
    stackBuilder.addParentStack(AudioActivity.class); 
    // Adds the Intent that starts the Activity to the top of the stack 
    stackBuilder.addNextIntent(intent); 

    PendingIntent resultPendingIntent = 
      stackBuilder.getPendingIntent(
        0, 
        PendingIntent.FLAG_UPDATE_CURRENT 
      ); 

    NotificationCompat.Builder status = 
      new NotificationCompat.Builder(getApplicationContext()); 
    status.setContent(views); 
    status.setContentIntent(resultPendingIntent); 
    status.setCustomBigContentView(bigViews); 
    status.setSmallIcon(R.drawable.ic_launcher); 
    // status.flags |= Notification.FLAG_NO_CLEAR; 
    /*status.contentView = views; 
    status.bigContentView = bigViews; 
    status.flags = Notification.FLAG_ONGOING_EVENT; 
    status.icon = R.drawable.ic_launcher; 
    status.contentIntent = pendIntent; 

    status.flags |= Notification.FLAG_NO_CLEAR;*/ 
    startForeground(Constants.NOTIFICATION_ID.FOREGROUND_SERVICE, status.build()); 
    return status; 
} 

@Override 
public void onDestroy() 
{ 
    Log.d(TAG, "Service on destroy called !!!!!!!!!!!!"); 
    /*Set this to false as the player unbinds from the service on being destroyed 
    this allows for a new instance of the player to be instantiated again */ 

    isPlayerInstantiated = false; 

    if(mHandler != null) 
    { 
     mHandler = null; 
    } 

    if(playerListener != null) 
     playerListener.releasePlayer(exoPlayer); 
    exoPlayer = null; 
} 

@Override 
public IBinder onBind(Intent intent) 
{ 
    return mIBinder; 
} 

@Override 
public boolean onUnbind(Intent intent) { 

    /* Set isPlayerInstantiated = false, as this service does not get destroyed on unbinding, we want all the clients 
    * binding to it to go ahead and use already create exoplayer instance */ 
    isPlayerInstantiated = false; 
    return super.onUnbind(intent); 
} 

}

나는 배경 색상을 사용자 정의 할 수 있기 때문에 그것을 MediaStyle을 방법을하고 싶지 그것은 또한 훨씬 간단하게 구현이 느끼는 이유 스타일.

+0

알림 코드를 'MediaStyle' 알림으로 바꾸면 작동하지 않는다고 생각합니까? 지금까지 뭐 해봤 어? – ianhanniballake

+0

글쎄, 미디어 컨트롤러 핸들이 필요하다. 맨 위의 링크 된 예제를 확인한다. Exoplayer는 다른 구현입니다. 나는 어쨌든 그것을 시도 생각하고 있지만. 계속 게시 할 것입니다. –

+0

@ianhanniballake 나는 exoplayer와 미디어 세션을 togethar와 연결하려고합니다. 그러나 나는 장벽을 쳤다. exoplayer에 대한 내 자신의 customplaybackcontrolview를 구현했으며이보기는 exoplayer 타임 라인을 확인하여 탐색 등을 업데이트합니다. exoplayer 인스턴스, 어떤 제안과 함께보기를 제공하는 방법을 잘 모르겠습니다? –

답변

1

예, ExoPlayer에서 MediaStyle 알림을 사용할 수 있습니다. 사실, 당신은 포함 Best practices in media playback의 숫자를 구현해야합니다

  • 오디오 초점
  • BECOMING_NOISY 방송
  • MediaSession
  • MediaStyle을 통지
  • 전경 서비스 것을 포함
  • 서비스 수명주기
  • UI를 동기화 상태로 유지

ExoPlayer는 이러한 것들을 처리하지 않습니다.

+0

감사합니다. Ian이 작업을 시작했습니다. –