Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

iOS randomly chooses speaker and earpiece #862

Closed
tGrothmannFluffy opened this issue Mar 7, 2024 · 3 comments
Closed

iOS randomly chooses speaker and earpiece #862

tGrothmannFluffy opened this issue Mar 7, 2024 · 3 comments

Comments

@tGrothmannFluffy
Copy link

tGrothmannFluffy commented Mar 7, 2024

Hi there,

after somewhat mitigating another ios issue (#842), I am still having trouble with the audio devices.

Whenever I open an audio output stream:

let host = cpal::default_host();
let device = host
    .default_output_device()
    .expect("no output device available");
let config = ...
device.build_output_stream(...

the iPhone seemingly randomly chooses between the built in earpiece and speaker. It isn't related to me holding it to my ear.

Is there a way to choose? I am only getting one audio output device ("Default Device") on the host...

@tGrothmannFluffy
Copy link
Author

tGrothmannFluffy commented Mar 7, 2024

I might have found the solution by doing something like this in flutter:

Future<AudioSession> startAudioSession(bool microphone) async {
  var session = await AudioSession.instance;
  if (microphone) {
    await session.configure(const AudioSessionConfiguration.music().copyWith(
        avAudioSessionCategory: AVAudioSessionCategory.playAndRecord,
        avAudioSessionMode: AVAudioSessionMode.measurement));
  } else {
    await session.configure(const AudioSessionConfiguration.music().copyWith(
      avAudioSessionCategory: AVAudioSessionCategory.playback,
      avAudioSessionCategoryOptions: AVAudioSessionCategoryOptions.defaultToSpeaker &
          AVAudioSessionCategoryOptions.allowBluetooth &
          AVAudioSessionCategoryOptions.allowBluetoothA2dp &
          AVAudioSessionCategoryOptions.allowAirPlay &
          AVAudioSessionCategoryOptions.mixWithOthers,
      avAudioSessionMode: AVAudioSessionMode.measurement,
    ));
  }
  await session.setActive(true);
  return session;
}

@kevinkouketsu
Copy link

Can you provide some details? I am facing some issues with iOS. AudioSession is something from flutter?

@tGrothmannFluffy
Copy link
Author

tGrothmannFluffy commented Mar 11, 2024

Audio Session is a flutter pacakge, it can manage your iOS AVAudioSession. It seems like cpal fails to do so (I don't know if by design or not?), and you have to configure and start the audio session yourself.

Maybe coreaudio-sys / coreaudio-rs could help.

I think this is only an issue since iOS 17

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants