接入声网音视频SDK-点击小屏,本地视频和远端视频切换

一、前言

类似微信视频通话中点击小窗口时,本地视频和远端视频切换。可惜这简单的功能声网SDK也是没有提供,得自己做,好在也不难。

二、先看下设计图

view_move.png

三、解决思路

本地视频和远端视频各声明一个SurfaceView,互换时就把它们add到对应的FrameLayout里面。

四、示例代码

private fun setupLocalVideo(isCloseCamera:Boolean,fl:FrameLayout? = mBinding.flVideoCallLocalVideo) {
    LogUtils.i("setupLocalVideo","setupLocalVideo")
    if (isMBindingViewInitialised()){
        fl?.let {
            if (isCloseCamera){
                it.removeAllViews()
                it.addView(LayoutInflater.from(mContext).inflate(R.layout.layout_close_local_video,it,false))
            } else {
                it.removeAllViews()
                surfaceViewLocal = null
                surfaceViewLocal = RtcEngine.CreateTextureView(mContext)
                mVideoManager?.setLocalPreview(surfaceViewLocal)
                //surfaceViewLocal!!.setZOrderMediaOverlay(true)
                it.addView(surfaceViewLocal)
                // Initializes the local video view.
                // RENDER_MODE_FIT: Uniformly scale the video until one of its dimension fits the boundary. Areas that are not filled due to the disparity in the aspect ratio are filled with black.
                //rtcEngine?.setupLocalVideo(VideoCanvas(surfaceViewLocal, VideoCanvas.RENDER_MODE_HIDDEN, SPUtil.getUserId()))
                surfaceViewLocal?.tag = SPUtil.getUserId()

            }
        }

        
    }
}



private fun setupRemoteVideo(uid: Int,fl: FrameLayout? = mBinding.flVideoCallRemoteVideo,surfaceViewRemote1:SurfaceView? = null) {
    if (isMBindingViewInitialised()){
        fl?.let {
            if (it.childCount >= 1) {
                return
            }
            it.removeAllViews()
            if (surfaceViewRemote1 == null){
                surfaceViewRemote = RtcEngine.CreateRendererView(mContext)
                it.addView(surfaceViewRemote)
                // Initializes the video view of a remote user.
                rtcEngine?.setupRemoteVideo(VideoCanvas(surfaceViewRemote, VideoCanvas.RENDER_MODE_HIDDEN, uid))
                surfaceViewRemote!!.tag = uid // for mark purpose
            } else {
                surfaceViewRemote = surfaceViewRemote1
                it.addView(surfaceViewRemote)
                surfaceViewRemote!!.tag = uid
            }
        }
    }
}

mBinding.flVideoCallLocalVideo.setOnClickListener {
        if (!mBinding.tvChatVideoAcceptingCameraClose.isSelected && surfaceViewLocal != null && surfaceViewRemote != null) {

            if (localViewSmall) {
                mBinding.flVideoCallRemoteVideo.removeView(surfaceViewRemote)
                mBinding.flVideoCallLocalVideo.removeView(surfaceViewLocal)

                mBinding.flVideoCallRemoteVideo.removeAllViews()
                mBinding.flVideoCallRemoteVideo.addView(surfaceViewLocal)
                mVideoManager?.setLocalPreview(surfaceViewLocal) //每次给了其他控件,需要调这个方法
                mBinding.flVideoCallLocalVideo.removeAllViews()
                mBinding.flVideoCallLocalVideo.addView(surfaceViewRemote)
                localViewSmall = false
            } else {
                mBinding.flVideoCallRemoteVideo.removeView(surfaceViewLocal)
                mBinding.flVideoCallLocalVideo.removeView(surfaceViewRemote)

                mBinding.flVideoCallRemoteVideo.removeAllViews()
                mBinding.flVideoCallRemoteVideo.addView(surfaceViewRemote)
                mBinding.flVideoCallLocalVideo.removeAllViews()
                mBinding.flVideoCallLocalVideo.addView(surfaceViewLocal)
                mVideoManager?.setLocalPreview(surfaceViewLocal)
                localViewSmall = true
            }
        }
    }

还有其他声网音视频SDK功能的问题,可私信我

你可能感兴趣的:(接入声网音视频SDK-点击小屏,本地视频和远端视频切换)