I have implemented ExoPlayer in my Android app 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);
The problem however is that I need to play videos of all aspect Ratios. How Do I adjust the surfaceView to fit the size of the video instead of the other way around?
Keeping in mind that I use android:screenOrientation="sensorLandscape" in AndroidManifest for the Activity that contains ExoPlayer in my project, here is the way that I used for changing the aspect ratio of SurfaceView according to aspect ratio of the video.
1. Place SurfaceView into container (FrameLayout) size of which will be changed:
<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 size of container according to 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 onPlayerStateChanged callback method of Activity or Fragment (which has to implement ExoPlayer.EventListener interface):
@Override
public void onPlayerStateChanged(boolean playWhenReady, int playbackState) {
if (playbackState == ExoPlayer.STATE_READY) applyAspectRatio();
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With