The Agora RTC SDK enables you to manage the volume of the recorded audio or of the audio playback according to your actual scenario. For example, to mute a remote user in a one-to-one call, you can set the audio playback volume as 0.
This article provides the APIs and additional information relating to audio capturing, audio mixing, and audio playback volume settings.
We provide an open-source JoinChannelAudio.swift sample project that implements adjusting the capturing and playback on GitHub. You can download the sample project and view the source code.
Before adjusting the audio volume, ensure that you have implemented the basic real-time communication functions in your project. For details, see Start a Call or Start Live Interactive Streaming.
Capturing is the process of sampling audio by a capturing device and transmitting the captured signal to the sender. To adjust the capturing volume, you can set the volume of the capturing device or set the volume of the captured signal. We recommend using audio device-related APIs to adjust the capturing volume.
Call setDeviceVolume
to set the volume of your capturing device.
The volume
parameter represents the audio level of your capturing device, ranging between 0 and 255:
As the following screenshot shows, the volume value corresponds to the audio level for your audio capturing device.
Sample code
// swift
// Sets the volume of your capturing device as 50.
agoraKit.setDeviceVolume(.audioRecording, volume: 50)
// objective-c
// Sets the volume of your capturing device as 50.
[agoraKit setDeviceVolume: AgoraMediaDeviceTypeAudioRecording volume: 50];
If setDeviceVolume
does not suffice, you can call adjustRecordingSignalVolume
to set the volume of the signal captured by the microphone or call adjustLoopbackRecordingSignalVolume
to set the volume of the signal captured by the sound card.
The volume
parameter represents the audio level of the captured signal, ranging between 0 and 100. The default value is 100, which represents the original volume.
Sample code
// Swift
// Sets the volume of the signal captured by the microphone as 50.
agoraKit.adjustRecordingSignalVolume(50)
// Sets the volume of the signal captured by the sound card as 50.
agoraKit.adjustLoopbackRecordingSignalVolume(50)
// Objective-C
// Sets the volume of the signal captured by the microphone as 50.
[agoraKit adjustRecordingSignalVolume: 50];
// Sets the volume of the signal captured by the sound card as 50.
[agoraKit adjustLoopbackRecordingSignalVolume: 50];
Playback is the process of playing the received audio signal on the local playback device. To adjust the playback volume, you can set the volume of the playback device, or set the volume of the audio signal.
To set the volume on the playback device directly, call setDeviceVolume
.
The volume
parameter represents the audio level of your playback device, ranging between 0 and 255:
As the following screenshot shows, the volume value corresponds to the audio level for your audio playback device.
Sample code
// Swift
// Sets the volume of the playback device as 50.
agoraKit.setDeviceVolume(.audioPlayout, volume: 50)
// Objective-C
// Sets the volume of the playback device as 50.
[agoraKit setDeviceVolume: AgoraMediaDeviceTypeAudioPlayout volume: 50];
If setDeviceVolume
does not suffice, you can use adjustPlaybackSignalVolume
or adjustUserPlaybackSignalVolume
to set the volume of the audio signal.
adjustPlaybackSignalVolume
:volume
parameter represents the playback audio level, ranging between 0 and 400. adjustUserPlaybackSignalVolume
:volume
parameter represents the playback audio level, ranging between 0 and 100. Sample code
// Swift
// Sets the volume of the local playback of all remote users as 50.
agoraKit.adjustPlaybackSignalVolume(50)
// Sets the volume of the local playback of a specified remote user as 50.
agoraKit.adjustUserPlaybackSignalVolume(uid, volume: 50)
// Objective-C
// Sets the volume of the local playback of all remote users as 50.
[agoraKit adjustPlaybackSignalVolume: 50];
// Sets the volume of the local playback of a specified remote user as 50.
[agoraKit adjustUserPlaybackSignalVolume: uid, volume: 50];
setDeviceVolume
adjustPlaybackSignalVolume
adjustUserPlaybackSignalVolume
adjustAudioMixingPlayoutVolume
When capturing, mixing, or playing audio, you can use the following methods to get the data of the loudest speaker in the channel.
Reports users with the highest peak volumes. The reportAudioVolumeIndicationOfSpeakers
reports the user IDs the corresponding volumes of the currently loudest speakers in the channel.
Sample code
// Swift
// Gets the user IDs of the users with the highest peak volume, the corresponding volumes, as well as whether the local user is speaking.
// @param speakers is an array containing the user IDs and volumes of the local and the remote users. The volume parameter ranges between 0 and 255.
// @param totalVolume refers to the total volume after audio mixing, ranging between 0 and 255.
func rtcEngine(_ engine: AgoraRtcEngineKit, reportAudioVolumeIndicationOfSpeakers speakers:
[AgoraRtcAudioVolumeInfo], totalVolume: Int) {
}
// Objective-C
// Gets the user IDs of the users with the highest peak volume, the corresponding volumes, as well as whether the local user is speaking.
// @param speakers is an array containing the user IDs and volumes of the local and the remote users. The volume parameter ranges between 0 and 255.
// @param totalVolume refers to the total volume after audio mixing, ranging between 0 and 255.
- (void)rtcEngine:(AgoraRtcEngineKit *_Nonnull)engine reportAudioVolumeIndicationOfSpeakers:(NSArray<AgoraRtcAudioVolumeInfo*> *_Nonnull)speakers totalVolume:(NSInteger)totalVolume {
}
Reports the user with the highest average volume. The activeSpeaker
callback reports the user ID with the highest average volume during a certain period of time.
Sample code
// Swift
// Gets the user ID of the user with the highest average volume during a certain period of time.
func rtcEngine(_ engine: AgoraRtcEngineKit, activeSpeaker speakerUid: UInt) {
}
// Objective-C
// Gets the user ID of the user with the highest average volume during a certain period of time.
- (void)rtcEngine:(AgoraRtcEngineKit *_Nonnull)engine activeSpeaker:(NSUInteger)speakerUid {
}
Setting the audio level too high may cause audio distortion on some devices.
When adjusting the audio volume, you can also refer to the following articles: