Android13播放状态回调未通过通知按钮调用
创始人
2024-10-01 00:01:07
0

在Android 13中,通过通知按钮控制音频播放时,可能出现播放状态回调未被调用的情况。这可能是由于以下原因之一造成的:

  1. 通知通道没有正确设置或过时
  2. 播放器可能未正确处理通知按钮点击事件

以下是解决此问题的一种方法:

  1. 确保您的应用程序中有通知通道,并正确设置
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
    NotificationManager notificationManager = getSystemService(NotificationManager.class);

    NotificationChannel channel = new NotificationChannel(
            CHANNEL_ID,
            "Channel name",
            NotificationManager.IMPORTANCE_LOW
    );

    channel.setSound(null, null);
    channel.setDescription("Channel description");
    notificationManager.createNotificationChannel(channel);
}
  1. 使用MediaSessionCompat将通知框架与您的播放器集成:
MediaSessionCompat mediaSession = new MediaSessionCompat(this, "tag");
mediaSession.setActive(true);
mediaSession.setMetadata(new MediaMetadataCompat.Builder()
        .putString(MediaMetadataCompat.METADATA_KEY_TITLE, "Title")
        .putString(MediaMetadataCompat.METADATA_KEY_ARTIST, "Artist")
        .build());
mediaSession.setCallback(new MediaSessionCompat.Callback() {
    @Override
    public void onPlay() {
        super.onPlay();
        // Start playback here
    }

    @Override
    public void onPause() {
        super.onPause();
        // Pause playback here
    }

    @Override
    public void onStop() {
        super.onStop();
        // Stop playback here
    }
});

PlaybackStateCompat state = new PlaybackStateCompat.Builder()
        .setState(
                PlaybackStateCompat.STATE_PLAYING,
                PlaybackStateCompat.PLAYBACK_POSITION_UNKNOWN,
                1.0f
        )
        .setActions(
                PlaybackStateCompat.ACTION_PLAY
                        | PlaybackStateCompat.ACTION_PAUSE
                        | PlaybackStateCompat.ACTION_STOP
        )
        .build();
mediaSession.setPlaybackState(state);

NotificationCompat.Builder builder = new NotificationCompat.Builder(this, CHANNEL_ID);
builder.setStyle(new androidx.media.app.NotificationCompat.MediaStyle()
        .setMediaSession(mediaSession.getSessionToken())
        .setShowActionsInCompactView(0, 1, 2)
        .setContentTitle("Title")
        .setContentText("Artist")
        .setSmallIcon(R.drawable.ic_launcher_background))
        .setPriority(NotificationCompat.PRIORITY_LOW)
        .setOngoing(true)
        .setVisibility(NotificationCompat.VISIBILITY_PUBLIC);
builder.addAction(new NotificationCompat.Action(
        R.drawable.ic_skip_previous,
        "Previous

相关内容

热门资讯

WePoKe透明挂!德扑快速计... WePoKe透明挂!德扑快速计算胜率(透视)本然是有挂(详细辅助AA德州教程)1、超多福利:超高返利...
透视规律!aapkoer德州辅... 透视规律!aapkoer德州辅助挂下载,wepoker养号规律(详细辅助安装教程)一、aapkoer...
德州之星外挂!wpk微扑克真的... 德州之星外挂!wpk微扑克真的有辅助(透视)从前是真的有挂(详细辅助黑科技教程)1、不需要AI权限,...
透视实锤!wpk辅助器安装,w... 透视实锤!wpk辅助器安装,wpk俱乐部是做什么的(详细辅助透明挂教程)1、wpk辅助器安装系统规律...
WePoKe透明挂!wpk微扑... WePoKe透明挂!wpk微扑克辅助ai(透视)其实有挂(详细辅助wepoke教程)(1)WePoK...
透视神器!wpk透视辅助靠谱吗... 透视神器!wpk透视辅助靠谱吗,wepoker国外版透视(详细辅助AI教程)1、这是跨平台的wpk透...
wepoke有挂!nzt德州辅... wepoke有挂!nzt德州辅助(透视)一直存在有挂(详细辅助专业教程)1、金币登录送、破产送、升级...
透视插件!wepoker透视脚... 透视插件!wepoker透视脚本网页,wepoker免费辅助器(详细辅助透牌教程)1、金币登录送、破...
微扑克游戏辅助器!红龙扑克模拟... 微扑克游戏辅助器!红龙扑克模拟器(透视)原生真的是有挂(详细辅助技巧教程)(1)微扑克游戏辅助器!红...
透视攻略!wpk辅助插件,we... 透视攻略!wpk辅助插件,wepoker轻量版透视(详细辅助2025新版教程);在进入wpk辅助插件...