Skip to content

Commit

Permalink
Set core audio session category to none
Browse files Browse the repository at this point in the history
By setting the core audio session to none, miniaudio will not
- set a (new) category on the shared audio session
- set any (new) options when setting a new category

This means that the shared AVAudioSession instance will be respected when audio is played; the developer will have to set up the shared AVAudioSession instance explicitly for their own set up.

This does not touch whether the session is made (in)active.

This is _probably_ the right way to go about this, rather than overwriting / overriding whatever settings developers had previously set. The side effect of this may be that we will have developers reaching out to us that audio is not playing when the silent switch is toggled on, or audio is not mixing; we should be proactive and update the docs stating that this now respects `AVAudioSession.sharedInstance` settings, and point to Apple's docs for how to set that up.

Relevant code from miniaudio:

```c
if (pConfig->coreaudio.sessionCategory == ma_ios_session_category_default) {
            /*
            I'm going to use trial and error to determine our default session category. First we'll try PlayAndRecord. If that fails
            we'll try Playback and if that fails we'll try record. If all of these fail we'll just not set the category.
            */
        #if !defined(MA_APPLE_TV) && !defined(MA_APPLE_WATCH)
            options |= AVAudioSessionCategoryOptionDefaultToSpeaker;
        #endif

            if ([pAudioSession setCategory: AVAudioSessionCategoryPlayAndRecord withOptions:options error:nil]) {
                /* Using PlayAndRecord */
            } else if ([pAudioSession setCategory: AVAudioSessionCategoryPlayback withOptions:options error:nil]) {
                /* Using Playback */
            } else if ([pAudioSession setCategory: AVAudioSessionCategoryRecord withOptions:options error:nil]) {
                /* Using Record */
            } else {
                /* Leave as default? */
            }
        } else {
            if (pConfig->coreaudio.sessionCategory != ma_ios_session_category_none) {
            #if defined(__IPHONE_12_0)
                if (![pAudioSession setCategory: ma_to_AVAudioSessionCategory(pConfig->coreaudio.sessionCategory) withOptions:options error:nil]) {
                    return MA_INVALID_OPERATION;    /* Failed to set session category. */
                }
            #else
                /* Ignore the session category on version 11 and older, but post a warning. */
                ma_log_postf(ma_context_get_log(pContext), MA_LOG_LEVEL_WARNING, "Session category only supported in iOS 12 and newer.");
            #endif
            }
        }
```

Diffs=
2be9ddc7e Set core audio session category to none (#7772)

Co-authored-by: David Skuza <[email protected]>
  • Loading branch information
dskuza and dskuza committed Aug 28, 2024
1 parent 1b24bf9 commit a5279e6
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 2 deletions.
2 changes: 1 addition & 1 deletion .rive_head
Original file line number Diff line number Diff line change
@@ -1 +1 @@
25d423274ff8e2894c874093b2d5ca86fc2895c2
2be9ddc7eb8bb795e9b1cc33ce4c33b3478859fe
10 changes: 9 additions & 1 deletion src/audio/audio_engine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,15 @@ rcp<AudioEngine> AudioEngine::Make(uint32_t numChannels, uint32_t sampleRate)
// need. This should automatically set available backends in priority order based on the target
// it's built for, which in the case of Apple is Core Audio first.
ma_context_config contextConfig = ma_context_config_init();
contextConfig.coreaudio.sessionCategoryOptions = ma_ios_session_category_option_mix_with_others;

// By setting the core audio session to none, miniaudio will not
// - set a (new) category on the shared audio session
// - set any (new) options when setting a new category
// This means that the shared AVAudioSession instance will be respected
// when audio is played; the developer will have to set up the shared
// AVAudioSession instance explicitly for their own set up.
// This does not touch whether the session is made (in)active.
contextConfig.coreaudio.sessionCategory = ma_ios_session_category_none;

// We only need to initialize space for the context if we're targeting Apple platforms
ma_context* context = (ma_context*)malloc(sizeof(ma_context));
Expand Down

0 comments on commit a5279e6

Please sign in to comment.