我有一些代码,该代码具有用于实现所需输出的开关实现。现在,我需要该实现也具有下一个和上一个按钮。但是我不知道简单的解决方案,这是入门。这是我的代码:
private MediaPlayer clipSetup(int i){
switch (i){
case 1:
mediaPlayer = MediaPlayer.create(this, R.raw.al01);
mediaPlayer.start();
break;
case 2:
mediaPlayer = MediaPlayer.create(this, R.raw.al02);
mediaPlayer.start();
break;
case 3:
mediaPlayer = MediaPlayer.create(this, R.raw.al02);
mediaPlayer.start();
break;
}
return mediaPlayer;
}
现在看这段代码,我希望我可以执行OnClick按钮。
答案 0 :(得分:1)
希望这会对您有所帮助。
int current=0;
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.btn_back:
current++;
break;
case R.id.btn_next:
if(current<=0)
current=2;
else
current--;
break;
}
clipSetup(current%3+1);
}
答案 1 :(得分:0)
如果您需要实现远程按钮,请使用下面的代码。
int keycode = event.getKeyCode();
if(event.getAction() != KeyEvent.ACTION_DOWN){
return true;
}
switch (keycode){
case KeyEvent.KEYCODE_DPAD_LEFT :
if(videoType.equals(GENERAL)){
if(event.getAction() == KeyEvent.ACTION_DOWN){
rewindSurfacePlayer();
}
}
break;
case KeyEvent.KEYCODE_DPAD_RIGHT:
if(videoType.equals(GENERAL)){
if(event.getAction() == KeyEvent.ACTION_DOWN){
fastForwardSurfacePlayer();
}
}
break;
case KeyEvent.KEYCODE_DPAD_UP:
if(videoType.equals(GENERAL)){
if(event.getAction() == KeyEvent.ACTION_DOWN){
showSurfaceControls();
}
}
break;
case KeyEvent.KEYCODE_DPAD_DOWN:
if(videoType.equals(GENERAL)){
if(event.getAction() == KeyEvent.ACTION_DOWN){
if(!vidPaused){
pauseSurfacePlayer();
}else {
playSurfacePlayer();
}
}
}
break;
case KeyEvent.KEYCODE_PROG_RED://rewind
if(videoType.equals(GENERAL)){
if(event.getAction() == KeyEvent.ACTION_DOWN){
rewindSurfacePlayer();
}
}
break;
case KeyEvent.KEYCODE_PROG_GREEN://forward
if(videoType.equals(GENERAL)){
if(event.getAction() == KeyEvent.ACTION_DOWN){
fastForwardSurfacePlayer();
}
}
break;
case KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE:
if(videoType.equals(GENERAL)){
if(event.getAction() == KeyEvent.ACTION_DOWN){
if(!vidPaused){
pauseSurfacePlayer();
}else {
playSurfacePlayer();
}
}
}
break;
case KeyEvent.KEYCODE_MEDIA_STOP:
stopVideo();
break;
case 23://play/pause
if(videoType.equals(GENERAL)){
if(event.getAction() == KeyEvent.ACTION_DOWN){
if(!vidPaused){
pauseSurfacePlayer();
}else {
playSurfacePlayer();
}
}
}
break;
case KeyEvent.KEYCODE_MEDIA_FAST_FORWARD:
if(videoType.equals(GENERAL)){
if(event.getAction() == KeyEvent.ACTION_DOWN){
fastForwardSurfacePlayer();
}
}
break;
case KeyEvent.KEYCODE_MEDIA_REWIND:
if(videoType.equals(GENERAL)){
if(event.getAction() == KeyEvent.ACTION_DOWN){
rewindSurfacePlayer();
}
}
break;
case KeyEvent.KEYCODE_MEDIA_PAUSE:
if(event.getAction() == KeyEvent.ACTION_DOWN){
pauseSurfacePlayer();
}
break;
case KeyEvent.KEYCODE_MEDIA_PLAY:
if(event.getAction() == KeyEvent.ACTION_DOWN){
playSurfacePlayer();
}
break;
}
这需要继续使用dispatchKeyEvent方法btw。您也可以在此处执行后退按钮的手柄。无需使用onBackPressed方法。如果您已经在使用onBackPressed,并且想使用dispatchKeyEvent,请删除您的onBackPressed方法。