Skip to content

Instantly share code, notes, and snippets.

@EkaanshArora
Created August 27, 2019 21:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save EkaanshArora/3b747745a8a8878c6efefeecc3589eb6 to your computer and use it in GitHub Desktop.
Save EkaanshArora/3b747745a8a8878c6efefeecc3589eb6 to your computer and use it in GitHub Desktop.
import React, { Component } from 'react';
import { View, StyleSheet, NativeModules, Platform } from 'react-native';
import { RtcEngine, AgoraView } from 'react-native-agora';
import Icon from 'react-native-vector-icons/MaterialIcons';
import { Actions } from 'react-native-router-flux';
const { Agora } = NativeModules; //Define Agora object as a native module
const {
FPS30,
AudioProfileDefault,
AudioScenarioDefault,
Adaptative,
} = Agora; //Set defaults for Stream
class Video extends Component {
constructor(props) {
super(props);
this.state = {
peerIds: [], //Array for storing connected peers
uid: Math.floor(Math.random() * 100), //Generate a UID for local user
appid: this.props.AppID, //Enter the App ID generated from the Agora Website
channelName: this.props.ChannelName, //Channel Name for the current session
vidMute: false, //State variable for Video Mute
audMute: false, //State variable for Audio Mute
joinSucceed: false, //State variable for storing success
};
if (Platform.OS === 'android') {
const config = { //Setting config of the app
appid: this.state.appid, //App ID
channelProfile: 0, //Set channel profile as 0 for RTC
videoEncoderConfig: { //Set Video feed encoder settings
width: 720,
height: 1080,
bitrate: 1,
frameRate: FPS30,
orientationMode: Adaptative,
},
audioProfile: AudioProfileDefault,
audioScenario: AudioScenarioDefault,
};
RtcEngine.init(config); //Initialize the RTC engine
}
}
componentDidMount() {
RtcEngine.on('userJoined', (data) => {
const { peerIds } = this.state; //Get currrent peer IDs
if (peerIds.indexOf(data.uid) === -1) { //If new user has joined
this.setState({
peerIds: [...peerIds, data.uid], //add peer ID to state array
});
}
});
RtcEngine.on('userOffline', (data) => { //If user leaves
this.setState({
peerIds: this.state.peerIds.filter(uid => uid !== data.uid), //remove peer ID from state array
});
});
RtcEngine.on('joinChannelSuccess', (data) => { //If Local user joins RTC channel
RtcEngine.startPreview(); //Start RTC preview
this.setState({
joinSucceed: true, //Set state variable to true
});
});
RtcEngine.joinChannel(this.state.channelName, this.state.uid); //Join Channel
RtcEngine.enableAudio(); //Enable the audio
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment