Skip to content

Commit

Permalink
Use a custom MediaController to avoid self hiding of controls.
Browse files Browse the repository at this point in the history
  • Loading branch information
xwz committed Sep 23, 2015
1 parent 79683a4 commit 61dc37a
Show file tree
Hide file tree
Showing 3 changed files with 98 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
import io.github.xwz.base.player.EventLogger;
import io.github.xwz.base.player.HlsRendererBuilder;
import io.github.xwz.base.player.VideoPlayer;
import io.github.xwz.base.views.PlaybackControls;
import io.github.xwz.base.views.VideoPlayerView;

/**
Expand All @@ -67,7 +68,7 @@ public abstract class VideoPlayerActivity extends BaseActivity implements Surfac

private EventLogger eventLogger;
private MediaSession mediaSession;
private MediaController mediaController;
private PlaybackControls mediaController;
private DurationLogger timeLogger;

private VideoPlayerView videoPlayerView;
Expand Down Expand Up @@ -117,7 +118,7 @@ public void onCreate(Bundle savedInstanceState) {
setContentView(R.layout.video_player_activity);
View root = findViewById(R.id.root);

mediaController = new MediaController(this);
mediaController = new PlaybackControls(this);
mediaController.setAnchorView(root);
videoPlayerView = new VideoPlayerView(this, mediaController, root);

Expand Down
91 changes: 91 additions & 0 deletions base/src/main/java/io/github/xwz/base/views/PlaybackControls.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
package io.github.xwz.base.views;

import android.content.Context;
import android.view.KeyEvent;
import android.view.View;
import android.widget.ImageButton;
import android.widget.MediaController;

public class PlaybackControls extends MediaController {
private MediaPlayerControl mPlayer;
private View mRoot;
private ImageButton mPauseButton;


public PlaybackControls(Context context) {
super(context);
}

@Override
public void setMediaPlayer(MediaPlayerControl player) {
mPlayer = player;
super.setMediaPlayer(player);
}

@Override
public void setAnchorView(View view) {
super.setAnchorView(view);
mRoot = this.getChildAt(0);
//mPauseButton = (ImageButton) mRoot.findViewById();
}

public boolean dispatchKeyEvent(KeyEvent event, int timeout) {
int keyCode = event.getKeyCode();
final boolean uniqueDown = event.getRepeatCount() == 0
&& event.getAction() == KeyEvent.ACTION_DOWN;
if (keyCode == KeyEvent.KEYCODE_HEADSETHOOK
|| keyCode == KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE
|| keyCode == KeyEvent.KEYCODE_SPACE) {
if (uniqueDown) {
doPauseResume();
show(timeout);
if (mPauseButton != null) {
mPauseButton.requestFocus();
}
}
return true;
} else if (keyCode == KeyEvent.KEYCODE_MEDIA_PLAY) {
if (uniqueDown && !mPlayer.isPlaying()) {
mPlayer.start();
updatePausePlay();
show(timeout);
}
return true;
} else if (keyCode == KeyEvent.KEYCODE_MEDIA_STOP
|| keyCode == KeyEvent.KEYCODE_MEDIA_PAUSE) {
if (uniqueDown && mPlayer.isPlaying()) {
mPlayer.pause();
updatePausePlay();
show(timeout);
}
return true;
} else if (keyCode == KeyEvent.KEYCODE_VOLUME_DOWN
|| keyCode == KeyEvent.KEYCODE_VOLUME_UP
|| keyCode == KeyEvent.KEYCODE_VOLUME_MUTE
|| keyCode == KeyEvent.KEYCODE_CAMERA) {
// don't show the controls for volume adjustment
return super.dispatchKeyEvent(event);
} else if (keyCode == KeyEvent.KEYCODE_BACK || keyCode == KeyEvent.KEYCODE_MENU) {
if (uniqueDown) {
hide();
}
return true;
}

show(timeout);
return super.dispatchKeyEvent(event);
}

private void updatePausePlay() {
super.setMediaPlayer(mPlayer);
}

private void doPauseResume() {
if (mPlayer.isPlaying()) {
mPlayer.pause();
} else {
mPlayer.start();
}
updatePausePlay();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public class VideoPlayerView {

private final Context mContext;

private final MediaController mediaController;
private final PlaybackControls mediaController;
private MediaController.MediaPlayerControl mPlayer;

private static final List<Integer> PLAY_PAUSE_EVENTS = Arrays.asList(
Expand All @@ -70,7 +70,7 @@ public class VideoPlayerView {
KeyEvent.KEYCODE_CAMERA
);

public VideoPlayerView(Context context, MediaController controller, View root) {
public VideoPlayerView(Context context, PlaybackControls controller, View root) {
mContext = context;
mediaController = controller;
shutterView = root.findViewById(R.id.shutter);
Expand All @@ -92,6 +92,7 @@ public VideoPlayerView(Context context, MediaController controller, View root) {
card.setFocusableInTouchMode(true);
card.setInfoVisibility(View.VISIBLE);
card.setExtraVisibility(View.VISIBLE);
card.setInfoAreaBackgroundColor(context.getResources().getColor(R.color.black_900));
Point size = new Point(context.getResources().getDimensionPixelSize(R.dimen.card_width),
context.getResources().getDimensionPixelSize(R.dimen.card_height));
nextEpisode = new EpisodeCardView(context, card, size, false);
Expand Down Expand Up @@ -122,7 +123,7 @@ private boolean handleKeyEvents(View v, int keyCode, KeyEvent event) {
doPauseResume();
return true;
} else if (PLAYER_EVENTS.contains(keyCode)) {
return mediaController.dispatchKeyEvent(event);
return mediaController.dispatchKeyEvent(event, 0);
}
return false;
}
Expand Down

0 comments on commit 61dc37a

Please sign in to comment.