声网实现音频通话

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

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

主要代码

import AgoraRTC from 'agora-rtc-sdk-ng'
// 定义的变量:
   appid: '', // appId
   token: '', // 根据房间号生成的token(房间号和token对应)
   channel: '', // 房间号(房间号和token对应)
   uid: null,
   agoraClient: null, // 实例
   localTracks: {  // 信道
     audioTrack: null
   },
   remoteUsers: {},
   callState: null

// 通话 开始 ==================================
      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('客户离开信道成功==============')
        }
      }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值