switchToSpeaker method

Future<bool> switchToSpeaker()

Implementation

Future<bool> switchToSpeaker() async {
  Log().info('switchToSpeaker');
  _currentInput = AudioDevice(id: "Speaker", name: "Speaker",
      isInput: false, isOutput: true,
      type: AudioDeviceType.builtInSpeaker);
  final androidAudioManager =
  !kIsWeb && Platform.isAndroid ? AndroidAudioManager() : null;
  if (androidAudioManager != null) {
    await androidAudioManager.setMode(AndroidAudioHardwareMode.normal);
    await androidAudioManager.stopBluetoothSco();
    await androidAudioManager.setBluetoothScoOn(false);
    await androidAudioManager.setSpeakerphoneOn(true);
  } else {
    final avAudioSession = !kIsWeb && Platform.isIOS ? AVAudioSession() : null;
    if (avAudioSession != null) {
      Future.delayed(const Duration(milliseconds: 1), () async {
        await avAudioSession.setCategory(AVAudioSessionCategory.playAndRecord,
            AVAudioSessionCategoryOptions.defaultToSpeaker |
            AVAudioSessionCategoryOptions.mixWithOthers |
            AVAudioSessionCategoryOptions.allowBluetooth |
            AVAudioSessionCategoryOptions.allowAirPlay |
            AVAudioSessionCategoryOptions.allowBluetoothA2dp);
        await avAudioSession.setMode(AVAudioSessionMode.voiceChat);
        await avAudioSession.overrideOutputAudioPort(
            AVAudioSessionPortOverride.speaker);
      });
    }
  }
  return true;
}