MediaPlayerService被销毁后,传输控件未更新

时间:2018-11-27 16:14:44

标签: java android service android-mediaplayer wear-os

好的。我一直在努力解决这个问题。 自从最新的Wear OS升级以来,播放/暂停传输控件显示在通知面板中,而不是在通知中显示媒体控件。我的媒体播放器服务可以正常工作,直到它被暂停了一段时间,然后当它移到后台时android销毁了它。如果用户从传输控件中选择了“播放”按钮,则音乐将按原样开始播放,但是传输控件不再更新,这意味着用户只能点击播放并且不能再暂停。如果他们需要停止音频并且不能停止,这可能会非常令人沮丧。

这是我初始化媒体会话的代码。这称为onCreate

private void initMediaSession() {
    if (mediaSessionManager != null) return;

    mediaSessionManager = (MediaSessionManager) getSystemService(Context.MEDIA_SESSION_SERVICE);
    //Create a new mediaSession
    mediaSession = new MediaSessionCompat(getApplicationContext(), "com.turndapage.navmusic.mediaplayer");
    //Get mediaSessions Transport Controls
    mediaSession.getController().getTransportControls();
    //Set Mediasession ready to receive media commands
    mediaSession.setActive(true);

    mediaSession.setFlags(
            MediaSessionCompat.FLAG_HANDLES_MEDIA_BUTTONS |
                    MediaSessionCompat.FLAG_HANDLES_TRANSPORT_CONTROLS);

    callback = new MediaSessionCompat.Callback() {
        // Implement callbacks
        @Override
        public void onPlay() {
            super.onPlay();
            Cat.d("Playing from transport controls.");
            // This is the event that is triggered from the transport controls
            resumeMedia();
        }

        @Override
        public void onPause() {
            super.onPause();
            Cat.d("Pausing from transport controls");
            pauseMedia();
        }

        @Override
        public void onSkipToNext() {
            super.onSkipToNext();
            skipToNext();
        }

        @Override
        public void onSkipToPrevious() {
            super.onSkipToPrevious();
            skipToPrevious();
        }

        @Override
        public void onStop() {
            super.onStop();
            stop();
        }

        @Override
        public void onSeekTo(long position) {
            super.onSeekTo(position);
            mediaPlayer.seekTo((int)position);
        }

        @Override
        public void onFastForward() {
            super.onFastForward();
            fastForward();
        }

        @Override
        public void onRewind() {
            super.onRewind();
            rewind();
        }

        @Override
        public void onPlayFromMediaId(String mediaId, Bundle extras) {
            int id = Integer.parseInt(mediaId.replace("song", ""));
            int thisIndex = 0;
            for(Song song : songLibrary.getSongs()) {
                if(song.getID() == id) {
                    ArrayList<Song> songs = getCurrentList();
                    thisIndex = songs.indexOf(song);
                    break;
                }
            }
            int[] ids = new int[getCurrentList().size()];
            for(int i = 0; i < ids.length; i++) {
                ids[i] = getCurrentList().get(i).getID();
            }
            startPlaying(ids, thisIndex);
        }

        @Override
        public boolean onMediaButtonEvent(Intent mediaButtonEvent) {
            KeyEvent event = mediaButtonEvent.getParcelableExtra(Intent.EXTRA_KEY_EVENT);
            Cat.e("onMediaButtonEvent called: " + event);
            if (event.getAction() == KeyEvent.ACTION_UP) {
                switch (event.getKeyCode()) {
                    case KeyEvent.KEYCODE_MEDIA_PAUSE:
                        pauseMedia();
                        break;
                    case KeyEvent.KEYCODE_MEDIA_PLAY:
                        playMedia();
                        break;
                    case KeyEvent.KEYCODE_MEDIA_NEXT:
                        //skipToNext();
                        break;
                    case KeyEvent.KEYCODE_MEDIA_PREVIOUS:
                        //skipToPrevious();
                        break;
                }
            }
            return super.onMediaButtonEvent(mediaButtonEvent);
        }
    };

    // Attach a callback to receive MediaSession update
    mediaSession.setCallback(callback);
    setSessionToken(mediaSession.getSessionToken());
}

当用户单击播放传输控件时,将调用此方法

public void resumeMedia() {
    wasPlaying = true;
    if(mediaPlayer != null) {
        try {
            if (!getPlaying()) {
                mediaPlayer.seekTo(resumePosition);
                mediaPlayer.start();
                sendPlayPauseUpdate(true);
                buildNotification(true);
            }
            ComplicationHelper.updatePlayPause(this, true);
        } catch (IllegalStateException ex) {
            ex.printStackTrace();
            //restoreState();
            startMediaPlayer();
        }
    }
}

发送播放/暂停更新只是向活动发送广播,以更新那里的控件。

在通知代码中,我用它来更新媒体会话的播放状态。如您所见,仅根据媒体播放器是否在播放来添加暂停或播放动作。尽管如此,仍会显示播放动作:

long actions = MEDIA_SESSION_ACTIONS;
    if(isPlaying)
        actions |= PlaybackStateCompat.ACTION_PAUSE;
    else
        actions |= PlaybackStateCompat.ACTION_PLAY;
    mediaSession.setPlaybackState(new PlaybackStateCompat.Builder()
            .setActions(actions)
            .setState(isPlaying ? PlaybackStateCompat.STATE_PLAYING :
                    PlaybackStateCompat.STATE_PAUSED, resumePosition, 1)
            .build());

这是媒体会话操作:

// Media Session Actions
private static final long MEDIA_SESSION_ACTIONS =
        PlaybackStateCompat.ACTION_SEEK_TO |
                PlaybackStateCompat.ACTION_SKIP_TO_PREVIOUS |
                PlaybackStateCompat.ACTION_SKIP_TO_NEXT |
                PlaybackStateCompat.ACTION_PLAY_FROM_SEARCH |
                PlaybackStateCompat.ACTION_PREPARE |
                PlaybackStateCompat.ACTION_PREPARE_FROM_SEARCH |
                PlaybackStateCompat.ACTION_PLAY_PAUSE |
                PlaybackStateCompat.ACTION_STOP |
                PlaybackStateCompat.ACTION_FAST_FORWARD |
                PlaybackStateCompat.ACTION_REWIND;

我尝试过的事情:

  • 将媒体会话更改为静态并返回
  • 在创建回调时将其设为变量,而不是在创建新回调时。
  • 从传输控件手动将mediasession的播放状态设置为播放后播放

尽管我正在使用挂起的意图发送启动命令来运行恢复功能而不是传输控件,但这在电话应用程序上可以找到。如果我正在使用手机播放音频,并且即使手机应用使用的是完全相同的类,则控件也可以正确更新,我也可以从手表的传输控件中恢复播放。

请让我知道其他信息是否有帮助。

谢谢!

-乔尔

0 个答案:

没有答案