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 and other customizations #53

Open
wants to merge 9 commits into
base: main
Choose a base branch
from

Conversation

ChristopherGabba
Copy link

@hirbod This is my first time contributing to open-source... like ever. And I should also add that this is my first time coding in Obj-C.... So please feel free to crush my soul on how I did all of this lmfao...

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. For example:

Screenshot 2025-03-11 at 3 26 58 PM

Additionally, you'll note here that the setCategory function had multiple different parameters
Screenshot 2025-03-11 at 8 35 41 AM

I took my best shot at implementing this level of customization.

An example of this new function would be:

await VolumeManager.configureAVAudioSession({
   category: AVAudioSessionCategory.PlayAndRecord,
   mode: AVAudioSessionMode.Record,
   policy: AVAudioSessionRoutSharingPolicy.LongFormAudio, // Options: https://developer.apple.com/documentation/avfaudio/avaudiosession/routesharingpolicy-swift.enum
   options: [AVAudioSessionCategoryOptions.MixWithOthers, AVAudioSessionCategoryOptions.AllowAirPlay], // Options: https://developer.apple.com/documentation/avfaudio/avaudiosession/categoryoptions-swift.struct 
   prefersNoInterruptionsFromSystemAlerts: true,
   prefersInterruptionOnRouteDisconnect: true,
   allowHapticsAndSystemSoundsDuringRecording: 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. (Note that you actually did not have it in your native code anyway...).

One last comment... 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)
      }

So I added this plus one other option I ran across in the docs.

So... the kicker... I compiled the app and did a little bit of testing, I worked through a few compile errors I missed (small things) and everything seems to be not crashing and the function appears to be calling correctly on iOS.

That being said.... You may want to review my code and test it somehow... Sorry for not knowing a lot, I just really wanted to help out with what I was trying to accomplish...

@ChristopherGabba
Copy link
Author

So of the 4 commits:

  • The first commit 1ebd892 was my first attempt at the new API I wanted, but using the same Objective C functions you already had written
  • The second commit 8a73b49 was my attempt to create a new objective-C function that allowed further customization
  • The third commit was me updating the docs with my new custom function
  • The fourth commit I forgot to remove the old code from the example app

For the record, I'm also quite stupid at git as well... sorry for the mess...

@ChristopherGabba
Copy link
Author

ChristopherGabba commented Mar 12, 2025

@hirbod I got a little addicted to the cause and I spent all morning adding another method called getAVAudioSessionStatus(). That did what I was requesting in my feature request #51. I also rewrote the readme with a little better formatting, examples, and descriptions in the API section.

I also improved some of the hover-over notes for the original new function I created.

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