switchToHeadphones method

Future<bool> switchToHeadphones()

Implementation

Future<bool> switchToHeadphones() async {
  Log().info('switchToHeadphones');
  final androidAudioManager =
  !kIsWeb && Platform.isAndroid ? AndroidAudioManager() : null;
  if (androidAudioManager != null) {
    androidAudioManager.setMode(AndroidAudioHardwareMode.inCommunication);
    androidAudioManager.stopBluetoothSco();
    androidAudioManager.setBluetoothScoOn(false);
    androidAudioManager.setSpeakerphoneOn(false);
    _currentInput = AudioDevice(id: "Headphone", name: "Headphone",
        isInput: false, isOutput: true,
        type: AudioDeviceType.headsetMic);
    return true;
  } else {
    final avAudioSession = !kIsWeb && Platform.isIOS ? AVAudioSession() : null;
    if (avAudioSession != null) {
      Future.delayed(const Duration(milliseconds: 1), () async {
        final result = await _switchToAnyIosPortIn(
            {AVAudioSessionPort.headsetMic});
      });
      // if (result == true) {
      //   _currentInput = AudioDevice(id: "Headphone", name: "Headphone",
      //       isInput: false, isOutput: true,
      //       type: AudioDeviceType.headsetMic);
      // }
      // return result;
      _currentInput = AudioDevice(id: "Headphone",
          name: "Headphone",
          isInput: false,
          isOutput: true,
          type: AudioDeviceType.headsetMic);
      return true; // Always return true to avoid delay update ui
    }
  }
  return true;
}