TextureView autorotates video stream on Nexus4 (but not on Galaxy S4)

When playing a video stream in TextureView, I saw different behaviors in Galaxy S4 and Nexus4.

The video source is recorded by Android in portrait mode, so the video is rotated 90 degrees. (The default orientation for the Android camera is landscape, so to capture the video in the portrait, we need to rotate the video 90 degrees clockwise) Therefore, when playing back, I first check the orientation of the video (by measuring the width and height), and if the width is greater than the height, turn TextureView is 90 degrees below the code.

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); 

This works on the Galaxy S4 or Xepria Z, but does not work on the Nexus4 or Galaxy S2. It seems that in Nexus4 TextureView autorotates the video 90 degrees, therefore, together with the below code, the video is rotated 180 degrees at the end.

Right now I have no way to manage this difference in behavior between devices. Someone has the same experience and knows how to solve it, I really appreciate your help.

+4
source share

Source: https://habr.com/ru/post/1498680/


All Articles