getAvailableAudioInputs method

List<AudioDevice> getAvailableAudioInputs()

Implementation

List<AudioDevice> getAvailableAudioInputs() {
  var availableInputs = AudioManager().getAvailableAudioInputs();
  print('getAvailableAudioInputs inputs = $availableInputs');

  availableInputs.removeWhere((item) => (item.type == AudioDeviceType.telephony ||
      item.type == AudioDeviceType.builtInEarpiece));

  // add Mute/Unmute option
  bool muteExist = false;
  for (AudioDevice inp in availableInputs) {
    if (inp.name == "Mute" || inp.name == "Unmute") {
      muteExist = true;
      break;
    }
  }
  // Remove if has any Mute/Unmute option
  if (muteExist == true) {
    availableInputs.removeAt(0);
  }
  // Add Mute/Unmute
  if (AudioManager().soundMuted == true) {
    print('getAvailableAudioInputs add Unmute');
    AudioDevice unmute = AudioDevice(id: "Unmute",
        name: "Unmute",
        isInput: false,
        isOutput: false,
        type: AudioDeviceType.unknown);
    availableInputs.insert(0, unmute);
  } else {
    print('getAvailableAudioInputs add Mute');
    AudioDevice mute = AudioDevice(id: "Mute",
        name: "Mute",
        isInput: false,
        isOutput: false,
        type: AudioDeviceType.unknown);
    availableInputs.insert(0, mute);
  }

  // Add earPiece option
  // bool earPieceExist = false;
  // for (AudioDevice inp in availableInputs) {
  //   if (inp.type == AudioDeviceType.builtInEarpiece) {
  //     earPieceExist = true;
  //     break;
  //   }
  // }
  // if (earPieceExist == true) {
  //   availableInputs.removeWhere((item) => item.type == AudioDeviceType.builtInEarpiece);
  // }
  // if (AudioManager().checkCurrentIsHeadphoneOrBluetooth() == false) {
  //   AudioDevice earpiece = AudioDevice(id: "Earpiece",
  //       name: "Earpiece",
  //       isInput: false,
  //       isOutput: true,
  //       type: AudioDeviceType.builtInEarpiece);
  //   availableInputs.insert(1, earpiece);
  // }

  // Add speaker option
  bool speakerExist = false;
  for (AudioDevice inp in availableInputs) {
    if (inp.type == AudioDeviceType.builtInSpeaker) {
      speakerExist = true;
      break;
    }
  }
  if (speakerExist == true) {
    availableInputs.removeWhere((item) => item.type == AudioDeviceType.builtInSpeaker);
  }
  if (AudioManager().checkCurrentIsHeadphoneOrBluetooth() == false) {
    AudioDevice speaker = AudioDevice(id: "Speaker",
        name: "Speaker",
        isInput: false,
        isOutput: true,
        type: AudioDeviceType.builtInSpeaker);
    availableInputs.insert(1, speaker);
  }
  return availableInputs;
}