• 声网实现音频通话


    声网官网文档: 官网链接
    安装

    npm install agora-rtc-sdk-ng --save
    
    • 1

    主要代码

    import AgoraRTC from 'agora-rtc-sdk-ng'
    
    • 1
    // 定义的变量:
       appid: '', // appId
       token: '', // 根据房间号生成的token(房间号和token对应)
       channel: '', // 房间号(房间号和token对应)
       uid: null,
       agoraClient: null, // 实例
       localTracks: {  // 信道
         audioTrack: null
       },
       remoteUsers: {},
       callState: null
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    // 通话 开始 ==================================
          sharRTC () {
            if (this.token) {
              this.token = ''
              this.Leave()
              return
            }
            // 创建本地客户端 AgoraRTC 的实例
            this.agoraClient = AgoraRTC.createClient({ mode: 'rtc', codec: 'vp8' })
            // 用户信息
            this.uid = null
            this.appid = window.SITE_CONFIG.appid
            this.channel = '1' // 通道号需要与token绑定,且通话两边保持一致
            API.***.getToken(this.channel).then(({data}) => { // 获取统一的token
              console.log('==getToken2===------=', data)
              this.token = data.data
              // 连接
              this.join()
            })
          },
          // 获取
          async join () {
            // 添加事件侦听器以在远程用户发布时播放远程曲目.
            this.agoraClient.on('user-published', this.handleUserPublished)
            this.agoraClient.on('user-unpublished', this.handleUserUnpublished)
            this.agoraClient.on('connection-state-change', this.getState)
            // 加入频道
            Promise.all([
              // join the channel
              this.agoraClient.join(this.appid, this.channel, this.token || null),
              // 使用麦克风
              AgoraRTC.createMicrophoneAudioTrack()]).then(async(result) => {
                console.log('使用麦克风=======', result)
                this.uid = result[0]
                this.localTracks.audioTrack = result[1]
                // 将本地曲目发布到频道
                await this.agoraClient.publish(Object.values(this.localTracks))
              }).catch((error) => {
                console.log(error)
              })
          },
          handleUserPublished (user, mediaType) {
            const id = user.uid
            this.remoteUsers[id] = user
            this.subscribe(user, mediaType)
          },
          handleUserUnpublished (user) {
            const id = user.uid
            delete this.remoteUsers[id]
          },
          async subscribe (user, mediaType) {
            const uid = user.uid
            // 订阅远程用户
            await this.agoraClient.subscribe(user, mediaType)
            if (mediaType === 'audio') {
              user.audioTrack.play()
            }
          },
          // 获取当前通话状态
          getState (state) {
            this.callState = state
          },
          // 客户离开信道
          async Leave () {
            if (this.callState === 'CONNECTING' || this.callState === 'CONNECTED' || this.callState === 'RECONNECTING') {
              this.localTracks.audioTrack && this.localTracks.audioTrack.stop()
              this.localTracks.audioTrack && this.localTracks.audioTrack.close()
              this.agoraClient && await this.agoraClient.leave()
              this.remoteUsers = {}
              console.log('客户离开信道成功==============')
            }
          }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
  • 相关阅读:
    java毕业设计青少年心理健康教育平台演示录像2020源码+lw文档+mybatis+系统+mysql数据库+调试
    clickhouse在执行alter table update delete等命令后数据没有更新
    rt-hwwb前端面试题
    基于Springboot+Mybatis+mysql+vue宠物领养网站1.0
    华为云之深入探究GaussDB如何助力企业公司打造金融核心数据
    数据结构与算法复习:第三十三弹
    NodeJS校园快递智能互助平台-计算机毕业设计源码58554
    proxomx虚机无法启动的奇怪问题
    快手小范围内测将“商城”独立,并放在顶部tab位置
    FFmpeg开发笔记(二十)Linux环境给FFmpeg集成AVS3解码器
  • 原文地址:https://blog.csdn.net/qq_40407998/article/details/128110891