Optimize Audio Track - iOS
While optimizing for the best listening experience, it is necessary to fine-tune the audio track that is being used during the calls.
You can pass a custom audio track for the participant's media before joining the room and while in it.
Custom Audio Track
This feature can be used to create a custom Audio track for the Audio of the participants with different encoder configuration and send it to other participants.
Once you set a custom audio track created using createMicrophoneAudioTrack(), the SDK remembers its settings and automatically reuses them whenever the mic is turned back on. To change the track configuration, pass a new track created the same way to changeMic(customMicrophoneTrack:). The SDK preserves only tracks created with this method.
Encoder Config Reference
| Encoder Config | Bitrate | Auto Gain | Echo Cancellation | Noise Suppression |
|---|---|---|---|---|
| speech_low_quality | 24 kbps | TRUE | TRUE | TRUE |
| speech_standard | 24 kbps | TRUE | TRUE | TRUE |
| music_standard | 40 kbps | FALSE | FALSE | FALSE |
| standard_stereo | 64 kbps | FALSE | FALSE | FALSE |
| high_quality | 128 kbps | FALSE | FALSE | FALSE |
| high_quality_stereo | 192 kbps | FALSE | FALSE | FALSE |
Note: If you face issues related to echo, you can use noiseConfig with echoCancellation: true. Echo cancellation attempts to prevent echo effects on a two-way audio connection by reducing or eliminating crosstalk between the user's output device and their input device — for example, filtering out sound produced by the speakers from being captured by the microphone input track.
How to Create Custom Audio Track ?
-
You can create an audio track using
createMicrophoneAudioTrack()method ofVideoSDKclass. -
This method can be used to create audio track using different noise configurations. Every
NoiseConfigflag is optional:noiseSuppression,echoCancellationandautoGainControldefault totrue, andhighPassFilterdefaults tofalse. -
The method is
async throws, so call it withtry await. It returns aCustomStreamTrack. -
To capture from a specific microphone, pass its
deviceIdfromVideoSDK.getMics()asmicrophoneId. -
The microphone opens when the track is created, but the method never asks for microphone permission. Ask for permission first, and create the track after access is granted. Until then, the method throws
ERROR_MICROPHONE_ACCESS_DENIED_OR_DISMISSED. -
The method waits up to 2 seconds for the microphone to open, and throws the errors that happen in that time, such as
ERROR_MICROPHONE_IN_USE. If the microphone fails after that, the track ends and itsonEndedhandler receives the error. -
If you create a track and don't use it, call
stop()on it to release the microphone.
Example
- Swift
do {
let audioMediaTrack = try await VideoSDK.createMicrophoneAudioTrack(
encoderConfig: .speech_standard, // Default : .speech_standard
microphoneId: nil, // A deviceId from VideoSDK.getMics(). Default : nil
noiseConfig: NoiseConfig(
// It is used to improve the quality of audio by removing background noise
// that can interfere with the clarity of speech.
noiseSuppression: true,
// It is used to remove unwanted echoes from voice.
echoCancellation: true,
// It is used to maintain a consistent level of loudness or amplitude in a voice.
autoGainControl: true,
// It removes low frequency sounds hence making the audio clearer.
highPassFilter: true
)
)
// Pass audioMediaTrack to createRoom() or changeMic(customMicrophoneTrack:)
} catch {
print("Failed to create the audio track: \(error)")
}
How to Setup Custom Audio Track ?
The custom track can be set up both when you create the room and after you join it.
1. Setting up a Custom Track when you create the room
If you are passing micEnabled: true to VideoSDK.createRoom and want to use custom tracks from the moment you join the room, you can pass custom track as customMicrophoneTrack to createRoom as shown below.
With micEnabled: false, the mic stays off when you join, so the custom track is not sent. When you turn the mic on later with unmuteMic(), the SDK uses the settings of this track.
Example
- Swift
do {
let customMicrophoneTrack = try await VideoSDK.createMicrophoneAudioTrack(
noiseConfig: NoiseConfig(
noiseSuppression: true,
echoCancellation: true,
autoGainControl: true,
highPassFilter: true
)
)
let room = try VideoSDK.createRoom(
roomId: roomId,
token: token,
participantName: name,
micEnabled: true, // when passed true, it creates an audio track with defined configs
camEnabled: cameraEnabled, // optional, default: true
// Pass the custom audio track here which will be used when the mic is auto started
customMicrophoneTrack: customMicrophoneTrack
)
} catch {
print("Failed to set up the room: \(error)")
}
2. Setting up a Custom Track with methods
In order to switch tracks while in the room, you have to pass the new track to the changeMic(customMicrophoneTrack:) method of Room. If the mic is on, the SDK turns it off and on again with the new track, so the new noise settings apply at once. If the mic is off, it turns on with the new track.
You can also pass a track to unmuteMic(customMicrophoneTrack:) when you turn the mic on. Both methods are async throws, so call them with try await inside a Task.
Example
- Swift
@IBAction func changeAudioTrackTapped(_ sender: Any) {
Task {
do {
let audioMediaTrack = try await VideoSDK.createMicrophoneAudioTrack(
noiseConfig: NoiseConfig(
noiseSuppression: true,
echoCancellation: true,
autoGainControl: true,
highPassFilter: true
)
)
try await self.room?.changeMic(customMicrophoneTrack: audioMediaTrack)
} catch {
print("Failed to change the audio track: \(error)")
}
}
}
API Reference
The API references for all the methods and events utilised in this guide are provided below.
Got a Question? Ask us on discord

