公司的產品是一款基於社交的內容聊天軟件,須要集成語音通信功能,在寫iOS原生項目時,用到的就是Agora SDK,如今寫React Native也直接採用了Agora的庫。react
請參考連接:https://github.com/syanbo/react-native-agoragit
提示:由於Agora的庫有兩個版本,必須採用最新版,以避免有些功能不支持(筆者在集成時,當時沒有注意,有很多折騰呢)github
不知道怎麼開頭寫的時候,參考官方Demo : https://github.com/agoraio-community/agora-rn-quickstartreact-native
configureAgora(){ var that = this; const config = { appid: "17423c8***********5cd46f89e", channelProfile: this.props.channelProfile, clientRole: this.props.clientRole, audioProfile: AudioProfileMusicHighQuality, audioScenario: AudioScenarioChatRoomGaming, } console.log("[CONFIG]", JSON.stringify(config)); console.log("[CONFIG.encoderConfig", config.videoEncoderConfig); RtcEngine.on('videoSizeChanged', (data) => { console.log("[RtcEngine] videoSizeChanged ", data) }) RtcEngine.on('remoteVideoStateChanged', (data) => { console.log('[RtcEngine] `remoteVideoStateChanged`', data); }) RtcEngine.on('userJoined', (data) => { //用戶加入 }) RtcEngine.on('userOffline', (data) => {//用戶下線 }) RtcEngine.on('audioVolumeIndication', (data) => { console.log(data); }) RtcEngine.on('clientRoleChanged', (data) => { console.log("[RtcEngine] onClientRoleChanged", data); }) RtcEngine.on('joinChannelSuccess', (data) => { console.log('[RtcEngine] onJoinChannelSuccess', data); // console.log(RtcEngine.options); RtcEngine.startPreview().then(data => { // this.setState({ // joinSucceed: true, // animating: false // }) }) // RtcEngine.setEnableSpeakerphone(true) // RtcEngine.setDefaultMuteAllRemoteAudioStreams(true) global.channel = data.channel global.voiceStatus = 'join'; }) RtcEngine.init(config); RtcEngine.enableAudio() }
RtcEngine.joinChannel(this.props.childTribeId,Parse.User.current().attributes.uid,"","") .then(result => { });
RtcEngine.leaveChannel((status)=>{
console.log(status)
})
提示:app
一、在如下代碼中filepath:爲絕對路徑或url,不能使用相對路徑ide
二、soundid是正確的,若是直接粘貼demo,會出錯誤(Demo中是:soundId)oop
RtcEngine.on('userJoined', (data) => { //用戶加入 console.log('[RtcEngine] onUserJoined', data); let playEffectOption = { soundid: 1, filepath:'https://oops-*****.cos.ap-shanghai.myqcloud.com/in.mp3', loopcount: 0, pitch: 1, pan: 0, gain: 40, publish: false, }; RtcEngine.playEffect(playEffectOption) // const {peerIds} = this.state; // if (peerIds.indexOf(data.uid) === -1) { // this.setState({ // peerIds: [...peerIds, data.uid] // }) // } }) RtcEngine.on('userOffline', (data) => {//用戶下線 console.log('[RtcEngine] onUserOffline', data); let playEffectOption = { soundid: 2, filepath:'https://oops-*****.cos.ap-shanghai.myqcloud.com/in.mp3', loopcount: 0, pitch: 1, pan: 0, gain: 40, publish: false, }; RtcEngine.playEffect(playEffectOption) })