gpt4 book ai didi

android - 根据 exoplayer 演示应用程序中视频的纵横比更改表面 View 的纵横比

转载 作者:太空宇宙 更新时间:2023-11-03 13:51:08 27 4
gpt4 key购买 nike

我已经使用以下代码在我的 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 以适应视频的大小而不是相反?

最佳答案

请记住,我在 AndroidManifest 中使用 android:screenOrientation="sensorLandscape" 作为我项目中包含 ExoPlayer 的 Activity,这是我用于更改 SurfaceView 纵横比的方法与视频的纵横比。

1. 将 SurfaceView 放入容器(FrameLayout)中,容器(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()方法放到Activity或Fragment的onPlayerStateChanged回调方法中(必须实现ExoPlayer. EventListener接口(interface)):

@Override
public void onPlayerStateChanged(boolean playWhenReady, int playbackState) {
if (playbackState == ExoPlayer.STATE_READY) applyAspectRatio();
}

关于android - 根据 exoplayer 演示应用程序中视频的纵横比更改表面 View 的纵横比,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34739779/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com