声网实现音频通话

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

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('客户离开信道成功==============')
        }
      }

猜你喜欢

转载自blog.csdn.net/qq_40407998/article/details/128110891