android 按2次退出程序,Android - VideoView需要按两次BACK才能退出

我有一个显示不同视频文件的活动。当我点击一个视频文件时,我被带到另一个活动,在那里一个VideoView播放视频。

我的问题是,当我想退出此活动并返回到上一个时,我应该单击两次后退按钮,以便返回。如果我只点击一次,视频就会再次开始播放,只有在第二次尝试我才能退出屏幕时。

然后我尝试了这个:

@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();

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值