Android 11媒体恢复会发出第二次通知

问题描述

我已实现媒体恢复,以在电话重启后显示最近的曲目。 根据{{​​3}},在点击播放按钮后,“静态媒体控件将与从您的通知创建的媒体控件进行交换”,但对我而言,它不会被交换,并且我有静态媒体控件通知和由此创建的新媒体通知。 怎么了系统如何知道应该交换哪些通知?

dev blog

enter image description here

我的代码:

public BrowserRoot onGetRoot(@NonNull String clientPackageName,int clientUid,@Nullable Bundle rootHints) {


        //ANDROID 11 playback resumption - https://developer.android.com/guide/topics/media/media-controls#java
        if (rootHints != null) {
            if (rootHints.getBoolean(BrowserRoot.EXTRA_RECENT)) {
                // Return a tree with a single playable media item for resumption.
                Bundle extras = new Bundle();
                extras.putBoolean(BrowserRoot.EXTRA_RECENT,true);

                KLog.d(clientPackageName + " -> onGetRoot BrowserRoot.EXTRA_RECENT");


                return new BrowserRoot(MEDIA_ID_RECENT,extras);
            }
        }

        

        return new BrowserRoot(MEDIA_ID_ROOT,null);
    }

onPlay:

  @Override
        public void onPlay() {
            super.onPlay();

            CommonOperations.crashLog("mediaSessionCallback onPlay");
            KLog.d("mediaSessionCallback onPlay");

            fakeStartForeground();

            if (realm == null || realm.isClosed()) {
                initRealm();
            }

            if (playlist != null && currentEpisode != null) {
                play();
            } else {
                List<Episode> unfinished = UserDataManager.getInstance(URLPlayerService.this)
                        .getUnfinishedEpisodesData();
                if (unfinished != null && unfinished.size() > 0) {
                    EpisodePlaylist list = new EpisodePlaylist(unfinished);
                    URLPlayerService.startActionSetPlaylist(URLPlayerService.this,list,true);
                } else {
                    KLog.w("stopself");
                    if (!wasForegroudStart) {
                        fakeStartForeground();
                    }
                    CommonOperations
                            .crashLog("stopself #" + new Exception().getStackTrace()[0].getLineNumber());
                    stopSelf();
                    cancelNotification();
                }
            }
        }

解决方法

我想我已经解决了我的问题。在Android 11中可能会有一些萌芽,当我在使用MediaStyle通知之前使用没有MediaStyle通知的startForeground时,此问题经常发生。即使没有它,我也会不时收到双重通知。

我最终使用了ExoPlayer扩展程序中的PlayerNotificationManager。

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...