封装
import user from '@/api/user'
import AgoraRTC, { IAgoraRTCClient } from 'agora-rtc-sdk-ng'
export const AGORA_USB_LOCAL = '_usbCamera'
export const AGORA_USB_CHECK = '_usbCheck'
const LiveTranscoding = {
videoCodecProfile: 77
}
export class AgoraRtc {
rtc: any
options: any
constructor () {
this.rtc = {
client: null,
localAudioTrack: null,
localVideoTrack: null,
role: 'audience'
}
this.options = {
appId: '94b0b3c96c814ec88aa701cef4cd052b',
uid: 0,
channel: 0,
token: null,
role: 'audience',
userAudioTrack: [],
type: 1
}
}
async init (role?: string, type?: number) {
return new Promise<void>((resolve, reject) => {
if (role) {
if (role === 'audience' || role === 'host') {
this.options.role = role
this.rtc.client = AgoraRTC.createClient({ mode: 'live', codec: 'vp8' })
this.rtc.client.setClientRole(role).then((res: any) => {
})
resolve()
} else {
reject(new Error('角色只支持audience或host'))
}
if (type === 1 || type === 2 || type === 3) {
this.options.type = type
} else {
this.options.type = 1
}
} else {
this.rtc.client = AgoraRTC.createClient({ mode: 'live', codec: 'vp8' })
resolve()
}
})
}
async join (channel: number) {
this.options.userAudioTrack = []
this.options.channel = channel
const uid = await this.rtc.client.join(this.options.appId, this.options.channel.toString(), null, null)
this.options.uid = uid
this.rtc.client.setLiveTranscoding(LiveTranscoding).then(() => {
})
}
async created () {
if (this.options.type === 1) {
this.rtc.localAudioTrack = await AgoraRTC.createMicrophoneAudioTrack()
this.rtc.localVideoTrack = await AgoraRTC.createCameraVideoTrack()
await this.rtc.client.publish([this.rtc.localAudioTrack, this.rtc.localVideoTrack])
} else if (this.options.type === 2) {
this.rtc.localAudioTrack = await AgoraRTC.createMicrophoneAudioTrack()
await this.rtc.client.publish([this.rtc.localAudioTrack])
} else if (this.options.type === 3) {
this.rtc.localVideoTrack = await AgoraRTC.createCameraVideoTrack()
await this.rtc.client.publish([this.rtc.localVideoTrack])
}
}
subscribe (onSuccess: any) {
this.rtc.client.on('user-published', async (user: any, mediaType: any) => {
await this.rtc.client.subscribe(user, mediaType)
if (mediaType === 'video') {
this.options.userAudioTrack.push(user)
}
onSuccess(user, mediaType)
})
}
leaveMonitor (onSuccess: any) {
this.rtc.client.on('user-left', async (user: any, reason: string) => {
if (reason === 'Quit' || reason === 'ServerTimeOut') {
onSuccess && onSuccess(user)
}
})
}
getAllChannelMembers (onSuccess: any) {
onSuccess && onSuccess(this.rtc.client.remoteUsers)
}
async leave () {
if (this.rtc.localAudioTrack) {
this.rtc.localAudioTrack.close()
}
if (this.rtc.localVideoTrack) {
this.rtc.localVideoTrack.close()
}
await this.rtc.client.leave()
}
distalAudioVolume (value: number) {
if (this.options.userAudioTrack.length) {
this.options.userAudioTrack.forEach((item: any) => {
item.audioTrack.setVolume(value)
})
}
}
localAudioVolume (value: number) {
if (!this.rtc.localAudioTrack) return
this.rtc.localAudioTrack.setVolume(value)
}
play () {
this.rtc.play('agora_remote', (err: any) => {
if (err && err.status !== 'aborted') {
}
})
}
}
使用
initVoid () {
data.roomNumber = route.query.room
data.memberName = route.query.memberName
agoraRtc = new AgoraRtc()
agoraRtc.init('host', 2).then(() => {
agoraRtc.leave().then(() => {
agoraRtc.join(data.roomNumber).then(() => {
methods.handleMembersLeave()
agoraRtc.created()
agoraRtc.subscribe((user: any, mediaType: string) => {
data.player = true
if (mediaType === 'video') {
user.videoTrack.play(document.getElementById('video-container'), { fit: 'contain' })
}
if (mediaType === 'audio') {
user.audioTrack.play()
}
})
})
})
})
},
播放配置
user.videoTrack.play(document.getElementById('video-container'), { fit: 'contain' })