我正在开发流应用程序,因此我正在使用exoplayer。这是代码,一切正常,但是我需要显示通知以及标题和播放/暂停按钮。此播放/暂停按钮必须像活动播放/暂停按钮一样工作,并且此播放/暂停按钮必须根据播放/暂停进行更改。
int (*ary)[20]
我尝试了很多方法。 Music player control in notification
How to put media controller button on notification bar?
https://github.com/yalematta/ExoPlayback
但不起作用。因此,有什么可以帮我怎么做的,非常感谢您的建议。
答案 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;
}
}