我目前正在一个项目中使用 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”。不幸的是,这什么都不做。有没有人知道这应该如何完成以及我可以调整什么来让它工作?谢谢!
最佳答案
我知道这个很晚才回答。经过大量的头脑 Storm ,研究 EXOPlayer 源代码以实现此功能后,我自己弄明白了,我所做的只是添加以下行,它就像一个魅力:
mVideoView.setResizeMode(AspectRatioFrameLayout.RESIZE_MODE_ZOOM); //This will increase video's height or width to fit with maintaining aspect ratios of video.
并删除下面提到的其他两个选项:
mExoplayer.setVideoScalingMode(C.VIDEO_SCALING_MODE_SCALE_TO_FIT_WITH_CROPPING);
mVideoView.setResizeMode(AspectRatioFrameLayout.RESIZE_MODE_FILL)
希望这对其他人有帮助。
关于android - 使用 SurfaceView 居中裁剪 Exoplayer,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42494705/