2014-10-21 25 views
7

Chúng tôi đang trong quá trình di chuyển thông báo phát lại liên tục của mình sang các thông báo MediaStyle được giới thiệu trong Lollipop. RemoteControlClient dường như không được chấp nhận và thông báo MediaStyle không xử lý các sự kiện nút phương tiện (chẳng hạn như tạm dừng/phát qua tai nghe từ xa).Thông báo MediaStyle không phản hồi các sự kiện RemoteControl.

Có ai có được tác phẩm này không? Không có sự kiện nào trong MediaSessionCallback được gọi.

Sau đây là cách các phiên truyền thông được khởi

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); 

Sau đây là cách siêu dữ liệu được được thiết lập

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()); 

Cuối cùng, các mã thông báo:

 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(); 

Trả lời

5

Đặt tình trạng phát lại trong MediaSession của bạn với các hành động mà bạn hỗ trợ:

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

Đối với thư viện Compat, nó phải giống như sau: 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

+1

^chỉnh sửa nhẹ, nó sẽ là 'PlaybackStateCompat.ACTION_PLAY' v.v. – hypd09

Các vấn đề liên quan