Custom Audio Track - Android
You can pass a custom audio track with a different encoder configuration for a participant's audio.
Creating a Custom Audio Track
- You can create an audio track using
createMicrophoneAudioTrack()method ofVideoSDK. - This method can be used to create audio track using different encoding parameters, microphone and audio processing settings.
- The method opens the microphone, so request the
RECORD_AUDIOpermission before you call it.
Parameters
-
encoderConfig:
- type:
String - required:
false - default:
speech_standard - Allowed values :
speech_low_quality|speech_standard|music_standard|standard_stereo|high_quality|high_quality_stereo - It will be the encoder configuration you want to use for Audio Track.
- type:
-
microphoneId:
- type:
String - required:
false - The
deviceIdof a microphone fromVideoSDK.getAudioDevices(). If not passed, the current microphone is used.
- type:
-
noiseConfig:
- type:
NoiseConfig - required:
false - Turns noise suppression, echo cancellation, automatic gain control and the high-pass filter on or off. If not passed, the values of the
encoderConfigprofile are used. If passed, a setting you leave out takes its default: the first three on, the high-pass filter off.
- type:
-
context
- type:
Context - required:
true - Pass the Android Context for this parameter.
- type:
Returns
CustomStreamTrack?. It is nevernull: if the track can't be created, the method throwsVideoSDKExceptioninstead, for example3018whenRECORD_AUDIOhas not been granted.- Called on the main thread, the method returns before the microphone has opened. If the microphone then fails to open (for example
3024when another app is using it), the track ends and the error goes to thetrack.onEnded = { error -> }handler. - A track you have not published keeps the microphone open. If you don't publish it, call
track.stop()to release the microphone.
Example
try {
val audioCustomTrack: CustomStreamTrack? = VideoSDK.createMicrophoneAudioTrack(
encoderConfig = "high_quality",
context = this,
)
} catch (e: VideoSDKException) {
Log.e("VideoSDK", "Audio track creation failed: ${e.code} ${e.name}")
}
Using Custom Audio Track
Custom Track while initializing the meeting
If you are passing micEnabled = true in the initMeeting of VideoSDK and want to use custom tracks from start of the meeting, you can pass custom track in the initMeeting as shown below.
override fun onCreate(savedInstanceState: Bundle?) {
//..
val customTracks: MutableMap<String, CustomStreamTrack> = HashMap()
try {
val audioCustomTrack = VideoSDK.createMicrophoneAudioTrack(
encoderConfig = "high_quality",
context = this,
)
customTracks["mic"] = audioCustomTrack!! //Key must be "mic"
} catch (e: VideoSDKException) {
Log.e("VideoSDK", "Audio track creation failed: ${e.code} ${e.name}")
}
// create a new meeting instance
val meeting = VideoSDK.initMeeting(
context = this@MainActivity,
meetingId = meetingId,
name = participantName,
//MicEnabled , If true, it will use the passed custom track to turn mic on
micEnabled = true,
//WebcamEnabled
webcamEnabled = true,
//Pass the custom tracks here
customTracks = customTracks,
)
}
Custom Track with unmuteMic()
To turn the mic on with a custom track during the meeting, pass the CustomStreamTrack to the unmuteMic() method of Meeting. It is a suspend function, so call it from a coroutine.
You can also pass custom track in changeMic() method of Meeting.
unmuteMic() throws VideoSDKException with code 3054 when the mic is already on. To switch a mic that is on to a new track, use changeMic(), which mutes the mic and unmutes it with the new track.
lifecycleScope.launch {
try {
val audioCustomTrack = VideoSDK.createMicrophoneAudioTrack(
encoderConfig = "high_quality",
context = this@MainActivity,
)
if (meeting.isMicEnabled) {
// the mic is on: switch it to the new track
meeting.changeMic(audioCustomTrack!!)
} else {
meeting.unmuteMic(audioCustomTrack)
}
} catch (e: VideoSDKException) {
Log.e("VideoSDK", "Mic change failed: ${e.code} ${e.name}")
}
}
Got a Question? Ask us on discord

