videoview横shu屏播放
时间: 2023-09-11 21:12:09 浏览: 40
要实现VideoView横屏播放,可以按照以下步骤操作:
1. 在AndroidManifest.xml文件中的<activity>标签中添加android:configChanges="orientation|screenSize"属性,以便在屏幕旋转时不会重启Activity:
```
<activity
android:name=".YourActivity"
android:configChanges="orientation|screenSize">
...
</activity>
```
2. 在布局文件中,将VideoView包含在一个RelativeLayout中,并设置android:layout_width和android:layout_height属性为"match_parent",以充满整个屏幕:
```
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<VideoView
android:id="@+id/video_view"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</RelativeLayout>
```
3. 在Activity中,重写onConfigurationChanged()方法,以便在屏幕旋转时重新设置VideoView的宽高和位置:
```
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
// 横屏
RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.MATCH_PARENT,
RelativeLayout.LayoutParams.MATCH_PARENT);
layoutParams.addRule(RelativeLayout.CENTER_IN_PARENT);
mVideoView.setLayoutParams(layoutParams);
} else {
// 竖屏
RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.MATCH_PARENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
layoutParams.addRule(RelativeLayout.ALIGN_PARENT_TOP);
mVideoView.setLayoutParams(layoutParams);
}
}
```
4. 在代码中播放视频时,可以通过设置VideoView的MediaController来显示控制条,如下所示:
```
mVideoView.setMediaController(new MediaController(this));
mVideoView.setVideoPath("your_video_path");
mVideoView.start();
```
这样,就可以实现VideoView的横屏播放。