Skip to main content
Version: 4.x.x

Custom Audio Sources - iOS

For a high-quality streaming experience, fine-tuning audio tracks becomes essential—especially when delivering content to a broader live audience.

You can provide a custom audio track for a host's stream both before and during a live session.

How to Create Custom Audio Track ?​

  • You can create an Audio Track using createMicrophoneAudioTrack() method of VideoSDK class.

  • This method can be used to create audio track using different noise configurations. It returns a CustomStreamTrack, or throws if the track can't be created.

  • It is async throws, so call it with try await inside a Task.

  • Every flag of NoiseConfig is optional. A flag you leave out keeps its default: noiseSuppression, echoCancellation and autoGainControl are true, and highPassFilter is false.

  • To capture from a specific microphone, pass its deviceId from VideoSDK.getMics() as microphoneId. If no microphone matches, the method throws ERROR_INVALID_CUSTOM_TRACK_CONFIG.

  • The microphone opens when the track is created, but the method never asks for microphone permission. Ask for permission first, for example with await AVCaptureDevice.requestAccess(for: .audio), 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 its onEnded handler receives the error.

  • If you create a track and don't use it, call stop() on it to release the microphone.

Example​

Task {
do {
let audioMediaTrack = try await VideoSDK.createMicrophoneAudioTrack(
// Optional: the deviceId of the microphone to use, from VideoSDK.getMics().
microphoneId: nil, // 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 before and after the initialization of the Room.

  1. Setting up a Custom Track during the initialization of a room
  2. Setting up a Custom Track with methods
1. Setting up a Custom Track during the initialization of a room​

If you are passing micEnabled: true in VideoSDK.createRoom() and want to use custom tracks from the start of the live stream, you can pass the custom track as customMicrophoneTrack in createRoom() as shown below.

caution

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​
Task {
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 live stream: \(error)")
}
}

2. Setting up a Custom Track with methods​

In order to switch tracks during the live stream, 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.

tip

If changeMic(customMicrophoneTrack:) fails, the SDK stops the track you passed. Create a new track before you try again.

Example​
@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