Android – change the aspect ratio of the surface view according to the aspect ratio of the video in the exoplayer demo application
I have implemented exoplayer in the Android application using the following code
String versionName;
try {
String packageName = getPackageName();
PackageInfo info = getPackageManager().getPackageInfo(packageName, 0);
versionName = info.versionName;
} catch (PackageManager.NameNotFoundException e) {
versionName = "?";
}
player = ExoPlayer.Factory.newInstance(2);
Allocator allocator = new DefaultAllocator(BUFFER_SEGMENT_SIZE);
DataSource dataSource = new DefaultUriDataSource(this, null, versionName);
MediaPresentationDescriptionParser parser = new MediaPresentationDescriptionParser();
UriDataSource manifestDataSource = new DefaultUriDataSource(this, versionName);
ManifestFetcher<MediaPresentationDescription> manifestFetcher = new ManifestFetcher<>(passedWorkoutObject.workoutMediaURL, manifestDataSource, parser);
ExtractorSampleSource sampleSource = new ExtractorSampleSource(
Uri.parse(VIDEO_URL), dataSource, allocator, BUFFER_SEGMENT_COUNT * BUFFER_SEGMENT_SIZE);
MediaCodecVideoTrackRenderer videoRenderer = new MediaCodecVideoTrackRenderer(
this, sampleSource, MediaCodec.VIDEO_SCALING_MODE_SCALE_TO_FIT_WITH_CROPPING);
MediaCodecAudioTrackRenderer audioRenderer = new MediaCodecAudioTrackRenderer(sampleSource);
player.prepare(videoRenderer, audioRenderer);
player.sendMessage(videoRenderer, MediaCodecVideoTrackRenderer.MSG_SET_SURFACE, surface);
player.setPlayWhenReady(true);
control = new PlayerControl(player);
But the problem is that I need to play all aspect ratio videos. How to adjust the surfaceview to fit the video size, not the opposite?
resolvent:
Please remember that in my project, I use Android: screenorientation = "sensorlandscape" in androidmanifest for the activity containing exoplayer, which is a method to change the aspect ratio of surfaceview according to the aspect ratio of video
1. Put the surfaceview into the container whose size will be changed (FrameLayout):
<FrameLayout
android:id="@+id/rootView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/colorBlack"
android:focusable="true"
android:keepScreenOn="true">
<FrameLayout
android:id="@+id/video_frame"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center">
<SurfaceView
android:id="@+id/surfaceView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center" />
</FrameLayout>
</FrameLayout>
2. Change the size of the container according to the aspect ratio of the video:
private void applyAspectRatio(FrameLayout container, SimpleExoPlayer exoPlayer) {
float videoRatio = (float) exoPlayer.getVideoFormat().width/exoPlayer.getVideoFormat().height;
Display display = getActivity().getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
float displayRatio = (float) size.x/size.y;
if (videoRatio > displayRatio) {
Log.d(TAG, "applying " + videoRatio + "aspectRatio");
container.getLayoutParams().height = Math.round(container.getMeasuredWidth()/videoRatio);
container.requestLayout();
} else {
Log.d(TAG, "applying usual aspectRatio");
FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
params.gravity = CENTER;
container.setLayoutParams(params);
}
}
3. Put this applyaspectratio() method into the onplayerstatechanged callback method of activity or fragment (the exoplayer. EventListener interface must be implemented):
@Override
public void onPlayerStateChanged(boolean playWhenReady, int playbackState) {
if (playbackState == ExoPlayer.STATE_READY) applyAspectRatio();
}