setAudioEnabled method

Future<void> setAudioEnabled(
  1. bool enabled
)

Enables/disables local audio stream based on enabled.

Implementation

Future<void> setAudioEnabled(bool enabled) async {
  Log.debug('setAudioEnabled($enabled)', '$runtimeType');

  switch (audioState.value) {
    case LocalTrackState.disabled:
    case LocalTrackState.disabling:
      if (enabled) {
        audioState.value = LocalTrackState.enabling;
        try {
          if (!hasAudio) {
            await _room?.enableAudio();

            final List<LocalMediaTrack> tracks = await MediaUtils.getTracks(
              audio: AudioPreferences(device: audioDevice.value?.deviceId()),
            );
            tracks.forEach(_addLocalTrack);
          }
          await _room?.unmuteAudio();
          audioState.value = LocalTrackState.enabled;
        } on MediaStateTransitionException catch (_) {
          // No-op.
        } on LocalMediaInitException catch (e) {
          audioState.value = LocalTrackState.disabled;
          if (e.message().contains('Permission denied')) {
            _notifications.add(MicrophonePermissionDeniedNotification());
          } else {
            addError('unmuteAudio() call failed due to ${e.message()}');
            rethrow;
          }
        } catch (e) {
          audioState.value = LocalTrackState.disabled;
          addError('unmuteAudio() call failed with $e');
          rethrow;
        }
      }
      break;

    case LocalTrackState.enabled:
    case LocalTrackState.enabling:
      if (!enabled) {
        audioState.value = LocalTrackState.disabling;
        try {
          await _room?.muteAudio();
          audioState.value = LocalTrackState.disabled;
        } on MediaStateTransitionException catch (_) {
          // No-op.
        } catch (e) {
          audioState.value = LocalTrackState.enabled;
          addError('muteAudio() call failed with $e');
          rethrow;
        }
      }
      break;
  }
}