I am currently using Exoplayer in a project and am looking to achieve a "CenterCrop" type attribute similar on the video to what you might see in an imageview. Basically it will fit to the height of the surfaceview, keep the same aspect ratio, and crop the edges on the side that go off the screen. My layout looks something like this:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/videoPlayerContainer"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="404dp">
<com.google.android.exoplayer2.ui.SimpleExoPlayerView
android:id="@+id/videoPlayer"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clickable="true"
app:controller_layout_id="@layout/empty_controls">
<SurfaceView
android:id="@+id/surfaceView"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</com.google.android.exoplayer2.ui.SimpleExoPlayerView>
<TextView
android:id="@+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Text"
android:background="@android:color/white"/>
</FrameLayout>
Most of the code to create and start the player was already set up by a colleague in a wrapper and basically looks like the following:
BandwidthMeter bandwidthMeter = new DefaultBandwidthMeter();
TrackSelection.Factory videoTrackSelectionFactory = new AdaptiveVideoTrackSelection.Factory(bandwidthMeter);
trackSelector = new DefaultTrackSelector(videoTrackSelectionFactory);
LoadControl loadControl = new DefaultLoadControl();
simpleExoPlayer = ExoPlayerFactory.newSimpleInstance(this.context, trackSelector, loadControl);
simpleExoplayer.setSurfaceView(surfaceView);
DataSource.Factory mediaDataSourceFactory = new DefaultDataSourceFactory(context, "Agent");
MediaSource videoSource = new HlsMediaSource(uri, mediaDataSourceFactory, new Handler(), null);
simpleExoplayer.addListener(this);
simpleExoplayer.prepare(videoSource);
simpleExoplayer.setPlayWhenReady(true);
//this is supposedly used to set the scaling mode
simpleExoplayer.setVideoScalingMode(C.VIDEO_SCALING_MODE_SCALE_TO_FIT_WITH_CROPPING);
This code takes in a uri does some setup and plays the video, which is all working fine. After doing some research on cropping, I found the "setVideoScalingMode" which I added as the last line in the code above. This unfortunately does nothing. Does anyone have any idea on how this is supposed to be done and what I can adjust to get it to work? Thanks!
SurfaceView
?TextureView
has a nice feature ofsetTransform(Matrix)
that gives you a full control for scaling/translating/rotating – StrodeVIDEO_SCALING_MODE_SCALE_TO_FIT_WITH_CROPPING
description is somehow misleading as it does not say how the content is cropped to fit the surface – Strode