开发者

Android: getting back after playing video

I am working on an application where the user have a video list with play buttons on each row of listview. Whenever the user plays the play button, a separate activity is started for playing video. The problem I am having is that I have to press the back-button twice in order to return to the main Video. Moreover, I have set my video to landscape mode. When my original application is in portrait mode and the video is in landscape mode and then I return to my main application, it crashes. however, when my application or my device is in landscape mode and I play a video, then it is fine when I go back. So, I have two questions

1- Why I have to press the back button twice to go back to the mainActivity (I have also used onBackPressed in the video activity to finish the current activity but its not working) 2- Why my application crashes when it is in the portrait mode and the video plays in the landscape mode and while returning to the main application, it changes the video to portrait mode as well for sometime and then crashes. Here is the code of my video playback (i don't know why I have to press back button twice to go back to the main application)

public void onCreate(Bundle icicle) {
    super.onCreate(icicle);
    setContentView(R.layout.videoview);
    Intent i = getIntent();
    Bundle extras = i.getExtras();
    filename = extras.getString("videofilename");
    mVideoView = (VideoView)findViewById(R.id.videoview);
    path=filename;
    if (path == "") {

        Toast.makeText(
                ViewVideo.this, N开发者_开发技巧o video found,
                Toast.LENGTH_LONG).show();

    } else {

          mVideoView.setVideoPath(path);
          mVideoView.setMediaController(new MediaController(this));
          mVideoView.requestFocus();
          mVideoView.start();

    }
}


  @Override
  public void onBackPressed()
   {
   Log.v(TAG,"movie finished");
   finish();
  }}

I hope my question was clear and someone will be able to help me

THANKS


Activity will destroy recreated it self if the orientation changes from portrait to landscape and vice versa,for that reason when you go back from land to port it may be some of the variable which you used in port activities may not be set,possibly throws NullPointerException.It is better if you provide more details like logcat and code.


1. To avoid double back clicking when playing video you can simply write:

mVideoView.setMediaController(new MediaController(this){
            public boolean dispatchKeyEvent(KeyEvent event)
            {
                if (event.getKeyCode() == KeyEvent.KEYCODE_BACK)
                    ((Activity) getContext()).finish();

                return super.dispatchKeyEvent(event);
            }
        });

2. To prevent application from crashing when portrait mode changes, add to your AndroidManifest.xml tag android:configChanges like that:

<activity
    android:name=".YourActivity"
    android:configChanges="orientation|screenSize">
</activity>

So, your activity won't be recreated.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜