surfaceView改变视频源的宽高和方向
时间: 2023-12-14 08:36:40 浏览: 153
android使用surfaceview+MediaPlayer播放视频
要改变SurfaceView的视频源的宽高和方向,您需要在代码中进行以下操作:
1. 获取SurfaceView的Holder对象:
```java
SurfaceView surfaceView = findViewById(R.id.surfaceView);
SurfaceHolder holder = surfaceView.getHolder();
```
2. 设置SurfaceView的宽高和格式:
```java
holder.setFixedSize(width, height);
holder.setFormat(PixelFormat.RGBA_8888);
```
3. 通过SurfaceHolder.Callback监听SurfaceView的状态变化,例如SurfaceView创建、销毁、改变大小等事件:
```java
holder.addCallback(new SurfaceHolder.Callback() {
@Override
public void surfaceCreated(SurfaceHolder holder) {
// SurfaceView创建时的操作
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
// SurfaceView改变大小时的操作
}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
// SurfaceView销毁时的操作
}
});
```
4. 在SurfaceView创建时,获取MediaPlayer对象,并将其与SurfaceView关联起来:
```java
MediaPlayer mediaPlayer = new MediaPlayer();
mediaPlayer.setDataSource("your_video_path");
mediaPlayer.setDisplay(holder);
mediaPlayer.prepareAsync();
```
5. 在SurfaceView大小改变时,重新设置MediaPlayer的宽高和方向:
```java
mediaPlayer.reset();
mediaPlayer.setDataSource("your_new_video_path");
mediaPlayer.setDisplay(holder);
mediaPlayer.setVideoScalingMode(MediaPlayer.VIDEO_SCALING_MODE_SCALE_TO_FIT_WITH_CROPPING);
mediaPlayer.prepareAsync();
```
注意:设置视频的宽高和方向需要根据实际情况进行调整,代码中的示例仅供参考。
阅读全文