【发布时间】:2018-09-06 04:07:28
【问题描述】:
我使用TextureView 播放视频:
录制视频时我必须使用rotation=90 来显示视频正确的方向
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#777777" >
<TextureView
android:id="@+id/textureView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:rotation="90"
android:layout_alignParentTop="true" />
我尝试设置 TextureView 的 setTransform 以将视频大小调整为全屏,但它不起作用:
@Override
public void onSurfaceTextureAvailable(SurfaceTexture surfaceTexture, int width, int height) {
Surface surface = new Surface(surfaceTexture);
try {
mMediaPlayer = new MediaPlayer();
mMediaPlayer.setDataSource(FILE_NAME);
mMediaPlayer.setSurface(surface);
mMediaPlayer.setLooping(false);
mMediaPlayer.prepareAsync();
updateTextureViewScaling(width,height);
// Play video when the media source is ready for playback.
mMediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
@Override
public void onPrepared(MediaPlayer mediaPlayer) {
mediaPlayer.start();
}
});
} catch (IllegalArgumentException e) {
Log.d(TAG, e.getMessage());
} catch (SecurityException e) {
Log.d(TAG, e.getMessage());
} catch (IllegalStateException e) {
Log.d(TAG, e.getMessage());
} catch (IOException e) {
Log.d(TAG, e.getMessage());
}
}
private void updateTextureViewScaling(int viewWidth, int viewHeight) {
float scaleX = 1.0f;
float scaleY = 1.0f;
DisplayMetrics displayMetrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
int VIDEO_HEIGHT = displayMetrics.heightPixels;
int VIDEO_WIDTH = displayMetrics.widthPixels;
if (VIDEO_WIDTH > viewWidth && VIDEO_WIDTH > viewHeight) {
scaleX = (float) VIDEO_WIDTH / viewWidth;
scaleY = (float) VIDEO_HEIGHT / viewHeight;
} else if (VIDEO_WIDTH < viewWidth && VIDEO_HEIGHT < viewHeight) {
scaleY = (float) viewWidth / VIDEO_WIDTH;
scaleX = (float) viewHeight / VIDEO_HEIGHT;
} else if (viewWidth > VIDEO_WIDTH) {
scaleY = ((float) viewWidth / VIDEO_WIDTH) / ((float) viewHeight / VIDEO_HEIGHT);
} else if (viewHeight > VIDEO_HEIGHT) {
scaleX = ((float) viewHeight / VIDEO_WIDTH) / ((float) viewWidth / VIDEO_WIDTH);
}
// Calculate pivot points, in our case crop from center
int pivotPointX = viewWidth / 2;
int pivotPointY = viewHeight / 2;
Matrix matrix = new Matrix();
matrix.setScale(scaleX, scaleY, pivotPointX, pivotPointY);
textureView.setTransform(matrix);
}
目标结果:
图 1:未设置 android:rotation="90"
图 2:设置 android:rotation="90"
如何将视频的宽度和高度设置为全屏: 我有日志: onSurfaceTextureAvailable 中的宽度和高度 = 屏幕大小。
【问题讨论】:
-
我将 updateTextureViewScaling 更改为您的 setupMatrix,视频无法显示。
-
必须使用android:rotation="90"?
-
我使用了 setupMatrix(width,height,90);,但它无法正常工作。
-
添加 android:screenOrientation="sensor"
-
它在哪里添加?
标签: android