简体   繁体   English

更新到Android 5.0后,媒体样式通知无法正常工作

[英]Media style notification not working after update to Android 5.0

I tried implementing the Media Style Notification using this link . 我尝试使用此链接实现Media Style Notification However when I recently updated to Android 5.0 SDK, the createSession method is not working. 但是,当我最近更新到Android 5.0 SDK时, createSession方法无效。

mMediaPlayer = new MediaPlayer();
mManager = (MediaSessionManager) getSystemService(Context.MEDIA_SESSION_SERVICE);
mSession = mManager.createSession("sample session"); //shows compile error
mController = MediaController.fromToken( mSession.getSessionToken() );

Is there any other method that needs to be used? 还有其他需要使用的方法吗? When I checked the change log, it said that createSession is no longer used. 当我检查更改日志时,它表示不再使用createSession What are the alternatives then for implementing such media style notifications. 那么实现这种媒体样式通知的替代方案是什么。

You no longer use MediaController.fromToken , but instead MediaSessin.getController . 您不再使用MediaController.fromToken ,而是使用MediaSessin.getController And you no longer use MediaSessionManager.createSession , but instead just create a new MediaSession object and MediaSession.getSessionToken to retrieve your MediaSession.Token . 并且您不再使用MediaSessionManager.createSession ,而只是创建一个新的MediaSession对象和MediaSession.getSessionToken来检索您的MediaSession.Token

A very basic example implementation might be something like: 一个非常基本的示例实现可能是这样的:

private static final String ACTION_TOGGLE_PLAYBACK = "com.your.package.name.TOGGLE_PLAYBACK";
private static final String ACTION_PREV = "com.your.package.name.PREV";
private static final String ACTION_NEXT = "com.your.package.name.NEXT";

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    ...

    final Bitmap artwork = ...;

    // Create a new MediaSession
    final MediaSession mediaSession = new MediaSession(this, "debug tag");
    // Update the current metadata
    mediaSession.setMetadata(new MediaMetadata.Builder()
            .putBitmap(MediaMetadata.METADATA_KEY_ALBUM_ART, artwork)
            .putString(MediaMetadata.METADATA_KEY_ARTIST, "Pink Floyd")
            .putString(MediaMetadata.METADATA_KEY_ALBUM, "Dark Side of the Moon")
            .putString(MediaMetadata.METADATA_KEY_TITLE, "The Great Gig in the Sky")
            .build());
    // Indicate you're ready to receive media commands
    mediaSession.setActive(true);
    // Attach a new Callback to receive MediaSession updates
    mediaSession.setCallback(new MediaSession.Callback() {

        // Implement your callbacks

    });
    // Indicate you want to receive transport controls via your Callback
    mediaSession.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(mediaSession.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();

    // Do something with your TransportControls
    final TransportControls controls = mediaSession.getController().getTransportControls();

    ((NotificationManager) getSystemService(NOTIFICATION_SERVICE)).notify(1, noti);
}

private PendingIntent retreivePlaybackAction(int which) {
    Intent action;
    PendingIntent pendingIntent;
    final ComponentName serviceName = new ComponentName(this, YourPlaybackService.class);
    switch (which) {
        case 1:
            // Play and pause
            action = new Intent(ACTION_TOGGLE_PLAYBACK);
            action.setComponent(serviceName);
            pendingIntent = PendingIntent.getService(this, 1, action, 0);
            return pendingIntent;
        case 2:
            // Skip tracks
            action = new Intent(ACTION_NEXT);
            action.setComponent(serviceName);
            pendingIntent = PendingIntent.getService(this, 2, action, 0);
            return pendingIntent;
        case 3:
            // Previous tracks
            action = new Intent(ACTION_PREV);
            action.setComponent(serviceName);
            pendingIntent = PendingIntent.getService(this, 3, action, 0);
            return pendingIntent;
        default:
            break;
    }
    return null;
}

Results 结果

例

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 媒体样式通知操作按钮不适用于 PendingIntent.getBroadcast() - Media Style Notification action buttons not working with PendingIntent.getBroadcast() android媒体播放器通知 - android media player notification Firebase 通知图标未在 Android 5.0 及更高版本上显示 - Firebase notification icon not shown on Android 5.0 and newer Java Android 媒体播放器(通知) - Java Android Media Player (Notification) ColorPrimary在android 5.0以下无法正常工作 - ?ColorPrimary not working below android 5.0 PlaceAutocompleteFragment无法在Android 5.0及更高版本上运行 - PlaceAutocompleteFragment not working on Android 5.0 & Up Android通知更新定期静默不起作用(处理程序问题?) - Android Notification Update Periodically Silently Not Working (Handler issue?) Android Spinner没有使用Android 5.0的三星设备 - Android Spinner not working on Samsung Devices with Android 5.0 当通知后应用程序未在堆栈上打开时,单击在 android 中不起作用 - when app is not open on stack after notification click not working in android 在设备上更新棒棒糖后,Android GCM无法正常工作 - Android GCM not working after Lollipop update on device
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM