如何在exoplayer中显示播放/暂停通知?

时间:2018-08-24 13:18:06

标签: android notifications exoplayer

我正在开发流应用程序,因此我正在使用exoplayer。这是代码,一切正常,但是我需要显示通知以及标题和播放/暂停按钮。此播放/暂停按钮必须像活动播放/暂停按钮一样工作,并且此播放/暂停按钮必须根据播放/暂停进行更改。

int (*ary)[20]

我尝试了很多方法。  Music player control in notification

How to put media controller button on notification bar?

https://github.com/yalematta/ExoPlayback

但不起作用。因此,有什么可以帮我怎么做的,非常感谢您的建议。

1 个答案:

答案 0 :(得分:0)

基本上,ExoPlayer为开发人员提供了一种方法来告知系统“我想在通知区域显示播放器”。那就是PlayerNotificationManager。首先,您需要在AndroidManifest.xml

中添加此声明
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />

然后,您可以实现以下代码:

private ExoPlayer exoPlayer;
private PlayerNotificationManager playerNotificationManager;
private int notificationId = 1234;
private PlayerNotificationManager.MediaDescriptionAdapter mediaDescriptionAdapter = new PlayerNotificationManager.MediaDescriptionAdapter() {
    @Override
    public String getCurrentSubText(Player player) {
        return "Sub text";
    }

    @Override
    public String getCurrentContentTitle(Player player) {
        return "Title";
    }

    @Override
    public PendingIntent createCurrentContentIntent(Player player) {
        return null;
    }

    @Override
    public String getCurrentContentText(Player player) {
        return "ContentText";
    }

    @Override
    public Bitmap getCurrentLargeIcon(Player player, PlayerNotificationManager.BitmapCallback callback) {
        return null;
    }
};

@Override
public void onCreate() {
    super.onCreate();
    BandwidthMeter bandwidthMeter = new DefaultBandwidthMeter();
    TrackSelection.Factory videoTrackSelectionFactory = new AdaptiveTrackSelection.Factory(bandwidthMeter);
    TrackSelector trackSelector = new DefaultTrackSelector(videoTrackSelectionFactory);
    exoPlayer = ExoPlayerFactory.newSimpleInstance(this, trackSelector);
    exoPlayer.setPlayWhenReady(true);
    playerNotificationManager = PlayerNotificationManager.createWithNotificationChannel(this, "My_channel_id", R.string.channel_name, notificationId, mediaDescriptionAdapter, new PlayerNotificationManager.NotificationListener() {
        @Override
        public void onNotificationPosted(int notificationId, Notification notification, boolean ongoing) {
        }

        @Override
        public void onNotificationCancelled(int notificationId, boolean dismissedByUser) {
        }
    });
    playerNotificationManager.setPlayer(exoPlayer);
}

@Override
public void onDestroy() {
    super.onDestroy();
    if (playerNotificationManager != null) {
        playerNotificationManager.setPlayer(null);
    }
    if (exoPlayer != null) {
        exoPlayer.release();
        exoPlayer = null;
    }
}