Skip to main content

You are viewing Agora Docs forBetaproducts and features. Switch to Docs

Android
iOS
macOS
Web
Windows
Electron
Flutter
React Native
React JS
Unity
Unreal Engine
Unreal (Blueprint)

Join multiple channels

Agora Video SDK enables you to simultaneously join multiple channels. This capability allows you to receive and publish audio and video streams across multiple channels concurrently.

Understand the tech

Video SDK's multi-channel functionality is based on two key components:

  • RtcConnection

    The RtcConnection object identifies a connection. It contains the following information:

    • Channel name
    • User ID of the local user

    You create multiple RtcConnection objects, each with a different channel name and user ID. Each RtcConnection instance can independently publish multiple audio streams and a single video stream.

  • RtcEngineEx

    The class contains methods tailored for interacting with a designated RtcConnection object.

    To join multiple channels, you call joinChannelEx method in the RtcEngineEx class multiple times, using a different RtcConnection instance each time.

When joining multiple channels:

  • Ensure that the user ID for each RtcConnection object is unique and nonzero.
  • Configure publishing and subscribing options for the RtcConnection object in joinChannelEx.
  • Pass the IRtcEngineEventHandler object to the eventHandler parameter when calling the joinChannelEx method to receive multiple channel-related event notifications.

Prerequisites

Ensure that you have implemented the SDK quickstart in your project.

Implementation

This section explains how to join a second channel as a host after you have already joined the first channel.

  1. Declare variables for RtcEngineEx and RtcConnection objects.


    _2
    private RtcEngineEx engine;
    _2
    private RtcConnection rtcConnection2 = new RtcConnection();

  2. Initialize the engine instance.


    _1
    engine = (RtcEngineEx) RtcEngine.create(config);

  3. Join the channel using a random user ID.


    _10
    private boolean joinSecondChannel() {
    _10
    ChannelMediaOptions option = new ChannelMediaOptions();
    _10
    mediaOptions.autoSubscribeAudio = true;
    _10
    mediaOptions.autoSubscribeVideo = true;
    _10
    rtcConnection2.channelId = "channel-2";
    _10
    rtcConnection2.localUid = new Random().nextInt(512)+512;
    _10
    int ret = engine.joinChannelEx("your token", rtcConnection2, mediaOptions,
    _10
    iRtcEngineEventHandler2);
    _10
    return (ret == 0);
    _10
    }

  4. Listen for events in rtcConnection2 and set up the remote video in the onUserJoined callback.


    _32
    private final IRtcEngineEventHandler iRtcEngineEventHandler2 = new IRtcEngineEventHandler() {
    _32
    @Override
    _32
    public void onJoinChannelSuccess(String channel, int uid, int elapsed) {
    _32
    Log.i(TAG, String.format("channel2 onJoinChannelSuccess channel %s uid %d", channel2, uid));
    _32
    showLongToast(String.format("onJoinChannelSuccess channel %s uid %d", channel2, uid));
    _32
    }
    _32
    @Override
    _32
    public void onUserJoined(int uid, int elapsed) {
    _32
    Log.i(TAG, "channel2 onUserJoined->" + uid);
    _32
    showLongToast(String.format("user %d joined!", uid));
    _32
    Context context = getContext();
    _32
    if (context == null) {
    _32
    return;
    _32
    }
    _32
    handler.post(() ->
    _32
    {
    _32
    // Display the remote video stream
    _32
    SurfaceView surfaceView = null;
    _32
    if (fl_remote2.getChildCount() > 0) {
    _32
    fl_remote2.removeAllViews();
    _32
    }
    _32
    // Create the rendering view through RtcEngine
    _32
    surfaceView = new SurfaceView(context);
    _32
    surfaceView.setZOrderMediaOverlay(true);
    _32
    // Add the view to the remote container
    _32
    fl_remote2.addView(surfaceView, new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
    _32
    _32
    // Set the remote view
    _32
    engine.setupRemoteVideoEx(new VideoCanvas(surfaceView, RENDER_MODE_FIT, uid), rtcConnection2);
    _32
    });
    _32
    }
    _32
    };

Reference

This section contains content that completes the information on this page, or points you to documentation that explains other aspects to this product.

Sample project

Agora provides the JoinMultipleChannels open-source sample project for your reference. Download the project or view the source code for a more detailed example.

API reference

Interactive Live Streaming