uni-app技术分享| 10分钟实现一个简易uniapp视频通话
视频讲解
创建 uniapp 项目
- 创建 uniapp 项目
- 前往 anyRTC 控制台-项目管理 创建新项目,获取 appid
引入插件
- 前往 uniapp插件市场搜索 anyRTC,选中anyRTC音视频SDK插件
- 云打包购买插件(免费引入)引入创建的对应uniapp 项目
- uniapp项目的
manifest.json
中App原生插件配置
选择云端插件选中
anyRTC音视频SDK插件` - 打包自定义基座
- 选择自定义基座运行
代码逻辑
必须在
nvue
页面中
<view class="content">
<button class="button" type="primary" @click="stepOne">步骤一:初始化回调</button>
<button class="button" type="primary" @click="stepTwo">步骤二:初始化实例</button>
<button class="button" type="primary" @click="stepThree">步骤三:启用视频模块</button>
<button class="button" type="primary" @click="stepFour">步骤四:加入频道房间</button>
<view class="video">
本地用户 {{localuid}} 音视频渲染
<AR-CanvasView ref="location" style="flex: 1;" />
</view>
<view class="video">
远端 {{remotenableuid}} 音视频渲染
<AR-CanvasView ref="remotenable" style="flex: 1;" />
</view>
</view>
// rtc 音视频引入
const rtcModule = uni.requireNativePlugin('AR-RtcModule');
export default {
data() {
return {
appid: "177e21c0d1641291c34e46e1198bd49a",
channel: "123456",
localuid: "", // 本地用户
remotenableuid: "", // 远端用户
}
},
onLoad() {
},
methods: {
// 步骤一:
stepOne() {
rtcModule.setCallBack(res => {
switch (res.engineEvent) {
// 发生警告回调
case "onWarning":
console.log("发生警告回调", res);
break;
// 发生错误回调
case "onError":
console.log("发生错误回调", res);
break;
// 加入频道成功回调
case "onJoinChannelSuccess":
console.log("加入频道成功回调", res);
// 本地用户视频渲染
this.localVideo();
break;
// 远端用户加入当前频道回调
case "onUserJoined":
uni.showToast({
title: '用户' + res.uid + '加入频道',
icon: 'none',
duration: 2000
});
break;
// 远端用户离开当前频道回调
case "onUserOffline":
uni.showToast({
title: '远端用户' + res.uid + '离开频道',
icon: 'none',
duration: 2000
});
break;
// 已显示远端视频首帧回调
case "onFirstRemoteVideoDecoded":
console.log("已显示远端视频首帧回调", res);
// 远端视频渲染
this.remotenableVideo(res.uid);
break;
// 远端用户视频状态发生已变化回调
case "onRemoteVideoStateChanged":
console.log("远端用户视频状态发生已变化回调", res);
break;
}
});
},
// 步骤二:
stepTwo() {
rtcModule.create({
"appId": this.appid
}, res => {
console.log('初始化实例 rtc', res);
});
// 智能降噪
// rtcModule.setParameters({
// Cmd: 'SetAudioAiNoise',
// Enable: 1,
// }, (res) => {
// console.log('私人定制', res);
// });
},
// 步骤三:
stepThree() {
rtcModule.enableVideo((res) => {
console.log('RTC 启用视频 enableVideo 方法调用', (res.code === 0 ? '成功' : '失败:') +
res);
});
},
// 步骤四:
stepFour() {
this.localuid = this.randomFn(6);
rtcModule.joinChannel({
"token": "",
"channelId": this.channel,
"uid": this.localuid,
}, (res) => {
console.log('RTC joinChannel 方法调用', (res.code === 0 ? '成功' : '失败:') + res);
});
},
// 本地视频渲染
async localVideo() {
// 渲染视频
await this.$refs.location.setupLocalVideo({
"renderMode": 1,
"channelId": this.channel,
"uid": this.localuid,
"mirrorMode": 0
}, (res) => {
console.log('渲染视频', res);
});
// 本地预览
await this.$refs.location.startPreview((res) => {
console.log('本地预览', res);
})
},
async remotenableVideo(uid) {
this.remotenableuid = uid;
this.$refs.remotenable.setupRemoteVideo({
"renderMode": 1,
"channelId": this.channel,
"uid": uid,
"mirrorMode": 0
}, (res) => {
console.log('渲染视频', res);
});
},
// 随机生成
randomFn(len, charSet) {
charSet = charSet || 'abcdefghijklmnopqrstuvwxyz0123456789';
let randomString = '';
for (let i = 0; i < len; i++) {
let randomPoz = Math.floor(Math.random() * charSet.length);
randomString += charSet.substring(randomPoz, randomPoz + 1);
}
return randomString;
},
}
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?