VideoView中的后退按钮出现问题 [英] Problem with back button in VideoView
问题描述
按下后,我很难让后退按钮真正完成我的活动.我正在运行一个非常简单的videoview,使用progressdialog显示加载对话框和onpreparedlistener等简单的东西.无论如何,当前当我按下后退"按钮时,它只会取消进度对话框,并留下黑屏,然后再次按下,进度对话框会重新启动!!!然后再次单击后退"按钮时,它会显示一个警告对话框,无法播放视频".很烦人.感谢您的帮助.
I am having difficulty getting the back button to actually finish my activity when pressed. I am running a very simple videoview, using a progressdialog to show loading dialog and onpreparedlistener, etc etc. simple stuff. Anyways, currently when I press the back button, it will just cancel the progressdialog, and leave a black screen, and pressed again, the progressdialog restarts!!! and then when I click the back button again, it displays an alert dialog, "video cannot be played." very annoying. Thanks for your help.
public class VideoActivity extends Activity {
private VideoView mVideoView;
private static ProgressDialog progressdialog;
private String path;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.videoview);
progressdialog = ProgressDialog.show(this, "", " Video Loading...", true);
progressdialog.setCancelable(true);
mVideoView = (VideoView) findViewById(R.id.surface_view);
mVideoView.setMediaController(new MediaController(this));
Bundle b = this.getIntent().getExtras();
path = b.getString("path");
mVideoView.setVideoURI(Uri.parse(path));
mVideoView.setOnPreparedListener(new OnPreparedListener() {
public void onPrepared(MediaPlayer mp) {
progressdialog.dismiss();
mVideoView.requestFocus();
mVideoView.start();
}
});
}
@Override
public void onBackPressed() {
// TODO Auto-generated method stub
super.onBackPressed();
super.finish();
}
}
推荐答案
您可以简单地编写:(无需为MediaController创建新类)
You can simply write: (No need to create new class for MediaController)
mVideoView.setMediaController(new MediaController(this){
public boolean dispatchKeyEvent(KeyEvent event)
{
if (event.getKeyCode() == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_UP)
((Activity) getContext()).finish();
return super.dispatchKeyEvent(event);
}
});
这篇关于VideoView中的后退按钮出现问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!