一、前言
類似微信視頻通話中點(diǎn)擊小窗口時企巢,本地視頻和遠(yuǎn)端視頻切換枫慷。可惜這簡單的功能聲網(wǎng)SDK也是沒有提供包斑,得自己做流礁,好在也不難。
二罗丰、先看下設(shè)計(jì)圖
view_move.png
三神帅、解決思路
本地視頻和遠(yuǎn)端視頻各聲明一個SurfaceView,互換時就把它們add到對應(yīng)的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) //每次給了其他控件,需要調(diào)這個方法
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
}
}
}
還有其他聲網(wǎng)音視頻SDK功能的問題绍填,可私信我