Set the Subscribing State
Overview
In real-time engagement, you may need to set the subscribing state of audio and video streams according to your business scenarios. For example, after joining a channel, a user needs to unsubscribe from a remote user.
This article describes how to set the subscribing state in common scenarios.
API introduction
By default, users subscribe to all remote users' audio and video streams when joining a channel or switching to a channel. You can call the following APIs to set the subscribing state:
| API | Target | Requirements for calling the API | 
|---|---|---|
| autoSubscribeAudioorautoSubscribeVideoofchannelMediaOptions | All remote users | Set this API when calling joinChannel[2/2] orswitchChannel[2/2] | 
| muteAllRemoteAudioStreamsormuteAllRemoteVideoStreams | All remote users | Call at any time in the channel | 
| muteRemoteAudioStreamormuteRemoteVideoStream | A specified remote user | Call at any time in the channel | 
muteAll or muteRemote can override the subscribing settings when joining a channel or switching to a channel.muteAll and muteRemote are independent of each other. When the two methods are called together, the method that is called later takes effect.Recommended settings
Scenario one: Unsubscribe from specified users in a channel
If the user subscribes to all remote users when joining a channel or switching to a channel, the user may need to unsubscribe from one or more remote users in the channel. Agora recommends the following steps:
- Use the default settings of the SDK when joining a channel or switching to a channel.
- In the channel, call muteRemoteAudioStream(uid,true) ormuteRemoteVideoStream(uid,true) to unsubscribe from specified remote users.
Scenario two: Subscribe to specified users when joining a channel
The user may need to subscribe to one or more remote users when joining a channel or switching to a channel. Agora recommends the following steps:
- Call joinChannel[2/2] orswitchChannel[2/2] and setautoSubscribeAudio(false)orautoSubscribeVideo(false)to unsubscribe from all remote users.
- In the channel, call muteRemoteAudioStream(uid,false) ormuteRemoteVideoStream(uid,false)to subscribe to specified remote users.
Scenario three: Subscribe to different remote users in a channel
The user may need to subscribe to one or more remote users when joining a channel or switching to a channel, and then subsequently change the subscribed remote users. Agora recommends the following steps:
- Refer to Scenario Two to subscribe to specified remote users when joining a channel or switching to a channel.
- When the user needs to change the subscribed remote users, you can first call muteRemoteAudioStream(uid, true)ormuteRemoteVideoStream(uid, true)to unsubscribe the remote users you no longer need, and then callmuteRemoteAudioStream(uid, false)ormuteRemoteVideoStream(uid, false)to subscribe to the remote users you need.
muteAllRemoteAudioStreams(true) and muteAllRemoteVideoStreams(true) to unsubscribe all remote users first, and then call muteRemoteAudioStream (uid, false) or muteRemoteVideoStream(uid, false) to subscribe to the remote users you need.mute method to unsubscribe a remote user and then immediately subscribe to that user can cause a momentary lag in that user's audio and video, so Agora recommends that you use muteRemote instead of muteAllRemote to unsubscribe if you have subscribed to a remote user both before and after changing.Scenario four: Set the subscribing state when interrupted by a system or third-party application event
In real-time engagement, the user may be interrupted by a system or third-party application event, such as receiving a system phone call. You typically need to set the subscribing state in two circumstances:
- When the user starts using a system or third-party application and this application occupies the audio or video modules of the device, Agora recommends unsubscribing from all users.
- When the user stops using a system or third-party application, Agora recommends resuming the original subscribing state.
The logic of subscribing settings varies according to the original subscribing state.
- 
If the user subscribes to all remote users in real-time engagement, Agora recommends setting the subscribing state as follows: - When the user starts using a system or third-party application, call muteAllRemoteAudioStreams(true)ormuteAllRemoteVideoStreams(true)to unsubscribe from all remote users.
- When the user stops using a system or third-party application, call muteAllRemoteAudioStreams(false)ormuteAllRemoteVideoStreams(false)to resume subscribing to all remote users.
 
- When the user starts using a system or third-party application, call 
- 
If the user subscribes to some remote users in real-time engagement, Agora recommends setting the subscribing state as follows: - 
Record the original subscribing state. For example, the user only subscribes to remote users A and B. 
- 
When the user starts using a system or third-party application, call muteAllRemoteAudioStreams(true)ormuteAllRemoteVideoStreams(true)to unsubscribe from all remote users.
- 
When the user stops using system or third-party applications, resume the original subscribing state according to the records in step one. For example, call the following methods to resume subscribing to remote users A and B: - Call muteRemoteAudioStream(A, false)andmuteRemoteVideoStream(A, false)to resume subscribing to remote user A.
- Call muteRemoteAudioStream(B, false)andmuteRemoteVideoStream(B, false)to resume subscribing to remote user B.
 
- Call 
 
- 
API changes
As of v3.3.0, Agora adds joinChannel[2/2] and switchChannel[2/2], and modifies the logic for using mute-related methods. If you need to upgrade to v3.3.0 or later, Agora recommends that you adjust your subscribing settings as follows to avoid any impact on your business functions:
Earlier than v3.3.0:
- 
You cannot set the subscribing state when you join a channel or switch channels. By default, you subscribe to the audio or video streams in the current channel. 
- 
mute-related methods can be called both before and after joining a channel or switching to a channel.
- 
The methods with the prefix muteAll(hereinafter referred to asmuteAll) serve as a master switch, and the methods with the prefixmuteRemoteorsetDefaultMute(hereinafter referred to asmuteRemoteandsetDefaultMute) serve as sub-switches.
- 
Call muteAll(true)to disconnect the master switch, and the user unsubscribes from all audio and video streams.muteRemoteorsetDefaultMutedoes not take effect.
- 
Call muteAll(false)to connect the master switch.muteRemoteorsetDefaultMutecontrols the user subscribing state.- Set trueto disconnect the sub-switches, and the user unsubscribes from streams.
- Set falseto connect the sub-switches, and the user subscribes to streams.
 
- Set 
- 
muteAll(false)does not resume subscribing to all audio or video streams, but only the original subscribing state.
As of v3.3.0:
- Deprecate setDefaultMuteAllRemoteAudioStreamsandsetDefaultMuteAllRemoteVideoStreams.
- When joining a channel or switching to a channel, call joinChannel[2/2] orswitchChannel[2/2] to set the subscribing state.
- mute-related methods must be called after joining a channel or switching to a channel; otherwise these methods do not take effect.
- muteAllmethods do not serve as a master switch. Each- mute-related method can independently control the user's subscribing state.- When muteAllandmuteRemotemethods are called together, the method that is called later takes effect.
- muteAllcontains the function of- setDefaultMute. Agora does not recommend calling- muteAlland- setDefaultMutetogether; otherwise the settings may not take effect.
 
- When 
- muteAll(false)resumes subscribing to all audio or video streams.
Corresponding methods in different programming languages
| Java/C++ | Objective-C | C# | Typescript/Dart | 
|---|---|---|---|
| joinChannel[2/2] | joinChannelByToken[2/2] | JoinChannel[2/2] | joinChannel | 
| switchChannel[2/2] | switchChannelByToken[2/2] | SwitchChannel[2/2] | switchChannel | 
| muteAllRemoteAudioStreams | muteAllRemoteAudioStreams | MuteAllRemoteAudioStreams | muteAllRemoteAudioStreams | 
| muteAllRemoteVideoStreams | muteAllRemoteVideoStreams | MuteAllRemoteVideoStreams | muteAllRemoteVideoStreams | 
| muteRemoteAudioStream | muteRemoteAudioStream | MuteRemoteAudioStream | muteRemoteAudioStream | 
| muteRemoteVideoStream | muteRemoteVideoStream | MuteRemoteVideoStream | muteRemoteVideoStream |