/**
* agora声网解决方案
* 1. 创建 AgoraClient 对象
* 2. join进入相应频道
* 3. on 监听 user-published 拿到远端用户对象 AgoraRTCRemoteUser
* 4. subscribe订阅流 将3.内的参数(远端用户对象, type('video'/'audio'))赋值给subscribe,拿到最新的远端用户信息
* 5. 根绝监听 user-published 返回的type来分别处理音/视频
* video -> AgoraRTCRemoteUser.videoTrack.play(videoId 或者是 video的dom) 元素为 div
* user -> AgoraRTCRemoteUser.audioTrack.play()
*/
npm
npm install agora-rtc-sdk-ng --save
引入
import AgoraRTC from 'agora-rtc-sdk-ng'
创建AgoraClient对象
const AgoraClient = AgoraRTC.createClient({
mode: 'live', // 模式
codec: 'vp8', // 编码格式
role: 'audience' // 角色
});
监听/订阅流
async initAgora() {
await AgoraClient.join(this.appId, this.channel, this.token, null)
AgoraClient.on('user-published', async (user, type) => {
// 开始订阅远端用户。
let userData = await AgoraClient.subscribe(user, type)
if (type === 'video') {
// 这里的 id 可以是一个 div 盒子的 id 属性,也可以是一个 dom,但标签必须是 div
userData.play(id)
console.log('video订阅成功了')
} else if (type === 'audio') {
userData.play()
console.log('audio订阅成功了')
}
})
}
参考这位大佬和文档写出来的上面👆