ryanheise / audio_session

MIT License
115 stars 81 forks source link

Headphone's mic to speaker #93

Closed rossaai closed 1 year ago

rossaai commented 1 year ago

I'm struggiling to achive a way to record audio from headphones' mic and play it to speaker. It's not posible record audio from phone's mic because appers Larson Effect

It's posible record audio from phone's mic and play it to headphones but the other way around is not possible.

Init code

  final _speakerPlayer = ap.AudioPlayer();
  FlutterSoundPlayer? _mPlayer = FlutterSoundPlayer();
  FlutterSoundRecorder? _mRecorder = FlutterSoundRecorder();
  bool _isInited = false;

  Future<void> init() async {
    await _mRecorder!.openRecorder();
    await _mPlayer!.openPlayer();

    final context = ap.AudioContextConfig(forceSpeaker: true).build();
    _speakerPlayer.setAudioContext(context);

    final session = await AudioSession.instance;
    await session.configure(AudioSessionConfiguration(
      avAudioSessionCategory: AVAudioSessionCategory.playAndRecord,
      avAudioSessionCategoryOptions:
          AVAudioSessionCategoryOptions.allowBluetooth |
              AVAudioSessionCategoryOptions.defaultToSpeaker,
      avAudioSessionMode: AVAudioSessionMode.spokenAudio,
      avAudioSessionRouteSharingPolicy:
          AVAudioSessionRouteSharingPolicy.defaultPolicy,
      avAudioSessionSetActiveOptions: AVAudioSessionSetActiveOptions.none,
      androidAudioAttributes: const AndroidAudioAttributes(
        contentType: AndroidAudioContentType.speech,
        flags: AndroidAudioFlags.audibilityEnforced,
        usage: AndroidAudioUsage.media,
      ),
      androidAudioFocusGainType: AndroidAudioFocusGainType.gain,
      androidWillPauseWhenDucked: true,
    ));
  }

Android control

    final androidAudioManager = AndroidAudioManager();

    // //For BT (Record and play on BT headphones)
    // androidAudioManager.setMode(AndroidAudioHardwareMode.inCommunication);
    // androidAudioManager.startBluetoothSco();
    // androidAudioManager.setBluetoothScoOn(true);

    // //For phone ear piece
    // androidAudioManager.setMode(AndroidAudioHardwareMode.inCommunication);
    // androidAudioManager.stopBluetoothSco();
    // androidAudioManager.setBluetoothScoOn(false);
    // androidAudioManager.setSpeakerphoneOn(false);

    // //For phone speaker(loadspeaker)
    // await androidAudioManager.setMode(AndroidAudioHardwareMode.normal);
    // await androidAudioManager.stopBluetoothSco();
    // await androidAudioManager.setBluetoothScoOn(false);
    // await androidAudioManager.setSpeakerphoneOn(true);

Combination BT and setSpeakerphoneOn nothing happens. setSpeakerphoneOn ignores Bluetooth's mic on totally and record audio in device microphone

    androidAudioManager.setMode(AndroidAudioHardwareMode.inCommunication);
    androidAudioManager.startBluetoothSco();
    androidAudioManager.setBluetoothScoOn(true);
    await androidAudioManager.setSpeakerphoneOn(true);
    // await androidAudioManager.setMicrophoneMute(true);  // Mute both mics
ryanheise commented 1 year ago

I think this question would be best asked on stack overflow. To get an answer, I would try to ask two separate questions, one to the iOS native developers and one to the native Android developers. Of course, search the existing questions first since it's likely Android and iOS developers have encountered this challenge before. Since this is not an issue with audio_session itself that I need to add code for (since this plugin already exposes all of the native APIs), I'll close this.