我有一個顯示不同視頻文件的活動。當我點擊一個視頻文件時,我被帶到另一個活動,在那裏一個VideoView播放視頻。Android - VideoView需要按兩次BACK才能退出
我的問題是,當我想退出此活動並返回到上一個時,我應該單擊兩次後退按鈕,以便返回。如果我只點擊一次,視頻就會再次開始播放,只有在第二次嘗試我才能退出屏幕時。
然後我嘗試這樣的:
@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的「視頻播放器回壓」,該活動不會退出見。我仍然應該按兩次後退按鈕。
編輯:這是最相關的(我相信)的源代碼。但請注意,視頻是從互聯網播放的,我沒有使用Mediacontroller,而是定義自己的佈局並鏈接到videoview conrols。
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();
}
}
可以顯示ü[R怎麼玩這個視頻? –
添加了代碼。 –