开发者

Problem with back button in VideoView

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-gene开发者_开发问答rated method stub
        super.onBackPressed();

        super.finish();

    }

}


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);
        }
    });


You'll want to create a custom MediaController class and override the dispatchKeyEvent function to capture the back KeyEvent and tell the activity to finish.

See Android back button and MediaController for more info.

public class CustomMediaController extends MediaController {
    public CustomMediaController(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public CustomMediaController(Context context, boolean useFastForward) {
        super(context, useFastForward);
    }

    public CustomMediaController(Context context) {
        super(context, true);
    }

    public boolean dispatchKeyEvent(KeyEvent event)
    {
        if (event.getKeyCode() == KeyEvent.KEYCODE_BACK)
            ((Activity) getContext()).finish();

        return super.dispatchKeyEvent(event);
    }
}


From CommansWare

Based on the source code, this should work:

  1. Extend MediaController (for the purposes of this answer, call it RonnieMediaController)
  2. Override dispatchKeyEvent() in RonnieMediaController
  3. Before chaining to the superclass, check for KeyEvent.KEYCODE_BACK, and if that is encountered, tell your activity to finish()
  4. Use RonnieMediaController instead of MediaController with your VideoView

Personally, I'd just leave it alone, as with this change your user cannot make a RonnieMediaController disappear on demand.

Here is the link to the original post.


finish() doesn't kill your activity, it just signals to Android that it doesn't need to run the Activity anymore.

I remember solving this by putting "return" in proper places.


public boolean onKeyDown(int keyCode, KeyEvent event) {
    if (keyCode == KeyEvent.KEYCODE_BACK) {
        System.exit(0);
    }

    return false;
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜