4

我正在處理一些Android代碼,我不想構建MediaStyle通知。我已經在大多數媒體播放器和媒體會議中使用AppCompat,並且我還沒有使用過我的計劃,因此我可以保持4.x兼容性。無法使用支持v4 MediaSession令牌與通知MediaStyle.setMediaSession

問題?那麼,我正在嘗試製作MediaStyle通知,併爲其提供MediaSession標記。我的support.v4.media.session.MediaSession.Token似乎不符合media.session.MediaSession.Token

我試過鑄造,只是把它保留原始。我真的很困惑,因爲文檔說他們是兼容的。

如果你想在代碼的其餘部分中,code can be found here

或者你也可以在相關的代碼看這裏。

Intent nIntent = new Intent(context, MainActivity.class); 
    PendingIntent pIntent = PendingIntent.getActivity(context, 0, nIntent, 0); 

    n.setLatestEventInfo(context, notifTitle, notifMessage, pIntent); 

    notificationManager.notify(notifId, n); 

    ComponentName c = new ComponentName("com.thefan.android", "BackgroundService"); 
    ms = new MediaSessionCompat(this, "TheFan", c, pIntent); 
    ms.setMetadata(new MediaMetadataCompat.Builder() 
      .putBitmap(MediaMetadataCompat.METADATA_KEY_ALBUM_ART, artwork) 
      .putString(MediaMetadataCompat.METADATA_KEY_ARTIST, "Pink Floyd") 
      .putString(MediaMetadataCompat.METADATA_KEY_ALBUM, "Dark Side of the Moon") 
      .putString(MediaMetadataCompat.METADATA_KEY_TITLE, "The Great Gig in the Sky") 
      .build()); 
    // Indicate you're ready to receive media commands 
    ms.setActive(true); 
    // Attach a new Callback to receive MediaSession updates 
    ms.setCallback(new MediaSessionCompat.Callback() { 
     // Implement your callbacks 
    }); 
    // Indicate you want to receive transport controls via your Callback 
    ms.setFlags(MediaSession.FLAG_HANDLES_TRANSPORT_CONTROLS); 
    // Create a new Notification 
    final Notification noti = new Notification.Builder(this) 
      // Hide the timestamp 
      .setShowWhen(false) 
        // Set the Notification style 
      .setStyle(new Notification.MediaStyle() 
        // Attach our MediaSession token 
        .setMediaSession(ms.getSessionToken()) 
          // Show our playback controls in the compat view 
        .setShowActionsInCompactView(0, 1, 2)) 
        // Set the Notification color 
      .setColor(0xFFDB4437) 
        // Set the large and small icons 
      .setLargeIcon(artwork) 
      .setSmallIcon(R.drawable.your_small_icon) 
        // Set Notification content information 
      .setContentText("Pink Floyd") 
      .setContentInfo("Dark Side of the Moon") 
      .setContentTitle("The Great Gig in the Sky") 
        // Add some playback controls 
      .addAction(R.drawable.your_prev_icon, "prev", retreivePlaybackAction(3)) 
      .addAction(R.drawable.your_pause_icon, "pause", retreivePlaybackAction(1)) 
      .addAction(R.drawable.your_next_icon, "next", retreivePlaybackAction(2)) 
      .build(); 

回答

2

不可思議的。有一個Token.getToken();你需要使用它。

再次,MediaStyle通知只是API 21兼容,所以祝你好運。

+0

這就是答案。 'compat'標記實例帶有對'normal'標記實例的引用,你可以通過調用'getToken()'來獲得該標記實例。通過調用靜態MediaSessionCompate.Token.fromToken()方法也可以反轉。 – aroth

-1

這是可能的。

檢查您的導入,也許您導入的是不好的版本。

  • MediaStyle應該android.support.v7.app.NotificationCompat.MediaStyle
  • NotificationBuilder應該是android.support.v7.app.NotificationCompat.Builder
  • 通知應COMPAT android.support.v4.app.NotificationCompat

如果你要支持老版本比21,你需要使用COMPAT類(ALL COMPAT類而不是「正常」的)。

1

對於這可能有所幫助。

首先你需要進口V4 MediaSessionCompat和評論這樣一般MediaSession:

//進口android.media.session.MediaSession;

import android.support.v4.media.session.MediaSessionCompat;

在你的代碼,你需要使用MediaSessonCompat這樣的:

MediaSessionCompat mediaSession =新MediaSessionCompat(getApplicationContext(), 「會話標籤」);

MediaSessionCompat.Token token = mediaSession.getSessionToken();

mediaStyle.setMediaSession(token);