Skip to main content
Version: 3.x.x

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 of VideoSDK.
  • 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_AUDIO permission 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.
  • microphoneId:

    • type: String
    • required: false
    • The deviceId of a microphone from VideoSDK.getAudioDevices(). If not passed, the current microphone is used.
  • 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 encoderConfig profile are used. If passed, a setting you leave out takes its default: the first three on, the high-pass filter off.
  • context

    • type: Context
    • required: true
    • Pass the Android Context for this parameter.

Returns​

  • CustomStreamTrack?. It is never null: if the track can't be created, the method throws VideoSDKException instead, for example 3018 when RECORD_AUDIO has 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 3024 when another app is using it), the track ends and the error goes to the track.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.

note

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