Canardoux / flutter_sound

Flutter plugin for sound. Audio recorder and player.
Mozilla Public License 2.0
877 stars 572 forks source link

Use same audio engine for player and recorder in IOS #1109

Open parkjihwanjay opened 4 days ago

parkjihwanjay commented 4 days ago

Is your feature request related to a problem? Please describe. When I use flutter sound player and recorder for real time communication through server in IOS, the sound gets played through built-in-receiver, not built-in-speaker. I configured audio session initially like this, but it is same.

    await Permission.microphone.request();

    final session = await AudioSession.instance;

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

    await AVAudioSession().overrideOutputAudioPort(AVAudioSessionPortOverride.speaker);

    await session.setActive(true);

    await _recorder.openRecorder();
    await _player.openPlayer();

Describe the solution you'd like To solve this problem, player and recorder have to use same audio engine in IOS and setVoiceProcessingEnabled as true of AVAudioInputNode as true before recording

    let inputNode: AVAudioInputNode
    try inputNode.setVoiceProcessingEnabled(true)

Describe alternatives you've considered I don't the native code of flutter_sound, but it seems like player and recorder does not use same audio engine in IOS. That might be why sound plays in receiver rather than speaker.

I'd like to suppose player and recorder use same audio engine in IOS and setVoiceProcessingEnabled as true before recording.

Additional context If there is solution to this problem, please let me know. Thanks : )

Larpoux commented 4 days ago

Actually Flutter Sound 9.x use the default mic and the default speaker. There is no possibility to select a specific mic and/or a specific speaker. This is really something missing in 9.x.

I am currently working on Flutter Sound 10.x, and it will be possible in this future version to select the speaker and the mic. Unfortunately this new release is far from being released. There is much work to do on this new version because everything is re-coded differently. I expect to release an alpha version at the end of the year or beginning of next year, but only for web. Port on mobiles and desktop will be later during 2025.

parkjihwanjay commented 4 days ago

Then in 9.x, is there way to solve the problem that the sound is played through receiver rather than speaker when set to voiceChat mode?