【问题标题】:VideoView plays in portrait mode but not in landscape modeVideoView 以纵向模式播放,但不以横向模式播放
【发布时间】:2015-07-15 15:33:05
【问题描述】:

我创建了两种布局,一种是普通布局,一种是横向布局。下面是两个方向的VideoView xml。视频从 URL 流式传输。视频在纵向模式下完美播放,但是当将方向更改为横向模式时,视频不会播放,它只会继续向我显示ProgressDialog。下面的代码可能有助于理解我的问题。

正常布局:

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

横向布局:

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

用于执行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();
    }
});

感谢任何形式的帮助。

【问题讨论】:

标签: android android-videoview


【解决方案1】:

如果您真的不需要手动处理方向更改,请在清单中定义如下所示的活动。

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

这将管理方向更改,您无需为此编写代码。

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

【讨论】:

  • 是的,但在这种情况下会发生什么情况是视频不会变成全屏,当它们是纵向时宽度和高度相同
  • 是的,因为您需要纵向模式下的视频高度为“250”。你可以做的是,你可以在onConfigurationChanged()方法中改变videoView的高度。
  • 我从 kushan2 的评论中得到了答案,但我想知道你会怎么做
  • 那么你在尝试我的建议吗?
  • 是的,我会在onConfigurationChanged()中解释如何做到这一点
猜你喜欢
  • 2019-01-27
  • 2013-03-27
  • 2012-11-04
  • 1970-01-01
  • 1970-01-01
  • 2012-10-18
  • 2014-11-21
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多