This experience is the “voice call/voice live” module under sonnet, so practical and popular function using Agora SDK actually feel so simple and smooth, we first download the official demo to have a look


The official demo

1. Enter the official website, register an acoustic account first, create a project, and then get an AppID




2. Select the SDK we want to use in the SDK interface



3. Download and decompress the two folders (libs and samples). We will directly import project samples from androidstudio (my as version is 3.1.3). Use the apID “C v” we just got on the official website


4. The official Demo is very simple, relying on some libraries and OS in the lib folder. There is only one VoiceChatViewActivity class in the project, which contains less than 200 lines of code


Initialization code: very simple, just set our appID

private void initializeAgoraEngine() {
    try {
        mRtcEngine = RtcEngine.create(getBaseContext(), getString(R.string.agora_app_id), mRtcEventHandler);
    } catch (Exception e) {
        Log.e(LOG_TAG, Log.getStackTraceString(e));

        throw new RuntimeException("NEED TO check rtc sdk init fatal error\n"+ Log.getStackTraceString(e)); }}Copy the code


Join a channel: Note that you need to close the previous channel before joining a channel

 private void joinChannel(a) {
    mRtcEngine.joinChannel(null."demoChannel1"."Extra Optional Data".0); // if you do not specify the uid, Agora will assign one.
}Copy the code


Off channel: also called in onDestory

 private void leaveChannel() {
    mRtcEngine.leaveChannel();
}Copy the code


We added a button in the official demo, click to add a channel, later useful

mRtcEngine.leaveChannel();
mRtcEngine.joinChannel(null, "voiceDemoChannel1"."Extra Optional Data", 0);Copy the code


The basic process is like this, you can see it is very simple


So let’s implement this Demo ourselves

1. Create a new project, add SDK and so file structure as shown in the figure (depending on the lib folder from SDK)




2. Add it in build.gradle of app

sourceSets {  
  main {  
      jniLibs.srcDirs = ['src/main/jniLibs']}}Copy the code


3. Done, now implement the following code, when a user enters the channel screen alert

private final IRtcEngineEventHandler mRtcEventHandler = new IRtcEngineEventHandler@override public void onUserOffline(final int uid, final int reason) { runOnUiThread(newRunnable() {  
          @Override           
 public void run() { onLeaveRoom(uid, reason); }}); @override public void onUserJoined(final int uid, final int Elapsed) {super.onuserjoined (uid, Elapsed); runOnUiThread(newRunnable() {      
      @Override            
public void run() { onIntoRoom(uid, elapsed); }}); }}; /** * 1. Initialize the sound net **/private voidinitializeAgoraEngine() {    
try {      
  mRtcEngine = RtcEngine.create(getBaseContext(), "b4dfdd47b81a4d4eadee22c9a479e193", mRtcEventHandler);
    } catch (Exception e) {  
      Log.e(LOG_TAG, Log.getStackTraceString(e));   
     throw new RuntimeException("NEED TO check rtc sdk init fatal error\n"+ Log.getStackTraceString(e)); } /** * 2. Add channel **/ private voidjoinChannel() { 
   mRtcEngine.joinChannel(null, "voiceDemoChannel1"."Extra Optional Data", 0); // if you do not specify the uid, we will generate the uid forPrivate void onLeaveRoom(final int uid, int reason) {this.runonuithRead (new)Runnable() {      
  @Override      
  public void run() {  
          tvMessaghe.setText(tvMessaghe.getText() + "\ n users" + uid + "Leave the room"+getNowTime()); }}); } /** * The user enters the room **/ private void onIntoRoom(final int uid, int reason) {this.runonuithread (newRunnable() {       
 @Override      
  public void run() {       
     tvMessaghe.setText(tvMessaghe.getText() + "\ n users" + uid + "Enter the room"+getNowTime()); }}); } /** * 3. To exit the room, call **/ private void when closing the programleaveChannel() { 
   mRtcEngine.leaveChannel();
}Copy the code





4. Final effect: After joining the room, you can make a 1V1 call. Since you can’t record the sound, you can see the effect is about like this







5. It has many functions, such as voice quality, mixing and so on, here is not an example, interested in children’s boots can go to its official website to have a look, are very simple




Summary: The overall feeling is that Flutter is simple and easy to use, with simple API. It is really just a few lines of code to achieve a 1V1 call and develop the first choice quickly. In addition, I heard that Xiaomi and Momo are using it, and it seems that Flutter supports cross-platform.



The last

Agora SDK Use experience essay contest| the nuggets technical essay, the campaign is underway