android - PlayerNotificationManager 未显示在 exoplayer、android 上播放视频的通知?

标签 android exoplayer

我的应用播放视频

我正在关注这个Exoplayer Notifications

有相同的代码

 PlayerNotificationManager playerNotificationManager = new PlayerNotificationManager(
            appCMSPresenter.getCurrentContext(),
            "kingkdfn",
            459, new DescriptionAdapter());

    playerNotificationManager.setPlayer(getPlayer());

描述适配器

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;
}
}

全部添加到 list 中

<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);
                }

关于android - PlayerNotificationManager 未显示在 exoplayer、android 上播放视频的通知?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62423354/

相关文章:

android - 如何在编辑文本android上创建凹面 View

Android GCM - list 文件和注册

Android Studio - Gradle 实现 (...) { exclude ... } 不工作(无法从依赖项中排除组)

android - 如何在播放时下载视频,使用 ExoPlayer?

android - 膨胀类 com.google.android.exoplayer2.ui.SimpleExoPlayerView 时出错

android - 使用 SurfaceView 居中裁剪 Exoplayer

android - 如何解决Gradle上的(显然不可见的)依赖冲突?

java - 从 EditText Android 获取整数值

android - Textview 在第一次点击时为空,但在第二次点击时更新

java - ExoPlayer,MediaSource 创建的视频播放器不播放从 URL 中提取的视频