Introduction

If the audience of a live broadcast wants to leave for another channel, usually you need to call the following methods:

  • The leaveChannel method to leave the current channel.
  • The joinChannelByToken method to join the new channel.

Starting with v2.9.0, the Agora Native SDK provides the switchChannel method for the audience to quickly switch between live broadcast channels while staying connected to Agora. With this method, you can achieve a much faster channel switch than with the leaveChannel and joinChannelByToken methods.

Implementation

Before implementing the quick switch function in your project, ensure that you have implemented the basic live broadcast in your project. For details, see the following documents:

After the audience joins a live broadcast channel, call the switchChannelByToken method to enable the audience to switch to another live broadcast channel. Pass in the token and channel name of the new channel in this method.

A successful channel switch with this method triggers the didLeaveChannel and didJoinChannel callbacks.

API call sequence

The following diagram shows the API call sequence for channel switching:

Sample code

You can refer to the following code snippets and implement quick switching in your project:

agoraKit.switchChannel(byToken: nil, channelId: channel.channelName, joinSuccess: nil)

We also provide an open-source demo project that implements Quick-Switch-iOS on GitHub. You can try the demo and view the source code of the switchChannel function in the MediaCenter.swift file.

API reference

Considerations

The switchChannelByToken method takes effect only when the client role is AUDIENCE.