问题描述
我遇到了一个奇怪的问题,即每当我在横向/反向横向/传感器横向中运行活动时,SurfaceVieWrenderer
预览都将变为空白。奇怪的问题是,在某些手机中,视频预览在反向横向模式下和某些手机横向模式下工作。例如,当我在Android 5.0
中运行活动时,横向模式下的视频预览可以正常工作,但不能在反向横向模式下运行。然后,我尝试在运行Android 10
的Oneplus手机中执行相同操作。在这里,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
预览是否发生了这种情况。在CameraX
视频预览中使用androidx.camera.view.PreviewView
可以在所有方向上正常工作。但是SurfaceVieWrenderer
视频预览无法正常工作。
在这方面的任何帮助将不胜感激。 预先感谢。
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)