2014-10-21 48 views
7

我們正在將我們正在進行的播放通知遷移到Lollipop中引入的MediaStyle通知。 RemoteControlClient似乎不推薦使用,MediaStyle通知不處理媒體按鈕事件(例如暫停/通過遠程耳機播放)。MediaStyle通知不響應RemoteControl事件。

有沒有人得到這份工作? MediaSessionCallback中的任何事件都不會被調用。

這裏是媒體會話初始化方式

mSession = new MediaSessionCompat(this, TAG); 
    mSession.setCallback(new MediaSessionCallback()); 
    mSession.setFlags(MediaSessionCompat.FLAG_HANDLES_MEDIA_BUTTONS | MediaSessionCompat.FLAG_HANDLES_TRANSPORT_CONTROLS); 
    mSession.setPlaybackToLocal(AudioManager.STREAM_MUSIC); 
    mSession.setActive(true); 

這裏是元數據如何設置

MediaMetadataCompat.Builder metadataBuilder = new MediaMetadataCompat.Builder(); 
    metadataBuilder 
      .putLong(MediaMetadata.METADATA_KEY_DURATION, clip.getDuration()) 
      .putString(MediaMetadata.METADATA_KEY_MEDIA_ID, clip.getClipId()) 
      .putString(MediaMetadata.METADATA_KEY_TITLE, clip.getTitle()) 
      .putString(MediaMetadata.METADATA_KEY_ARTIST, clip.getSourceName()) 
      .putString(MediaMetadata.METADATA_KEY_ALBUM_ART_URI, clip.getImageUrl()) 
      .putLong(MediaMetadata.METADATA_KEY_DURATION, clip.getDuration()); 
    mSession.setMetadata(metadataBuilder.build()); 

最後,通知代碼:

 MediaSession mediaSession = (MediaSession) session.getMediaSession(); 
     Notification.Builder builder = 
       new Notification.Builder(c) 
         .setDefaults(0) 
         .setSmallIcon(R.drawable.ic_notif) 
         .setVisibility(NotificationCompat.VISIBILITY_PUBLIC) 
         .setContentTitle(clip.getTitle()) 
         .setContentText(clip.getSourceName()) 
         .setProgress((int)duration, (int)progress, false) 
         .setWhen(0) 
         .setContentIntent(pendingIntent); 

     if (playing) { 
      builder.addAction(R.drawable.ic_media_pause, c.getString(R.string.media_pause), 
        getPendingIntentForKeyCode(app.getApplicationContext(), KeyEvent.KEYCODE_MEDIA_PAUSE)); 
     } else { 
      builder.addAction(R.drawable.ic_media_play, c.getString(R.string.media_play), 
        getPendingIntentForKeyCode(app.getApplicationContext(), KeyEvent.KEYCODE_MEDIA_PLAY)); 
     } 
     builder.addAction(R.drawable.ic_media_next, c.getString(R.string.media_next), 
        getPendingIntentForKeyCode(app.getApplicationContext(), KeyEvent.KEYCODE_MEDIA_NEXT)); 

     builder.setStyle(new Notification.MediaStyle() 
       .setMediaSession(mediaSession.getSessionToken()) 
       .setShowActionsInCompactView(new int[] {1, 2}) 
       ) 
     ); 

     notification = builder.build(); 

回答

5

設置播放狀態在MediaSession中使用您支持的操作:

PlaybackState state = new PlaybackState.Builder() 
     .setActions(
       PlaybackState.ACTION_PLAY | PlaybackState.ACTION_PLAY_PAUSE | 
       PlaybackState.ACTION_PLAY_FROM_MEDIA_ID | PlaybackState.ACTION_PAUSE | 
       PlaybackState.ACTION_SKIP_TO_NEXT | PlaybackState.ACTION_SKIP_TO_PREVIOUS) 
     .setState(PlaybackState.STATE_PLAYING, position, speed, SystemClock.elapsedRealtime()) 
     .build(); 
mSession.setPlaybackState(state); 
+2

對於Compat庫,它應該是這樣的:PlaybackStateCompat.Builder stateBuilder = new PlaybackStateCompat.Builder(); stateBuilder.setActions(PlaybackState.ACTION_PLAY | PlaybackState.ACTION_PLAY_PAUSE | PlaybackState.ACTION_PLAY_FROM_MEDIA_ID | PlaybackState.ACTION_PAUSE | PlaybackState.ACTION_SKIP_TO_NEXT | PlaybackState.ACTION_SKIP_TO_PREVIOUS); stateBuilder.setState(PlaybackState.STATE_PLAYING,0,1); m_objMediaSession.setPlaybackState(stateBuilder.build()); – goRGon 2014-12-25 22:29:37

+1

^輕微更正,它會是'PlaybackStateCompat.ACTION_PLAY'等 – hypd09 2015-03-26 07:13:33