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

Feat: Type-safe configureAVAudioSession function that ensures compatibility with AVAudioSessionModes #52

Conversation

ChristopherGabba
Copy link

@ChristopherGabba ChristopherGabba commented Mar 11, 2025

@hirbod This is my first time contributing to open-source... like ever. So please feel free to crush my soul on how I did all of this.

The gist of it is, after reading all the docs: https://developer.apple.com/documentation/avfaudio/avaudiosession, it became clear that each AVAudioSessionCategory could only be used with specific AVAudioSessionModes. I was pretty thorough in making sure that I have the compatible modes with each category.

In order to make this type-safe, I needed to combine them into one function. I also used this to change the types of AVAudioSessionMode and AVAudioSessionCategory to an enum type so that could properly get good developer experience tool tips when mousing over each category.

An example of this new function would be:

await VolumeManager.configureAVAudioSession({
   category: AVAudioSessionCategory.PlayAndRecord,
   mode: AVAudioSessionMode.Record,
   mixWithOthers: true
})

I also marked the setCategory function and the setMode function as deprecated using this new API.

Among other changes, it would appear that the previously had Alarm category is no longer listed, and the AudioProcessing category is listed as deprecated. https://developer.apple.com/documentation/avfaudio/avaudiosession/category-swift.struct

So I removed the Alarm option and flagged the AudioProcessing as deprecated.

Now, one last comment. You'll note here: https://developer.apple.com/documentation/avfaudio/avaudiosession/setcategory(_:mode:policy:options:)

That setting this AVAudioSessionCategory also presents more options like options and policy.
Screenshot 2025-03-11 at 8 35 41 AM

I think it would be fantastic to add these to the API for further configuration. Additionally I noticed while studying react-native-vision-camera Audio Session setup he has this code:

      if #available(iOS 14.5, *) {
        // prevents the audio session from being interrupted by a phone call
        try audioSession.setPrefersNoInterruptionsFromSystemAlerts(true)
      }

      if #available(iOS 13.0, *) {
        // allow system sounds (notifications, calls, music) to play while recording
        try audioSession.setAllowHapticsAndSystemSoundsDuringRecording(true)
      }

I am not sure how we handle this in the volume-manager package, but may be other things to include as options?

Maybe a final API would look like this??:

await VolumeManager.configureAVAudioSession({
   category: AVAudioSessionCategory.PlayAndRecord,
   mode: AVAudioSessionMode.Record,
   policy: AVAudioSessionRoutSharingPolicy.LongFormAudio, // Options: https://developer.apple.com/documentation/avfaudio/avaudiosession/routesharingpolicy-swift.enum
   categoryOptions: [AVAudioSessionCategoryOptions.MixWithOthers, AVAudioSessionCategoryOptions.AllowAirPlay], // Options: https://developer.apple.com/documentation/avfaudio/avaudiosession/categoryoptions-swift.struct 
   prefersNoInterruptionsFromSystemAlerts: true,
   allowHapticsAndSystemSoundsDuringRecording: true
})

Anyway, thanks again!!

@ChristopherGabba
Copy link
Author

@hirbod I also just noticed my logic is a little flawed. I used:

  if(!isAndroid) {
    await Promise.all([VolumeManagerNativeModule.setCategory(category, mixWithOthers), VolumeManagerNativeModule.setMode(mode)]);
  }

and I think it should be:

  if(!isAndroid) {
    await VolumeManagerNativeModule.setCategory(category, mixWithOthers) 
    await VolumeManagerNativeModule.setMode(mode)
  }

Maybe you don't want to fire these methods simultaneously, but sequentially instead...

@ChristopherGabba
Copy link
Author

CLOSING THIS PR, SUPERCEDED BY #53

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

Successfully merging this pull request may close these issues.

1 participant