音视频通话

多方视频通话

功能介绍

多方视频通话时,根据当前业务场景合理设置视频编码参数,可以在较低的带宽占用下实现流畅清晰的音视频沟通,下面将针对几种常见场景进行介绍。

1.一对一

  • 画中画布局示例图:

画中画布局示例图

常用于如双人视频聊天场景,双方通常都希望看到对方比较清晰的视频,此时可以使用较高的视频编码分辨率,比如720P或480P。

<template>
	<view class="video-box">
		<rtcsdk-video-view class="myVideo" :usrVideoId="{userId: myUserId}"/>
		<rtcsdk-video-view class="remoteVideo" :usrVideoId="{userId: remoteUserId}" />
	</view>
</template>

<script>
export default {
    data(){
        return {
            myUserId: null,
            remoteUserId: null, //userId可通过成员信息接口获取
        }
    },
    mounted() {
        const cfg = RTCSDK.getVideoCfg();
        cfg.size = {width: 1280, height: 720}; // 设置自己视频分辨率为720P
        RTCSDK.setVideoCfg(cfg);
        RTCSDK.openVideo(this.myUserId);// 打开自己的摄像头
    }
}
</script>

<style lang="scss">
.video-box {
	width: 750rpx;
	flex: 1;
	position: relative;

	.myVideo {
		width: 750rpx;
		flex: 1;
	}
	.remoteVideo {
		position: absolute;
		bottom: 0rpx;
		right: 0rpx;
		width: 144rpx;
		height: 256rpx;
		background-color: #070604;
		.video-ui {
			width: 144rpx;
			height: 256rpx;
		}
	}
}
</style>

相关API请参考:

相关结构定义请参考:

2.多方视频

  • 多方视频示例图:

多方视频示例图

常用于在线教育场景,老师的视频画面比较大,可以使用较高的分辨率比如720P,下面学生的视频画面比较小,应采用较低的视频编码分辨率,比如360P或256P。

  • 示例代码如下:
<template>
	<view class="video-box">
		<view class="mainVideo" v-if="teacherId">
			<rtcsdk-video-view class="video-ui" :usrVideoId="{userId: teacherId}" :key="teacherId" scaleType='1' />
		</view>
		<view class="videoList">
			<view class="video" v-for="item in otherList" :key="item.userId">
				<rtcsdk-video-view class="video-ui" :usrVideoId="{userId: item.userId}" scaleType='1' />
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				memberList: [],
				userInfo: null,
				teacherId: '',
			};
		},
		computed: {
			otherList() {
				const list = this.memberList.filter(item => item.userId !== this.teacherId);
				if (list.length > 4) list.length = 4;	//只取4个用户
				return list;
			}
		},
		onLoad() {
			// SDK插件
			this.RTCSDK = getApp().globalData.RTCSDK.getInstance();
			//自己的用户信息
			this.userInfo = getApp().globalData.userInfo;
			this.memberList = this.RTCSDK.getAllMembers();
    
			this.teacherId = this.memberList[0].userId; //默认第一个位是主视频

			//每五秒切换teacherId,测试主视频轮换效果
			setInterval(() => {
				this.memberList.some((item, index) => {
					if (item.userId === this.teacherId) {
						if (index === this.memberList.length - 1) {
							this.teacherId = this.memberList[index === this.memberList.length - 1 ? 0 : index + 1].userId;
						} else {
							this.teacherId = this.memberList[index + 1].userId;
						}
						return true;
					}
				})
			}, 5000);
		},
		mounted() {
			//获取前后置摄像头
			const devicePosition = this.RTCSDK.getMyVideoPosition();
			//将前置摄像头设置为默认摄像头
			this.RTCSDK.setDefaultVideo(this.userInfo.userId, devicePosition.front);

			//打开自己的摄像头麦克风
			this.RTCSDK.openVideo(this.userInfo.userId);
			this.RTCSDK.openMic(this.userInfo.userId);
		}
	}
</script>

<style lang="scss">
	.video-box {
		flex: 1;
		background-color: #1d232f;
		position: relative;

		.mainVideo {
			flex: 1;

			.video-ui {
				flex: 1;
				width: 750rpx;
			}
		}

		.videoList {
			flex-wrap: wrap;
			flex-direction: row;
			height: 320rpx;
			justify-content: space-around;

			.video {
				background-color: #000;

				.video-ui {
					width: 180rpx;
					height: 320rpx;
				}
			}
		}
	}
</style>

相关API请参考: