声网RTC接入记录| React Native
1.在已有项目接入RTC(ts)(0.63.2)
npx react-native init MyApp --template react-native-template-typescript
插件地址
https://github.com/AgoraIO-Community/react-native-agora
API文档说明
https://agoraio-community.github.io/react-native-agora/globals.html
<pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="" cid="n104" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-size: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-top-left-radius: 3px; border-top-right-radius: 3px; border-bottom-right-radius: 3px; border-bottom-left-radius: 3px; padding: 8px 4px 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; background-position: inherit inherit; background-repeat: inherit inherit;">yarn add react-native-agora</pre>
<pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="ts/hook" cid="n157" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-size: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-top-left-radius: 3px; border-top-right-radius: 3px; border-bottom-right-radius: 3px; border-bottom-left-radius: 3px; padding: 8px 4px 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; background-position: inherit inherit; background-repeat: inherit inherit;">定义初始变量
const [info, setInfo] = useState<any>({
appId: '23f75a64593a412d90da30b6e9986990', //声网开发者后台获取
channelName: 'channel-x',//频道name,
joinSucceed: false,
peerIds: [],//同一频道下的用户的id集合
isMute: false,//是否静音
});</pre>
<pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="" cid="n178" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-size: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-top-left-radius: 3px; border-top-right-radius: 3px; border-bottom-right-radius: 3px; border-bottom-left-radius: 3px; padding: 8px 4px 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; background-position: inherit inherit; background-repeat: inherit inherit;"> //创建engine 对象
const engine = useRef<any>(null);
engine.current = await RtcEngine.create(appId);</pre>
<pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="" cid="n196" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-size: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-top-left-radius: 3px; border-top-right-radius: 3px; border-bottom-right-radius: 3px; border-bottom-left-radius: 3px; padding: 8px 4px 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; background-position: inherit inherit; background-repeat: inherit inherit;"> //开启视频功能以及加入"channel-x"频道
await engine.current.enableVideo();
await engine?.current?.joinChannel(null, info.channelName, null, 0);</pre>
<pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="" cid="n202" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-size: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-top-left-radius: 3px; border-top-right-radius: 3px; border-bottom-right-radius: 3px; border-bottom-left-radius: 3px; padding: 8px 4px 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; background-position: inherit inherit; background-repeat: inherit inherit;"> //通过一系列的回调可获取频道,用户的一些状态
engine.current.addListener('UserJoined', (uid: any, elapsed: any) => {
const {peerIds} = info;
if (peerIds.indexOf(uid) === -1) {//频道内新进成员
setInfo((pre: any) => ({
...pre,
peerIds: [...peerIds, uid],
}));
}
});
engine.current.addListener('UserOffline', (uid: any, reason: any) => {
//成员离开频道
const {peerIds} = info;
setInfo((pre: any) => ({
...pre,
peerIds: peerIds.filter((id: any) => id !== uid),
}));
});
engine.current.addListener(
'JoinChannelSuccess',
(channel: any, uid: any, elapsed: any) => {
成员成功加入频道(可获取到加入的频道,加入的用户,elapsed?)
setInfo((pre: any) => ({
...pre,
joinSucceed: true,
}));
},
);</pre>
<pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="" cid="n206" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-size: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-top-left-radius: 3px; border-top-right-radius: 3px; border-bottom-right-radius: 3px; border-bottom-left-radius: 3px; padding: 8px 4px 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; background-position: inherit inherit; background-repeat: inherit inherit;"> //进入频道(本地视图开启)
engine?.current?.joinChannel(null, info.channelName, null, 0);
//离开频道(关闭视频通话)
engine?.current?.leaveChannel();
//开启/关闭所有远程用户的音频流()
engine?.current?.muteAllRemoteAudioStreams(info?.isMute);
setInfo((pre: any) => ({...pre, isMute: !info?.isMute}));
//切换摄像头
engine?.current?.switchCamera();
</pre>
<pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="" cid="n212" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-size: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-top-left-radius: 3px; border-top-right-radius: 3px; border-bottom-right-radius: 3px; border-bottom-left-radius: 3px; padding: 8px 4px 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; background-position: inherit inherit; background-repeat: inherit inherit;"> //android 10 需要代码获取相应权限
PermissionsAndroid.requestMultiple([
PermissionsAndroid.PERMISSIONS.CAMERA,
PermissionsAndroid.PERMISSIONS.RECORD_AUDIO,
]);</pre>
遇到过错误码104,查看文档得知,是没有网络。(有文档就是好) 纠错文档地址:https://docs.agora.io/cn/Interactive%20Broadcast/the_error_native?platform=Android#%E8%AD%A6%E5%91%8A%E7%A0%81
两端运行,即可实现视频通话。
4.如上就是我全部的操作
3.跑ios 的时候项目的team 切换到自己的账号,运行后在设备上信任下证书,也就运行成功了
[图片上传失败...(image-fb67b8-1599971966138)]
慢的话可尝试,用迅雷下载到本地,然后修改安卓配置文件里关于grade的引用
[图片上传失败...(image-28a9b1-1599971966138)]
2.跑安卓的时候出现
1.声网注册应用,获取APPID,形如: 23f75a645.......990的一个字符串,修改demo的app.tsx文件中state的状态 变量appId
关于官方例子:https://github.com/AgoraIO-Community/Agora-RN-Quickstart 的几点说明:
5.more
https://docs.agora.io/cn/faq/call_invite_notification
3.如何实现发起视频通话请求
1.鉴权方式修改 2.channelName 做成从后台获取,只关联两个用户
4.todo
[图片上传失败...(image-810746-1599971966138)]
3.结果
实例方法
初始化
2.代码
Privacy - Microphone Usage Description (key值) Microphone(value值) Privacy - Camera Usage Description(key值) Camera(value值)
没有的话增加两行
iOS:npx pod-install (需要在项目的info文件检查是否有麦克风和摄像头的权限说明)
android:
网友评论