Skip to content

Commit

Permalink
refactor 'PlayerService'
Browse files Browse the repository at this point in the history
- add method: 'onCreateMediaSessionCallback():MediaSessionCallback'
- new feature: support custom 'MediaSessionCompat.Callback'
  • Loading branch information
jrfeng committed Sep 7, 2020
1 parent af82bd9 commit 1f41023
Showing 1 changed file with 65 additions and 12 deletions.
77 changes: 65 additions & 12 deletions player/src/main/java/snow/player/PlayerService.java
Original file line number Diff line number Diff line change
Expand Up @@ -324,7 +324,9 @@ public void onHeadsetHookClicked(int clickCount) {
private void initMediaSession() {
mMediaSession = new MediaSessionCompat(this, this.getClass().getName());
mPlayer.setMediaSession(mMediaSession);
mMediaSession.setCallback(new MediaSessionCallbackImp());

mMediaSession.setCallback(onCreateMediaSessionCallback());

setSessionToken(mMediaSession.getSessionToken());
}

Expand All @@ -349,6 +351,20 @@ private void initHistoryRecorder() {
mHistoryRecorder = createHistoryRecorder();
}

/**
* 创建一个 {@link MediaSessionCallback} 对象。
* <p>
* 如果你需要对 MediaSession 框架的 MediaSessionCompat.Callback 进行定制,则可以覆盖该方法并返回一个
* {@link MediaSessionCallback} 对象。{@link MediaSessionCallback} 类继承了
* MediaSessionCompat.Callback 类。
*
* @see MediaSessionCallback
*/
@NonNull
protected MediaSessionCallback onCreateMediaSessionCallback() {
return new MediaSessionCallback(this);
}

@Nullable
protected final NotificationView createNotificationView() {
if (injectNotificationView()) {
Expand Down Expand Up @@ -1035,10 +1051,45 @@ protected void detachAudioEffect() {
}
}

private class MediaSessionCallbackImp extends MediaSessionCompat.Callback {
/**
* 该类继承了 MediaSessionCompat.Callback 类,如果你需要对 MediaSession 框架的
* 的 MediaSessionCompat.Callback 进行定制,则可以覆盖 {@link #onCreateMediaSessionCallback()} 方法,
* 并返回一个自定义的 {@link MediaSessionCallback} 实现。
* <p>
* 注意!在覆盖 {@link MediaSessionCallback} 的方法时,使用 {@code super.xxx} 回调超类被覆盖的方法,
* 因为 {@link PlayerService} 的部分功能依赖这些方法。如果没有使用 {@code super.xxx}
* 回调超类被覆盖的方法,则这部分功能将无法正常工作。
*
* @see #onCreateMediaSessionCallback()
*/
public static class MediaSessionCallback extends MediaSessionCompat.Callback {
private PlayerService mPlayerService;
private Player mPlayer;

public MediaSessionCallback(@NonNull PlayerService playerService) {
Preconditions.checkNotNull(playerService);
mPlayerService = playerService;
mPlayer = mPlayerService.getPlayer();
}

/**
* 获取当前 {@link MediaSessionCallback} 关联到的 {@link PlayerService} 对象。
*/
@NonNull
public PlayerService getPlayerService() {
return mPlayerService;
}

/**
* 获取播放器的 {@link Player} 对象。用于对播放器进行控制。
*/
public Player getPlayer() {
return mPlayer;
}

@Override
public boolean onMediaButtonEvent(Intent mediaButtonEvent) {
if (PlayerService.this.onMediaButtonEvent(mediaButtonEvent)) {
if (mPlayerService.onMediaButtonEvent(mediaButtonEvent)) {
return true;
}

Expand All @@ -1047,12 +1098,12 @@ public boolean onMediaButtonEvent(Intent mediaButtonEvent) {

@Override
public void onCustomAction(String action, Bundle extras) {
PlayerService.this.onCustomAction(action, extras);
mPlayerService.onCustomAction(action, extras);
}

@Override
public void onPlay() {
PlayerService.this.getPlayer().play();
mPlayer.play();
}

@Override
Expand All @@ -1062,37 +1113,37 @@ public void onSkipToQueueItem(long id) {

@Override
public void onPause() {
PlayerService.this.getPlayer().pause();
mPlayer.pause();
}

@Override
public void onSkipToNext() {
PlayerService.this.getPlayer().skipToNext();
mPlayer.skipToNext();
}

@Override
public void onSkipToPrevious() {
PlayerService.this.getPlayer().skipToPrevious();
mPlayer.skipToPrevious();
}

@Override
public void onFastForward() {
PlayerService.this.getPlayer().fastForward();
mPlayer.fastForward();
}

@Override
public void onRewind() {
PlayerService.this.getPlayer().rewind();
mPlayer.rewind();
}

@Override
public void onStop() {
PlayerService.this.getPlayer().stop();
mPlayer.stop();
}

@Override
public void onSeekTo(long pos) {
PlayerService.this.getPlayer().seekTo((int) pos);
mPlayer.seekTo((int) pos);
}

@Override
Expand Down Expand Up @@ -1963,4 +2014,6 @@ public interface CustomAction {
*/
void doAction(@NonNull Player player, @Nullable Bundle extras);
}


}

0 comments on commit 1f41023

Please sign in to comment.