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

Toogling audio output between earpiece and speaker #9

Closed
mmartinalo opened this issue Oct 26, 2020 · 5 comments
Closed

Toogling audio output between earpiece and speaker #9

mmartinalo opened this issue Oct 26, 2020 · 5 comments

Comments

@mmartinalo
Copy link

Context

I am developing a mobile application that plays audios with the packages just_audio and audio_service. I am trying to add a feature using the proximity sensors of the device so that every time the user brings the mobile closer to the ear, the audio is played from the earpiece

Dependencies

  • audio_service: 0.15.2
  • just_audio: 0.5.5
  • audio_session: 0.0.9

My current state

When the service starts I configure the AudioSession as following

    final session = await AudioSession.instance;
    await session.configure(AudioSessionConfiguration.music());

And I have a custom action when the proximity sensor is actived

  final session = await AudioSession.instance;
  final AudioSessionConfiguration currentConfiguration = session.configuration;
  await session.configure(
    session.configuration.copyWith(
      androidAudioAttributes: AndroidAudioAttributes(
        usage: AndroidAudioUsage.voiceCommunication,
        flags: currentConfiguration.androidAudioAttributes.flags,
        contentType: currentConfiguration.androidAudioAttributes.contentType,
      ),
    ),
  );

It is working properly on Android but I am not sure if it is the recommended way and I have no idea how to do it in ios with the available options avAudioSessionCategory, avAudioSessionCategoryOptions or avAudioSessionMode

Thank you in advance.

@ryanheise
Copy link
Owner

Hi @mmartinalo , the eventual goal is to implement all of the AVAudioSession API but I started with just the features that I personally needed. If you take a look at the source documentation for Apple's AVAudioSession , perhaps you can identify which features you need, then you can let me know and I can add those features next.

@mmartinalo
Copy link
Author

Hello @ryanheise ,

I was reading in-depth the documentation and I realized that AVAudioSession playAndRecord category solve my problems:

  • plays audio through earpiece
  • locks the screen

Both for android and ios the code would be something like

    final session = await AudioSession.instance;
    final AndroidAudioAttributes currentAndroidAttributes = session.configuration.androidAudioAttributes;
    await session.configure(
      session.configuration.copyWith(
        androidAudioAttributes: AndroidAudioAttributes(
          usage: AndroidAudioUsage.voiceCommunication,
          flags: currentAndroidAttributes.flags,
          contentType: currentAndroidAttributes.contentType,
        ),
        avAudioSessionCategory: AVAudioSessionCategory.playAndRecord,
      ),
    );

I think it would be interesting to encapsulate a similar logic in a public function in the AudioPlayer.

Thanks for the support!

@ryanheise
Copy link
Owner

Glad you found a solution. I'd rather have a clear separation of concerns between these two plugins so that audio_session is responsible for switching between devices or detecting when a switch occurs, while just_audio doesn't need to know.

Interestingly, the name "just" audio actually stemmed from a goal to build an audio plugin that didn't take on any other responsibilities besides the audio signal itself, while different plugins could handle settings for the hardware devices, etc.

@mrtnetwork
Copy link

Hello @ryanheise ,

I was reading in-depth the documentation and I realized that AVAudioSession playAndRecord category solve my problems:

  • plays audio through earpiece
  • locks the screen

Both for android and ios the code would be something like

    final session = await AudioSession.instance;
    final AndroidAudioAttributes currentAndroidAttributes = session.configuration.androidAudioAttributes;
    await session.configure(
      session.configuration.copyWith(
        androidAudioAttributes: AndroidAudioAttributes(
          usage: AndroidAudioUsage.voiceCommunication,
          flags: currentAndroidAttributes.flags,
          contentType: currentAndroidAttributes.contentType,
        ),
        avAudioSessionCategory: AVAudioSessionCategory.playAndRecord,
      ),
    );

I think it would be interesting to encapsulate a similar logic in a public function in the AudioPlayer.

Thanks for the support!

thanks , you saved my time :X

@ryanheise
Copy link
Owner

Would you mind sharing your solution on this issue? #37

The goal of that issue is to provide a convenience method in the AudioSession class to handle this common use case in a platform-independent way (while still keeping the native APIs available for more advanced use cases).

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

3 participants