聲網(wǎng)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', //聲網(wǎng)開發(fā)者后臺獲取
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;"> //創(chuàng)建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;"> //通過一系列的回調(diào)可獲取頻道升薯,用戶的一些狀態(tài)
engine.current.addListener('UserJoined', (uid: any, elapsed: any) => {
const {peerIds} = info;
if (peerIds.indexOf(uid) === -1) {//頻道內(nèi)新進成員
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);
//離開頻道(關(guān)閉視頻通話)
engine?.current?.leaveChannel();
//開啟/關(guān)閉所有遠(yuǎn)程用戶的音頻流()
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 需要代碼獲取相應(yīng)權(quán)限
PermissionsAndroid.requestMultiple([
PermissionsAndroid.PERMISSIONS.CAMERA,
PermissionsAndroid.PERMISSIONS.RECORD_AUDIO,
]);</pre>
遇到過錯誤碼104同廉,查看文檔得知汽久,是沒有網(wǎng)絡(luò)羡洛。(有文檔就是好) 糾錯文檔地址:https://docs.agora.io/cn/Interactive%20Broadcast/the_error_native?platform=Android#%E8%AD%A6%E5%91%8A%E7%A0%81
兩端運行挂脑,即可實現(xiàn)視頻通話。
4.如上就是我全部的操作
3.跑ios 的時候項目的team 切換到自己的賬號欲侮,運行后在設(shè)備上信任下證書崭闲,也就運行成功了
[圖片上傳失敗...(image-fb67b8-1599971966138)]
慢的話可嘗試,用迅雷下載到本地威蕉,然后修改安卓配置文件里關(guān)于grade的引用
[圖片上傳失敗...(image-28a9b1-1599971966138)]
2.跑安卓的時候出現(xiàn)
1.聲網(wǎng)注冊應(yīng)用刁俭,獲取APPID,形如: 23f75a645.......990的一個字符串忘伞,修改demo的app.tsx文件中state的狀態(tài) 變量appId
關(guān)于官方例子:https://github.com/AgoraIO-Community/Agora-RN-Quickstart 的幾點說明:
5.more
https://docs.agora.io/cn/faq/call_invite_notification
3.如何實現(xiàn)發(fā)起視頻通話請求
1.鑒權(quán)方式修改 2.channelName 做成從后臺獲取薄翅,只關(guān)聯(lián)兩個用戶
4.todo
[圖片上傳失敗...(image-810746-1599971966138)]
3.結(jié)果
實例方法
初始化
2.代碼
Privacy - Microphone Usage Description (key值) Microphone(value值) Privacy - Camera Usage Description(key值) Camera(value值)
沒有的話增加兩行
iOS:npx pod-install (需要在項目的info文件檢查是否有麥克風(fēng)和攝像頭的權(quán)限說明)
android: