React Native [] integrated voice network voice communications Agora

 Preface:

  The company's product is a content-based social chat software, integrated voice communication needs, when writing iOS native project, is used Agora SDK, now write React Native directly using the Agora library.

 Integrated iOS, Android steps:

  Please refer to the link: https://github.com/syanbo/react-native-agora

  Tip: Because there are two versions of the Agora library, you must use the latest version, in order to avoid some features are not supported (the author at the time of integration, did not pay attention, there are a lot of tossing it)

 Use record:

  I do not know how to write the beginning of time, refer to the official Demo:  https://github.com/agoraio-community/agora-rn-quickstart

  1, the main function of the code:

configureAgora(){
        var that = this;
        const config = {
            appid: "17423c8***********5cd46f89e",
            channelProfile: this.props.channelProfile,
            clientRole: this.props.clientRole,
            audio profile: audio profiles High Quality Music,
            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) => { // Users
           
        })
        RtcEngine.on ( 'userOffline', (Data) => { // user offline
            
        })
        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()
    }

  2, by adding channels

RtcEngine.joinChannel(this.props.childTribeId,Parse.User.current().attributes.uid,"","")
            .then(result => {

            });

  3, exit channel

RtcEngine.leaveChannel((status)=>{
            console.log(status)

        })

  4, when someone joins or exits, Play sound

  prompt:

  1, filepath in the following code: an absolute path or a url, can not use a relative path

  2, soundid is correct, if directly pasted demo, will be out error (Demo is: soundId)

RtcEngine.on ( 'userJoined', (Data) => { // user joins 
            the console.log ( '[RtcEngine] onUserJoined' , Data);
            let playEffectOption = {
                soundid: 1,
                filepath:'https://oops-*****.cos.ap-shanghai.myqcloud.com/in.mp3',
                Over Count: 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) => { // user offline 
            the console.log ( '[RtcEngine] onUserOffline' , Data);
            let playEffectOption = {
                soundid: 2,
                filepath:'https://oops-*****.cos.ap-shanghai.myqcloud.com/in.mp3',
                Over Count: 0 ,
                pitch: 1,
                pan: 0,
                gain: 40,
                publish: false,
            };

            RtcEngine.playEffect(playEffectOption)
        })

 

Guess you like

Origin www.cnblogs.com/xjf125/p/12347378.html