我已使用以下代码在Android应用中实施了ExoPlayer。
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);
然而问题是我需要播放所有方面比率的视频。如何调整surfaceView以适应视频的大小而不是相反?
答案 0 :(得分:3)
请记住,我在AndroidManifest中使用android:screenOrientation="sensorLandscape"
作为我项目中包含ExoPlayer的Activity,这是我用来根据视频的宽高比更改SurfaceView的宽高比的方式。
1. 将SurfaceView放入容器(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. 根据视频的宽高比更改容器尺寸:
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. 将此applyAspectRatio()
方法放入活动或片段的onPlayerStateChanged
回调方法(必须实现ExoPlayer.EventListener
接口):
@Override
public void onPlayerStateChanged(boolean playWhenReady, int playbackState) {
if (playbackState == ExoPlayer.STATE_READY) applyAspectRatio();
}