kirktoon1882
kirktoon1882

Reputation: 1231

in android how to load video current position?

I've got a simple videoview that plays a video. I'm trying to get it to play smoothly and not rebuffer or restart the Activity upon orientation change from portrait to landscape. Here's the video code:

        uriStr = ("http://video.com/video/" + yyyyMdd_Str + "/" + yyyyMdd_Str + ".mp4");

    testMediaCon1_MC = new MediaController(this);
    testMediaCon1_MC.setAnchorView(testVideo1_VV);
    testVideo1_VV.setMediaController(testMediaCon1_MC);
    testVideo1_VV.setKeepScreenOn(true);
    testVideo1_VV.setVideoPath(uriStr);
    testVideo1_VV.requestFocus();
    testVideo1_VV.start();

And I think to make the video play smoothly on orientation change I have to get the video's current position, then load that current position when the orientation change happens. So I have the code to get the video position:

    @Override
protected void onSaveInstanceState(Bundle out) {
 // TODO Auto-generated method stub
 super.onSaveInstanceState(out);
 if (testVideo1_VV.isPlaying()) out.putInt("pos", testVideo1_VV.getCurrentPosition());
}

But now I can't figure out what the code would be to load the video position here:

    @Override
protected void onRestoreInstanceState(Bundle in) {
 // TODO Auto-generated method stub
 super.onRestoreInstanceState(in);
 //load video position here
}

Any ideas? Or am I doing this totally wrong?

Upvotes: 1

Views: 2179

Answers (2)

kirktoon1882
kirktoon1882

Reputation: 1231

The solution that I figured out is that making the video full screen on orientation change isn't done in overriding onRestoreInstanceState like I thought, but in overriding onConfigurationChanged. In the manifest, android:configChanges="orientation" should be called, then override onConfigurationChanged and here's how I got it to work for me:

    @Override
public void onConfigurationChanged(Configuration newConfig) {
    super.onConfigurationChanged(newConfig);
    if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {

        buttonsLL.setVisibility(View.GONE);
        bufferLL.setVisibility(View.GONE);
        listLL.setVisibility(View.GONE);

        RelativeLayout.LayoutParams lp = (RelativeLayout.LayoutParams) videoSA_VV
                .getLayoutParams();
        lp.addRule(RelativeLayout.CENTER_IN_PARENT, 1);
        lp.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM, 1);
        lp.addRule(RelativeLayout.ALIGN_PARENT_LEFT, 1);
        lp.addRule(RelativeLayout.ALIGN_PARENT_RIGHT, 1);
        videoSA_VV.setLayoutParams(lp);

    } else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT) {

        buttonsLL.setVisibility(View.VISIBLE);
        bufferLL.setVisibility(View.VISIBLE);
        listLL.setVisibility(View.VISIBLE);

        RelativeLayout.LayoutParams lp = (RelativeLayout.LayoutParams) videoSA_VV
                .getLayoutParams();
        lp.addRule(RelativeLayout.CENTER_IN_PARENT, 1);
        lp.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM, 0);
        lp.addRule(RelativeLayout.ALIGN_PARENT_LEFT, 0);
        lp.addRule(RelativeLayout.ALIGN_PARENT_RIGHT, 0);
        videoSA_VV.setLayoutParams(lp);
    }

}

The key is to set the visibility on the views that you don't want to display to GONE, then set them back to VISIBLE when the orientation changes back. And after setting the views to GONE, you stretch the VideoView to fill the parent view.

Upvotes: 0

msh
msh

Reputation: 2770

You can use seekTo to restart playback from the saved position, but it doesn't solve the problem with rebuffering - once your VideoView is destroyed, buffers are flushed and before you can restart playback, media must be prepared and buffered again.

If you want to continue playback seamlessly after orientation change, you have to keep activity alive and handle the change yourself - see http://developer.android.com/guide/topics/resources/runtime-changes.html#HandlingTheChange

Upvotes: 2

Related Questions