4

在 TextureView 上播放视频流时,我在 Galaxy S4 和 Nexus4 中看到了不同的行为。

视频源是 Android 以纵向模式录制的,因此视频旋转了 90 度。(Android 相机的默认方向是横向,因此要拍摄纵向视频,我们需要将视频顺时针旋转 90 度)所以在播放时我首先检查视频的方向(通过测量宽度和高度)以及宽度是否大于高度, 通过下面的代码将 TextureView 旋转 90 度。

MediaMetadataRetriever mediaMetadataRetriever = new MediaMetadataRetriever();
mediaMetadataRetriever.setDataSource(url, new HashMap<String, String>());
String heightString = mediaMetadataRetriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_VIDEO_HEIGHT);
String widthString = mediaMetadataRetriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_VIDEO_WIDTH);

int videoHeight = Integer.parseInt(heightString);
int videoWidth = Integer.parseInt(widthString);

FrameLayout.LayoutParams l;
DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);

if(videoWidth > videoHeight){
    Log.d(TAG, "Need to rotate by 90");

    l = new FrameLayout.LayoutParams(metrics.heightPixels, metrics.widthPixels, Gravity.CENTER_HORIZONTAL | Gravity.CENTER_VERTICAL);
    textureView.setRotation(90);            
}else{   
    l = new FrameLayout.LayoutParams(metrics.widthPixels, metrics.heightPixels, Gravity.CENTER_HORIZONTAL | Gravity.CENTER_VERTICAL);
}       

textureView.setLayoutParams(l);

这适用于 Galaxy S4 或 Xepria Z,但不适用于 Nexus4 或 Galaxy S2。似乎在 Nexus4 中,TextureView 将视频自动旋转 90 度,因此与下面的代码一起,视频最终旋转了 180 度。

现在我无法管理设备之间的这种行为差异。有没有人有相同的经验并且知道如何解决它,非常感谢您的帮助。

4

0 回答 0