简体   繁体   English

VideoView以纵向模式播放,但不以横向模式播放

[英]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. 以下是两个方向的VideoView xml。 The video is streamed from an URL. 视频是从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 . 该视频在纵向模式下可以完美播放,但是当将方向更改为横向模式时,则不会播放视频,而是继续向我显示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 : 用于执行VideoView Java代码:

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. 任何帮助都将受到赞赏。

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: 并在onConfigurationChanged方法中使用以下代码:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM