# Realize live interactive audio streaming

This guide introduces how to implement live interactive audio streaming. The API call sequence for live interactive streaming is shown in the figure below:

../../../../_images_en/multiaudioworkflow.jpg

# Initialize

Call JCMediaDevice.create (opens new window) and JCCall.create (opens new window) to initialize Modules needed to implement group calling:

// Declare object
JCMediaDevice mMediaDevice;
JCMediaChannel mMediaChannel;

// Initialization function
public boolean initialize(Context context) {

    //1. Media class
    mMediaDevice = JCMediaDevice.create(mClient, new JCMediaDeviceCallback() {
        @Override
        public void onCameraUpdate() {

        }
        @Override
        public void onAudioOutputTypeChange(int i) {

        }
        @Override
        public void onRenderReceived(JCMediaDeviceVideoCanvas jcMediaDeviceVideoCanvas) {

        }
        @Override
        public void onRenderStart(JCMediaDeviceVideoCanvas jcMediaDeviceVideoCanvas) {

        }
    });
    // 2. Media channel lass
    mMediaChannel = JCMediaChannel.create(client, mediaDevice, new JCMediaChannelCallback() {
        @Override
        public void onMediaChannelStateChange(int i, int i1) {

        }
        @Override
        public void onMediaChannelPropertyChange(JCMediaChannel.PropChangeParam propChangeParam) {

        }
        @Override
        public void onJoin(boolean b, int i, String s) {

        }
        @Override
        public void onLeave(int i, String s) {

        }
        @Override
        public void onStop(boolean b, int i) {

        }
        @Override
        public void onQuery(int i, boolean b, int i1, JCMediaChannelQueryInfo jcMediaChannelQueryInfo) {

        }
        @Override
        public void onParticipantJoin(JCMediaChannelParticipant jcMediaChannelParticipant) {

        }
        @Override
        public void onParticipantLeft(JCMediaChannelParticipant jcMediaChannelParticipant) {

        }
        @Override
        public void onParticipantUpdate(JCMediaChannelParticipant jcMediaChannelParticipant, JCMediaChannelParticipant.ChangeParam changeParam) {

        }
        @Override
        public void onMessageReceive(String s, String s1, String s2) {

        }
        @Override
        public void onInviteSipUserResult(int i, boolean b, int i1) {

        }
        @Override
        public void onParticipantVolumeChange(JCMediaChannelParticipant jcMediaChannelParticipant) {

        }
    });
}

# Role setting

Before joining the channel, you must set the role first. The role setting include the host and audiences. The role value can be customized according to the JCMediaChannel.CustomRole (opens new window) enumeration value, for example:

//Customize the role of the host according to the CustomState enumeration value
int ROLE_BROASCASTER = JCMediaChannel.CUSTOM_ROLE_0;
//Customize the role of audiences according to the CustomState enumeration value
int ROLE_AUDIENCE = JCMediaChannel.CUSTOM_ROLE_1;

Call setCustomRole (opens new window) to set your own role to enter the channel:

// Set the role; the value of participant (the second parameter) is null, which means that you set your own role
mediaChannel.setCustomRole(ROLE_BROASCASTER, null);

# Join a channel

  1. Call enableUploadAudioStream (opens new window) to enable audio streaming:

    // 1. Enable the audio stream
    mMediaDeviceChannel.enableUploadAudioStream(true);
    
  2. To create and join a channel, you need to pass in channelIdOrUri and JCMediaChannel.JoinParam (opens new window):

    • channelIdOrUri refers to the channel ID or channel Uri.

    • In JCMediaChannelJoinParam, when the uriMode parameter is set to true, it means the incoming channel Uri. When other parameters are set, it means the incoming channel ID. Users who pass in the same channel ID or the same channel Uri will enter the same Channel:

      mMediaChannel.join("222", null);
      
  3. The onJoin (opens new window) callback triggers after joining the channel:

    @Override
    public void onJoin(boolean result, @JCMediaChannel MediaChannelReason int reason, String channelId) {
        if (result) {
            // Join the channel successfully
        } else {
            // Join the channel failed
        }
    }
    

# Leave a channel

Call the leave (opens new window) method to leave the current channel:

mMediaChannel.leave();

In a group video call, you need to call stopVideo (opens new window) to remove the video image when leaving the channel:

mParticipant.stopVideo();

After leaving the channel, they receive the onLeave (opens new window) callback, and other members receive the onParticipantLeft (opens new window) callback at the same time.

# Destroy a channel

If you want to destroy a channel, you can call the following interface, and all members will be quit:

// End a channel
mMediaChannel.stop();

In a group video call, you need to call stopVideo (opens new window) to remove the video image when leaving the channel:

mParticipant.stopVideo();

After the channel is stopped, the member who initiated the termination receives the onStop (opens new window) callback, and other members also receive the onLeave (opens new window) callback. Please refer to MediaChannelReason (opens new window) for the enumeration value of the reason for failure:

@Override
public void onStop(boolean result, @JCMediaChannel.MediaChannelReason int reason) {
    //Destroy the video, canvas is the instance of JCMediaDeviceVideoCanvas object
    mParticipant.stopVideo();
    canvas = null;
}
最后更新时间: 2/1/2021, 3:54:49 PM