实现音频和视频通话的 Android 单应用程序 - 4. 代码
最编程
2024-04-13 15:53:02
...
4.1 集成插件
const RtcModule = uni.requireNativePlugin('AR-RtcModule');
- AR-RtcModule:插件名称,首页集成插件
4.2 初始事件回调
//callback 接收
callbackFn() {
RtcModule.setCallBack((res) => {
switch (res.engineEvent) {
case "onWarning":
this.promptFn("warn", res.warningCode);
break;
case "onError":
res.errorCode != 18 ? this.promptFn("error", res.errorCode) : '';
break;
case "onJoinChannelSuccess": //用户加入成功
uni.hideLoading();
this.role == 1 ? this.PeerVideoUser.push(res.uid) : "";
this.videoShow = true;
setTimeout(() => {
// this.videoShowBg = false;
this.promptText = ""
//扬声器
RtcModule.setEnableSpeakerphone({
"enabled": true
}, (res) => {
})
setTimeout(() => {
// 启用视频模块。
this.role == 1 ? this.setupLocalVideoFn() : RtcModule.enableVideo((res) => {
});
}, 200)
}, 2000)
break;
case "onLeaveChannel": //离开频道回调
setTimeout(() => {
this.closeAll()
}, 500)
break;
case "onUserJoined": //远端用户加入当前频道回调。
// this.promptFn("info", "远端用户加入当前频道回调");
this.PeerVideoUser.push(res.uid);
break;
case "onUserOffline": //远端用户离开当前频道回调。
this.PeerVideoUser = this.PeerVideoUser.filter((x) => x !== res.uid);
break;
case "onFirstLocalAudioFrame": //已发送本地音频首帧的回调。(页面上添加音频)
break;
case "onFirstLocalVideoFrame": //已显示本地视频首帧的回调。(页面添加本地视频)
// this.promptFn("error", "已显示本地视频首帧的回调");
break;
case "onFirstRemoteVideoDecoded": //已完成远端视频首帧解码回调。(页面添加远端视频)
// this.promptFn("info", "已完成远端视频首帧解码回调");
this.promptText = "请稍等。。。"
let uid = []
uid.push(res.uid)
setTimeout(() => {
this.promptText = "";
// this.videoShowBg = false; //设置背景开关
setTimeout(() => {
uid.map(item => {
this.$refs[`popup${
item}`][0].setupRemoteVideo({
"renderMode": 1,
"channelId": this.chanel,
"uid": item,
"mirrorMode": 0
}, (res) => {
})
//预览
RtcModule.startPreview((res) => {
});
})
}, 500)
}, 2000)
break;
}
})
},
- res.engineEvent:接收各种回调,加入频道成功,离开频道,错误码等。
4.3 创建实例
await RtcModule.create({
"appId": this.appid //anyRTC 为 App 开发者签发的 App ID。每个项目都应该有一个独一无二的 App ID。如果你的开发包里没有 App ID,请从anyRTC官网(https://www.anyrtc.io)申请一个新的 App ID
}, (res) => {
});
4.4 设置角色
setClientRole(num) {
this.role = num;
//设置直播场景下的用户角色
RtcModule.setClientRole({
"role": Number(num) //1:为主播,2:游客
}, (ret) => {
});
},
4.5 加入频道
await RtcModule.joinChannel({
"token": "",
"channelId": this.channel,
"uid": this.uid
}, (res) => {
})
- token: 注册不开启token验证,可以为空着。
- channelId: 你需要加入的频道ID。
- uid: 每个用户分配唯一UID,不能重复。
推荐阅读
-
实现音频和视频通话的 Android 单应用程序 - 4. 代码
-
Android 应用程序]模仿微信视频通话的真实项目(含源代码和演示)
-
实时音频和视频技术的发展与应用-1.1 双重音频和视频 从架构上看,双人音视频系统相对简单明了。红点代表房间信令服务,房间信令服务的主要功能是管理房间信息,实现容量协商和上下行链路的质量调节,例如当下行信道发生拥塞时,上行线路的码率和分辨率会降低。 在传输信道层面,我们的策略是优先直连,在跨区域、跨运营商的情况下,我们会选择单中转或双中转信道,在策略上尽量保持直连和中转信道同时存在,当其中一个信道的质量不好时,系统会自动切断到另一个信道的流量。 1.2 多人音视频 多人视频通话的产品形态是整个房间不超过 50 人,大盘平均房间规模约为 4.x 人,房间内部最多满足一个大视频和三个小视频(四屏)。根据这一条件,我们在架构中采用了典型的 SFU 小房间设计。 上图中的红点代表房间信令服务,主要用于房间管理和状态信息同步。房间管理主要包括用户列表的管理,例如哪些用户打开了视频/音频,我看了谁,谁看了我,这些都是基于房间管理的信息,然后房间信令服务会将这些信息同步到媒体传输服务进行数据分发。 房间服务的另一个作用是房间级容量协商和质量控制,例如,房间里的每个人一开始都支持 H.265 编码,当某个时刻进来一个只支持 H.264 编码的用户时,房间里所有的上游主播就必须把 H.265 切成 H.264。还有一种情况是,房间里有一定比例的人下行链路信道质量较差,这会导致上行链路房间质量下降。 在传输层面,我们采用的是单层分布式媒体传输网络,大家都选择中转方式,不区分双人和多人,采用 Full-Mesh 传输机制将所有数据推送过去,比如一个节点上的人并不都看另外两个人的视频,但还是会将视频推送给他们。