4

我正面临这个奇怪的问题,SurfaceViewRenderer每当我在横向/反向横向/传感器横向中运行活动时,预览就会变为空白。奇怪的问题是,在某些手机中,视频预览在反向横向模式和某些手机横向模式下工作。例如,当我在 中运行活动时Android 5.0,横向模式下的视频预览工作正常,但在反向横向模式下却不行。然后我尝试在我正在运行的 Oneplus 手机上做同样的事情Android 10。在这里,SurfaceViewRenderer 中的视频预览在反向横向模式下工作正常,但在横向模式下不行。

注意:即使预览变为空白,远程流也可以正常工作,只有视频预览不起作用。它以某种方式无法更新SurfaceViewRenderer但没有问题流向远程参与者。

<androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".ui.session.activities.LiveSessionActivity">

  
        <org.webrtc.SurfaceViewRenderer
            android:id="@+id/localGLSurfaceView"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:visibility="visible" />

        <View
            android:id="@+id/vBackground"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:alpha="0.4"
            android:background="@android:color/black"
            android:visibility="visible"
            app:layout_constraintBottom_toBottomOf="@+id/localGLSurfaceView"
            app:layout_constraintEnd_toEndOf="@+id/localGLSurfaceView"
            app:layout_constraintHorizontal_bias="0.0"
            app:layout_constraintStart_toStartOf="@+id/localGLSurfaceView"
            app:layout_constraintTop_toTopOf="@+id/localGLSurfaceView"
            app:layout_constraintVertical_bias="0.0" />

        <View
            android:id="@+id/vShadow"
            android:layout_width="wrap_content"
            android:layout_height="@dimen/_42sdp"
            android:background="@drawable/ic_rectangle_shadow"
            app:layout_constraintEnd_toEndOf="@+id/localGLSurfaceView"
            app:layout_constraintStart_toStartOf="@+id/localGLSurfaceView"
            app:layout_constraintTop_toTopOf="@+id/localGLSurfaceView" />

        <ImageView
            android:id="@+id/ivBackButton"
            android:layout_width="@dimen/_8sdp"
            android:layout_height="@dimen/_13sdp"
            android:layout_marginStart="@dimen/_12sdp"
            android:layout_marginTop="@dimen/_16sdp"
            android:src="@drawable/ic_back"
            app:layout_constraintStart_toStartOf="@+id/localGLSurfaceView"
            app:layout_constraintTop_toTopOf="@+id/localGLSurfaceView" />

        <TextView
            android:id="@+id/tvLiveClassName"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginStart="@dimen/_12sdp"
            android:text="Live Class 10"
            android:textColor="@android:color/white"
            android:textSize="@dimen/medium_text"
            app:layout_constraintBottom_toBottomOf="@+id/ivBackButton"
            app:layout_constraintStart_toEndOf="@+id/ivBackButton"
            app:layout_constraintTop_toTopOf="@+id/ivBackButton" />

        <TextView
            android:id="@+id/tvCountDown"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="5"
            android:textColor="@android:color/white"
            android:textSize="@dimen/xxx_large_text"
            app:layout_constraintBottom_toTopOf="@+id/tvTestAudio"
            app:layout_constraintEnd_toEndOf="@+id/localGLSurfaceView"
            app:layout_constraintStart_toStartOf="@+id/localGLSurfaceView" />

        <TextView
            android:id="@+id/tvGoLiveIn"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginBottom="@dimen/_18sdp"
            android:text="Going Live in"
            android:textColor="@android:color/white"
            android:textSize="@dimen/xx_large_text"
            app:layout_constraintBottom_toTopOf="@+id/tvCountDown"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="@+id/localGLSurfaceView" />

        <ImageView
            android:id="@+id/ivStrengthIcon"
            android:layout_width="@dimen/_19sdp"
            android:layout_height="@dimen/_19sdp"
            android:layout_marginTop="@dimen/_14sdp"
            android:layout_marginEnd="@dimen/_12sdp"
            android:src="@drawable/ic_network_strength"
            app:layout_constraintEnd_toEndOf="@+id/localGLSurfaceView"
            app:layout_constraintTop_toTopOf="@+id/localGLSurfaceView" />

        <TextView
            android:id="@+id/tvNetworkStrength"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginEnd="@dimen/_8sdp"
            android:layout_marginBottom="@dimen/_2sdp"
            android:text="Good"
            android:textColor="@android:color/white"
            android:textSize="@dimen/small_text"
            app:layout_constraintBottom_toBottomOf="@+id/ivStrengthIcon"
            app:layout_constraintEnd_toStartOf="@+id/ivStrengthIcon"
            app:layout_constraintTop_toTopOf="@+id/ivStrengthIcon" />

        <Button
            android:id="@+id/btnCancelLive"
            android:layout_width="@dimen/_108sdp"
            android:layout_height="@dimen/_29sdp"
            android:layout_marginBottom="@dimen/_25sdp"
            android:background="@drawable/ic_rectangle_button"
            android:gravity="center"
            android:text="Cancel Live"
            android:textAllCaps="false"
            android:textSize="@dimen/medium_text"
            app:backgroundTint="@null"
            app:layout_constraintBottom_toBottomOf="@+id/localGLSurfaceView"
            app:layout_constraintEnd_toEndOf="@+id/localGLSurfaceView"
            app:layout_constraintStart_toStartOf="@+id/localGLSurfaceView" />

        <TextView
            android:id="@+id/tvTestAudio"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginBottom="@dimen/_12sdp"
            android:text="Speak to test the audio"
            android:textColor="@android:color/white"
            android:textSize="@dimen/medium_text"
            app:layout_constraintBottom_toTopOf="@+id/btnCancelLive"
            app:layout_constraintEnd_toEndOf="@+id/localGLSurfaceView"
            app:layout_constraintStart_toStartOf="@+id/localGLSurfaceView" />

        <androidx.constraintlayout.widget.Group
            android:id="@+id/groupTopView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:visibility="gone"
            app:constraint_referenced_ids="ivBackButton,
            tvLiveClassName,tvNetworkStrength,ivStrengthIcon,vShadow" />

        <androidx.constraintlayout.widget.Group
            android:id="@+id/groupVideoView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:visibility="gone"
            app:constraint_referenced_ids="localGLSurfaceView,
            tvCountDown,tvGoLiveIn,btnCancelLive,tvTestAudio,
            vBackground,preview" />

        <Button
            android:id="@+id/btnJoinSession"
            style="@style/Widget.MaterialComponents.Button.OutlinedButton"
            android:layout_width="@dimen/_130sdp"
            android:layout_height="@dimen/_40sdp"
            android:text="Join Session"
            app:backgroundTint="@color/colorAccent"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

        <include
            android:id="@+id/include"
            layout="@layout/video_bottom_menu_layout"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:visibility="gone"
            app:layout_constraintBottom_toBottomOf="@id/localGLSurfaceView"
            app:layout_constraintEnd_toEndOf="@id/localGLSurfaceView"
            app:layout_constraintStart_toStartOf="@id/localGLSurfaceView" />
    </androidx.constraintlayout.widget.ConstraintLayout>

这就是我初始化视频预览的方式。

private fun initVideoView() {
        val rootEglBase = EglBase.create()
        mLiveSessionBinding.localGLSurfaceView.init(rootEglBase.eglBaseContext, null)
        mLiveSessionBinding.localGLSurfaceView.setScalingType(RendererCommon.ScalingType.SCALE_ASPECT_FILL)
        mLiveSessionBinding.localGLSurfaceView.setMirror(true)
        mLiveSessionBinding.localGLSurfaceView.setEnableHardwareScaler(true)
        mLiveSessionBinding.localGLSurfaceView.setZOrderMediaOverlay(true)
    }

我的摄像机设置以预览和流式传输

public VideoTrack MakeCameraSettingsReady() {
        final EglBase.Context eglBaseContext = EglBase.create().getEglBaseContext();
        PeerConnectionFactory peerConnectionFactory = this.sessionManager.getPeerConnectionFactory();

        // create AudioSource
        AudioSource audioSource = peerConnectionFactory.createAudioSource(new MediaConstraints());
        this.audioTrack = peerConnectionFactory.createAudioTrack("101", audioSource);

        surfaceTextureHelper = SurfaceTextureHelper.create("CaptureThread", eglBaseContext);
        // create VideoCapturer
        VideoCapturer videoCapturer = createCameraCapturer();
        VideoSource videoSource = peerConnectionFactory.createVideoSource(videoCapturer.isScreencast());
        videoCapturer.initialize(surfaceTextureHelper, context, videoSource.getCapturerObserver());
        videoCapturer.startCapture(480, 640, 30);

        return peerConnectionFactory.createVideoTrack("100", videoSource);
    }

这就是我创建相机捕捉器的方式

private VideoCapturer createCameraCapturer() {
        CameraEnumerator enumerator;
        if (Build.VERSION.SDK_INT > Build.VERSION_CODES.LOLLIPOP) {
            enumerator = new Camera2Enumerator(this.context);
        } else {
            enumerator = new Camera1Enumerator(false);
        }
        final String[] deviceNames = enumerator.getDeviceNames();

        // Try to find front facing camera
        for (String deviceName : deviceNames) {
            if (enumerator.isFrontFacing(deviceName)) {
                videoCapturer = enumerator.createCapturer(deviceName, null);
                if (videoCapturer != null) {
                    return videoCapturer;
                }
            }
        }
        // Front facing camera not found, try something else
        for (String deviceName : deviceNames) {
            if (!enumerator.isFrontFacing(deviceName)) {
                videoCapturer = enumerator.createCapturer(deviceName, null);
                if (videoCapturer != null) {
                    return videoCapturer;
                }
            }
        }
        return null;
    }

这是我在清单文件中声明的活动。

<activity android:name=".ui.session.activities.LiveSessionActivity"
            android:configChanges="orientation|screenSize"
            android:screenOrientation="sensorLandscape">

我试图检查CameraX预览是否会发生这种情况。CameraXandroidx.camera.view.PreviewView视频预览中使用在各种方向上都可以正常工作。但是SurfaceViewRenderer视频预览不起作用。

在这方面的任何帮助将不胜感激。提前致谢。

4

0 回答 0