# 实现多方通话

本章将介绍如何实现多方语音通话,多方语音通话的 API 调用时序见下图:

../../../../_images/multiaudioworkflow.jpg

# 获取设备权限

使用 checkSelfPermission (opens new window) 方法,在开启 Activity 时检查并获取 Android 移动设备的麦克风使用权限。

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    // 获取设备权限
    requestPermission();
    
}

/*动态申请权限*/
private void requestPermission() {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
        if (ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED ||
                ContextCompat.checkSelfPermission(this, Manifest.permission.RECORD_AUDIO) != PackageManager.PERMISSION_GRANTED ||
                ContextCompat.checkSelfPermission(this, Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {
            requestPermissions(new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.RECORD_AUDIO, Manifest.permission.CAMERA}, 1000);
        }
    }
}

# 初始化

调用 JCMediaDevice.create (opens new window)JCMediaChannel.create (opens new window) 以初始化实现多方通话需要的模块。

// 声明对象
JCMediaDevice mMediaDevice;
JCMediaChannel mMediaChannel;

// 初始化函数
public boolean initialize(Context context) {

    //1. 媒体类
    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. 媒体通道类
    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) {

        }
    });
}

# 加入频道

  1. 调用 enableUploadAudioStream (opens new window) 开启音频流。

    // 1. 开启音频流
    mMediaDeviceChannel.enableUploadAudioStream(true);
    
  2. 调用 join (opens new window),创建并加入频道。需要传入 channelIdOrUriJCMediaChannel.JoinParam (opens new window)

    • channelId:媒体频道标识。

    • JCMediaChannelJoinParam:加入参数,没有则填 NULL。

    mMediaChannel.join("222", null);
    
  3. 加入频道后自身会收到 onJoin (opens new window) 回调。其他成员会收到 onParticipantJoin (opens new window) 回调。

    @Override
    public void onJoin(boolean result, @JCMediaChannel MediaChannelReason int reason, String channelId) {
        if (result) {
            // 加入频道成功
        } else {
            // 加入频道失败
        }
    }
    

# 离开频道

调用 leave (opens new window) 方法可以离开当前频道。

mMediaChannel.leave();

离开频道后,自身收到 onLeave (opens new window) 回调,其他成员同时收到 onParticipantLeft (opens new window) 回调。

# 解散频道

如果想解散频道,可以调用下面的接口,此时所有成员都将被退出。

// 结束频道
mMediaChannel.stop();

解散频道后,发起结束的成员收到 onStop (opens new window) 回调,其他成员同时收到 onLeave (opens new window) 回调。 解散失败原因枚举值请参考 MediaChannelReason (opens new window)

@Override
public void onStop(boolean result, @JCMediaChannel.MediaChannelReason int reason) {
    // 销毁视频, canvas 为 JCMediaDeviceVideoCanvas 对象实例
    mParticipant.stopVideo();
    canvas = null;
}
最后更新时间: 2021/11/9 16:27:00