-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use a custom MediaController to avoid self hiding of controls.
- Loading branch information
Showing
3 changed files
with
98 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
91 changes: 91 additions & 0 deletions
91
base/src/main/java/io/github/xwz/base/views/PlaybackControls.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters