2020-09-13

聲網(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:

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市氓奈,隨后出現(xiàn)的幾起案子翘魄,更是在濱河造成了極大的恐慌,老刑警劉巖舀奶,帶你破解...
    沈念sama閱讀 216,919評論 6 502
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件暑竟,死亡現(xiàn)場離奇詭異,居然都是意外死亡育勺,警方通過查閱死者的電腦和手機但荤,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,567評論 3 392
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來涧至,“玉大人腹躁,你說我怎么就攤上這事∧吓睿” “怎么了纺非?”我有些...
    開封第一講書人閱讀 163,316評論 0 353
  • 文/不壞的土叔 我叫張陵哑了,是天一觀的道長。 經(jīng)常有香客問我烧颖,道長弱左,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 58,294評論 1 292
  • 正文 為了忘掉前任炕淮,我火速辦了婚禮拆火,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘涂圆。我一直安慰自己们镜,他們只是感情好,可當(dāng)我...
    茶點故事閱讀 67,318評論 6 390
  • 文/花漫 我一把揭開白布乘综。 她就那樣靜靜地躺著憎账,像睡著了一般。 火紅的嫁衣襯著肌膚如雪卡辰。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,245評論 1 299
  • 那天邪意,我揣著相機與錄音九妈,去河邊找鬼。 笑死雾鬼,一個胖子當(dāng)著我的面吹牛萌朱,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播策菜,決...
    沈念sama閱讀 40,120評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼晶疼,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了又憨?” 一聲冷哼從身側(cè)響起翠霍,我...
    開封第一講書人閱讀 38,964評論 0 275
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎蠢莺,沒想到半個月后寒匙,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,376評論 1 313
  • 正文 獨居荒郊野嶺守林人離奇死亡躏将,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,592評論 2 333
  • 正文 我和宋清朗相戀三年锄弱,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片祸憋。...
    茶點故事閱讀 39,764評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡会宪,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出蚯窥,到底是詐尸還是另有隱情掸鹅,我是刑警寧澤喜命,帶...
    沈念sama閱讀 35,460評論 5 344
  • 正文 年R本政府宣布,位于F島的核電站河劝,受9級特大地震影響壁榕,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜赎瞎,卻給世界環(huán)境...
    茶點故事閱讀 41,070評論 3 327
  • 文/蒙蒙 一牌里、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧务甥,春花似錦牡辽、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,697評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至挺尿,卻和暖如春奏黑,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背编矾。 一陣腳步聲響...
    開封第一講書人閱讀 32,846評論 1 269
  • 我被黑心中介騙來泰國打工熟史, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人窄俏。 一個月前我還...
    沈念sama閱讀 47,819評論 2 370
  • 正文 我出身青樓蹂匹,卻偏偏與公主長得像,于是被迫代替她去往敵國和親凹蜈。 傳聞我的和親對象是個殘疾皇子限寞,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 44,665評論 2 354