さまざまなビデオ ファイルを表示するアクティビティがあります。ビデオ ファイルをクリックすると、VideoView がビデオを再生する別のアクティビティに移動します。
私の問題は、このアクティビティを終了して前に戻りたい場合、戻るには戻るボタンを 2 回クリックする必要があることです。1 回だけクリックすると、ビデオの再生が再び開始され、2 回目の試行でのみ画面を終了できます。
それから私はこれを試しました:
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
Log.d(Constants.LOG_TAG, "back pressed in videoplayer");
finish();
return true;
}
return super.onKeyDown(keyCode, event);
}
そして、logcat で「ビデオ プレーヤーで押し戻されました」と表示されますが、アクティビティは終了しません。戻るボタンを 2 回押す必要があります。
EDIT:これは最も関連性の高い(私が信じている)ソースコードです。ただし、ビデオはインターネットから再生され、Mediacontroller を使用していないことに注意してください。代わりに、独自のレイアウトとビデオビュー コントロールへのリンクを定義しています。
public class VideoPlayer extends Activity implements OnClickListener, OnCompletionListener,
OnSeekBarChangeListener, OnPreparedListener, OnTouchListener {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.video_view);
// Gets the position of clicked video, from an ArrayList of video urls.
selectedVideo = getPosition();
// the play button
play = (ImageButton) findViewById(R.id.play);
play.setOnClickListener(this);
videoView = (VideoView) findViewById(R.id.videoView);
videoView.setOnCompletionListener(this);
videoView.setOnPreparedListener(this);
videoView.setOnTouchListener(this);
// the url to play
String path = videoUris.get(selectedVideo);
videoView.setVideoPath(getPath(path));
}
/**
* Play or Pause the current video file.
*
* If the video is paused, then invoking this method should start it. If the video is already playing, then the
* video should pause.
*/
private void play() {
if (!isVideoStarted) {
isVideoStarted = true;
videoView.start();
play.setImageResource(R.drawable.video_pause);
videoSeekBar.post(updateSeekBarRunnable);
} else if (isVideoStarted) {
isVideoStarted = false;
pause();
}
}
/**
* Start playing back a video file with the specified Uri.
*/
private void startPlayback() {
String path = videoUris.get(selectedVideo);
videoView.setVideoPath(getPath(path));
videoView.start();
}
/**
* Stops the currently playing video. (The SeekBar position is reset to beginning, 0.)
*/
private void stopPlayback() {
videoView.stopPlayback();
}
/**
* Pause the currently playing video. (The SeekBar remains in its position.)
*/
private void pause() {
videoView.pause();
@Override
public void onPrepared(MediaPlayer mp) {
play();
}
}