public VideoView mVideoView; //声明VideoView播放类公有对象mVideoView public MediaController mMediaController; //声明MediaController播放接口操作类公有对象mMediaController mVideoView = new VideoView(this); //在内存中创建播放对象mVideoView mVideoView = findViewById(R.id.video); //将播放容器控件ID地址赋给播放对象mVideoView mMediaController = new MediaController(this); //在内存中新建播放接口操作对象mMediaController if (aaa == 2131230818) { andx.setText("下个"); //将上曲按钮文字恢复初始状态 mVideoView.suspend(); //释放先前视频播放加载的资源 if(bfyybh == 22) //如果音乐文件编号等于15顺序运行缩进语句 { andx.setText("首个"); //设置下曲按钮文字为"首曲" bfyybh = 0; //音乐文件编号数值恢复初始值0 } bfyybh++; //音乐文件编号数值加1 textdx1.setText("视频编号:" + bfyybh ); // 显示音乐曲目编号 String uri = "/storage/emulated/0/musics/MP4/" + bfyybh + ".mp4"; //指定mp4视频文件绝对路径 mVideoView.setVideoURI(Uri.parse(uri)); //将视频文件绝对路径赋给播放对象mVideoView mVideoView.start(); //调用start方法播放打开的视频文件 } 我上面视频能在播放窗口容器中播放,但视频充满窗口长款比例失调,怎样改成按视频原来长宽比例在窗口中播放 ?
时间: 2024-04-09 18:28:41 浏览: 53
您可以尝试在VideoView上设置一个AspectRatioFrameLayout来保持视频的原始长宽比例。下面是一个示例代码:
```java
mVideoView = new VideoView(this);
AspectRatioFrameLayout frameLayout = findViewById(R.id.video_frame_layout);
frameLayout.addView(mVideoView);
// 设置视频URI和MediaController
mVideoView.setVideoURI(Uri.parse(uri));
mVideoView.setMediaController(mMediaController);
// 监听视频尺寸改变事件
mVideoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
@Override
public void onPrepared(MediaPlayer mediaPlayer) {
// 获取视频宽度和高度
int videoWidth = mediaPlayer.getVideoWidth();
int videoHeight = mediaPlayer.getVideoHeight();
// 获取窗口宽度和高度
int windowWidth = frameLayout.getWidth();
int windowHeight = frameLayout.getHeight();
// 计算视频宽高比
float videoAspectRatio = (float) videoWidth / videoHeight;
// 计算调整后的视频宽度和高度
int adjustedWidth, adjustedHeight;
if (videoAspectRatio > windowWidth / (float) windowHeight) {
adjustedWidth = windowWidth;
adjustedHeight = (int) (windowWidth / videoAspectRatio);
} else {
adjustedWidth = (int) (windowHeight * videoAspectRatio);
adjustedHeight = windowHeight;
}
// 设置VideoView的尺寸
ViewGroup.LayoutParams layoutParams = mVideoView.getLayoutParams();
layoutParams.width = adjustedWidth;
layoutParams.height = adjustedHeight;
mVideoView.setLayoutParams(layoutParams);
}
});
// 开始播放视频
mVideoView.start();
```
在上面的代码中,我们使用了一个AspectRatioFrameLayout作为VideoView的父容器,并通过计算视频的宽高比来调整VideoView的尺寸,以保持视频的原始长宽比例。您需要将`R.id.video_frame_layout`替换为您实际使用的父容器的ID。
阅读全文