PlayerNotificationManager 不显示在 exoplayer 上播放视频的通知,android?
PlayerNotificationManager not showing notification for playing video on exoplayer, android?
我的应用播放视频
我正在关注这个Exoplayer Notifications
有相同的代码
PlayerNotificationManager playerNotificationManager = new PlayerNotificationManager(
appCMSPresenter.getCurrentContext(),
"kingkdfn",
459, new DescriptionAdapter());
playerNotificationManager.setPlayer(getPlayer());
DescriptionAdapter
public class DescriptionAdapter implements
PlayerNotificationManager.MediaDescriptionAdapter {
@Override
public String getCurrentContentTitle(Player player) {
int window = player.getCurrentWindowIndex();
return "getTitle(window)";
}
@Nullable
@Override
public String getCurrentContentText(Player player) {
int window = player.getCurrentWindowIndex();
return "getDescription(window)";
}
@Nullable
@Override
public Bitmap getCurrentLargeIcon(Player player,
PlayerNotificationManager.BitmapCallback callback) {
return null;
}
@Nullable
@Override
public PendingIntent createCurrentContentIntent(Player player) {
return null;
}
}
全部添加到清单中
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
但是在播放视频时仍然没有显示通知。
我需要添加任何额外的东西吗?
创建通知
public void createNotification() {
if (notificationAdapter == null)
notificationAdapter = new PlayerNotificationAdapter();
if (pnm == null) {
pnm = new PlayerNotificationManager.Builder(mContext, 239, mContext.getString(R.string.player_notification_channel_id))
.setChannelNameResourceId(R.string.player_notification_channel_name)
.setChannelDescriptionResourceId(R.string.player_notification_channel_desc)
.setMediaDescriptionAdapter(notificationAdapter).build();
}
}
通知适配器
class PlayerNotificationAdapter : PlayerNotificationManager.MediaDescriptionAdapter {
override fun getCurrentContentTitle(player: Player): String {
return "Content Title"
}
override fun createCurrentContentIntent(player: Player): PendingIntent? {
return null
}
override fun getCurrentContentText(player: Player): String? {
return "Content Description"
}
override fun getCurrentLargeIcon(player: Player, callback: PlayerNotificationManager.BitmapCallback): Bitmap? {
return playerImage()
}
fun playerImage(): Bitmap? {
var image: Bitmap? = null
try {
var url = URL("content image url")
image = BitmapFactory.decodeStream(url.openConnection().getInputStream())
} catch (e: IOException) {
}
return image
}
}
将播放器设置为通知管理器
if (pnm != null) {
pnm.setUseNextAction(false);
pnm.setUsePreviousAction(false);
MediaSessionCompat mediaSession = new MediaSessionCompat(mContext, "Player");
mediaSession.setActive(true);
mediaSession.setMetadata(new MediaMetadataCompat.Builder()
.putString(MediaMetadataCompat.METADATA_KEY_TITLE, title)
.putString(MediaMetadataCompat.METADATA_KEY_DISPLAY_DESCRIPTION, description)
.build());
MediaSessionConnector mediaSessionConnector =
new MediaSessionConnector(mediaSession);
mediaSessionConnector.setPlayer(simpleExoPlayer);
pnm.setMediaSessionToken(mediaSession.getSessionToken());
pnm.setPlayer(simpleExoPlayer);
pnm.setPriority(PRIORITY_LOW);
}
我的应用播放视频
我正在关注这个Exoplayer Notifications
有相同的代码
PlayerNotificationManager playerNotificationManager = new PlayerNotificationManager(
appCMSPresenter.getCurrentContext(),
"kingkdfn",
459, new DescriptionAdapter());
playerNotificationManager.setPlayer(getPlayer());
DescriptionAdapter
public class DescriptionAdapter implements
PlayerNotificationManager.MediaDescriptionAdapter {
@Override
public String getCurrentContentTitle(Player player) {
int window = player.getCurrentWindowIndex();
return "getTitle(window)";
}
@Nullable
@Override
public String getCurrentContentText(Player player) {
int window = player.getCurrentWindowIndex();
return "getDescription(window)";
}
@Nullable
@Override
public Bitmap getCurrentLargeIcon(Player player,
PlayerNotificationManager.BitmapCallback callback) {
return null;
}
@Nullable
@Override
public PendingIntent createCurrentContentIntent(Player player) {
return null;
}
}
全部添加到清单中
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
但是在播放视频时仍然没有显示通知。 我需要添加任何额外的东西吗?
创建通知
public void createNotification() {
if (notificationAdapter == null)
notificationAdapter = new PlayerNotificationAdapter();
if (pnm == null) {
pnm = new PlayerNotificationManager.Builder(mContext, 239, mContext.getString(R.string.player_notification_channel_id))
.setChannelNameResourceId(R.string.player_notification_channel_name)
.setChannelDescriptionResourceId(R.string.player_notification_channel_desc)
.setMediaDescriptionAdapter(notificationAdapter).build();
}
}
通知适配器
class PlayerNotificationAdapter : PlayerNotificationManager.MediaDescriptionAdapter {
override fun getCurrentContentTitle(player: Player): String {
return "Content Title"
}
override fun createCurrentContentIntent(player: Player): PendingIntent? {
return null
}
override fun getCurrentContentText(player: Player): String? {
return "Content Description"
}
override fun getCurrentLargeIcon(player: Player, callback: PlayerNotificationManager.BitmapCallback): Bitmap? {
return playerImage()
}
fun playerImage(): Bitmap? {
var image: Bitmap? = null
try {
var url = URL("content image url")
image = BitmapFactory.decodeStream(url.openConnection().getInputStream())
} catch (e: IOException) {
}
return image
}
}
将播放器设置为通知管理器
if (pnm != null) {
pnm.setUseNextAction(false);
pnm.setUsePreviousAction(false);
MediaSessionCompat mediaSession = new MediaSessionCompat(mContext, "Player");
mediaSession.setActive(true);
mediaSession.setMetadata(new MediaMetadataCompat.Builder()
.putString(MediaMetadataCompat.METADATA_KEY_TITLE, title)
.putString(MediaMetadataCompat.METADATA_KEY_DISPLAY_DESCRIPTION, description)
.build());
MediaSessionConnector mediaSessionConnector =
new MediaSessionConnector(mediaSession);
mediaSessionConnector.setPlayer(simpleExoPlayer);
pnm.setMediaSessionToken(mediaSession.getSessionToken());
pnm.setPlayer(simpleExoPlayer);
pnm.setPriority(PRIORITY_LOW);
}