createMicrophoneAudioTrack static method

Future<CustomStreamTrack> createMicrophoneAudioTrack({
  1. String? microphoneId,
  2. CustomMicrophoneTrackConfig encoderConfig = CustomMicrophoneTrackConfig.SPEECH_STANDARD,
  3. NoiseConfig? noiseConfig,
})

Creates a microphone track before joining. Pass the result to createRoom.

The microphone opens here, and CustomStreamTrack.stop closes it until a room takes the track. This never asks for permission: get it first with requestPermissions. A failure throws VideoSDKError, for example 3018 without microphone permission, 3022 when there is no microphone, 3024 when it is in use, or 3071 for an unknown microphoneId.

Passing a noiseConfig means it decides every flag: one it leaves unset is on (highPassFilter off), not whatever encoderConfig's profile sets. Leave it out to take the profile's values, which the music and high-quality profiles turn off.

Implementation

static Future<CustomStreamTrack> createMicrophoneAudioTrack({
  String? microphoneId,
  CustomMicrophoneTrackConfig encoderConfig =
      CustomMicrophoneTrackConfig.SPEECH_STANDARD,
  NoiseConfig? noiseConfig,
}) async {
  final handle = await _channel.invokeMethod<Map<dynamic, dynamic>>(
    'track.createMicrophoneAudio',
    {
      'microphoneId': microphoneId,
      'encoderConfig': encoderConfig.name.toLowerCase(),
      'noiseConfig': noiseConfig?.toJson(),
    },
  );
  if (handle == null) throw VideoSDKError.ERROR_AUDIO_TRACK_CREATION_FAILED;

  final trackId = handle['trackId'] as String? ?? '';
  return CustomStreamTrack.audioTrack(
    mediaStream: NativeMediaStream(
      id: trackId,
      ownerTag: 'local',
      tracks: [MediaStreamTrack(id: trackId, kind: 'audio')],
    ),
    audioEncoderConfig: encoderConfig,
    noiseConfig: noiseConfig,
    microphoneId: microphoneId,
  );
}