我目前正在使用 Exoplayer 完成一个项目,并希望实现类似于 ImageView 中所见的“CenterCrop”类型属性在视频中。基本上它将适应 SurfaceView 的高度,保持相同的宽高比,并裁剪超出屏幕的两侧边缘。我的布局看起来像这样:
<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>
我的一个同事已经用包装器编写了大部分创建和启动播放器的代码,基本上看起来像下面这样:
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);
这段代码接受一个URI并进行一些设置,播放视频一切正常。在研究了裁剪之后,我找到了“setVideoScalingMode”,并将其添加为上述代码的最后一行。不幸的是,它没有起到任何作用。有谁知道如何操作以及我可以调整什么来使其正常工作吗?谢谢!
SurfaceView
吗?TextureView
具有一个很好的功能setTransform(Matrix)
,可以让你完全控制缩放/平移/旋转。 - pskinkVIDEO_SCALING_MODE_SCALE_TO_FIT_WITH_CROPPING
的描述有些误导,因为它没有说明如何裁剪内容以适应表面。 - pskink