编程语言
首页 > 编程语言> > android-根据exoplayer演示应用程序中视频的长宽比更改表面视图的长宽比

android-根据exoplayer演示应用程序中视频的长宽比更改表面视图的长宽比

作者:互联网

我已使用以下代码在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
        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接口):

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

标签:android-video-player,exoplayer,aspect-ratio,android
来源: https://codeday.me/bug/20191119/2034195.html