Vishwajit Palankar
Vishwajit Palankar

Reputation: 3083

VideoView plays in portrait mode but not in landscape mode

I have created two layout one normal layout and one for landscape. below is the VideoView xml for both the orientation. The video is streamed from an URL. The video is played perfectly in portrait mode but when is change the orientation to landscape mode the video is not played it just keeps on showing me the ProgressDialog. The below code may help in understanding my problem.

Normal Layout:

<VideoView
            android:id="@+id/vv_item_video"
            android:layout_width="match_parent"
            android:layout_height="250dp" />

Landscape Layout:

<VideoView
            android:id="@+id/vv_item_video"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />

Java code for executing the VideoView:

getWindow().setFormat(PixelFormat.TRANSLUCENT);
vvItemVideo.setVideoURI(Uri.parse(getResUrl()));
vvItemVideo.requestFocus();
progDailog = ProgressDialog.show(this, "Please wait ...", "Retrieving data ...", true, true);
vvItemVideo.setOnPreparedListener(new android.media.MediaPlayer.OnPreparedListener() {

    @Override
    public void onPrepared(android.media.MediaPlayer mp) {
        Log.e(TAG, "video is prepared");
        progDailog.dismiss();
        vvItemVideo.seekTo(position);
        vvItemVideo.start();
        final MyMediaController mc = new MyMediaController(ItemDetailActivity.this);
        vvItemVideo.setMediaController(mc);
        mc.setAnchorView(vvItemVideo);
        mc.show();
    }
});

Any kind of help is appreciated.

Upvotes: 0

Views: 1069

Answers (1)

Bunny
Bunny

Reputation: 586

If you really don't need to handle the orientation change manually, define your activity like below in manifest.

    <activity
        android:name="youractivity"
        android:configChanges="screenSize|orientation"
        android:launchMode="singleTask" >
    </activity>

This will manage the orientation change and you don't need to code for it.

And use this code in onConfigurationChanged method:

LinearLayout.LayoutParams linear_layoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) {
    linear_layoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);
} else if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT) {
    linear_layoutParams = new LinearLayout.LayoutParams(
    LinearLayout.LayoutParams.MATCH_PARENT, 250);
}
videoView.setLayoutParams(linear_layoutParams);

Upvotes: 1

Related Questions