Skip to content

Audio Session

levching edited this page Apr 15, 2020 · 3 revisions

This feature appeared in response to many user's requests.

The main goal is to ignore iPhone “silent” switch. This is achievable with the ISN_AVAudioSession.

In order for your device to play sounds even if iPhone currently in a silent mode, you need to activate audio session and change category to ISN_AVAudioSessionCategory.Playback. The code snippet below should do the trick:

using SA.iOS.AVFoundation;
...
ISN_AVAudioSession.SetActive(true);
ISN_AVAudioSession.SetCategory(ISN_AVAudioSessionCategory.Playback);

If you are using other native plugin's that allow you to play some audio or a youtube video, the audio session category can change. So you probably want to restore it to ISN_AVAudioSessionCategory.Playback once you have returned back to Unity.

Another Option might be to subscribe to an audio session OnAudioSessionRouteChange event and in case category was changed, just change it back:

using SA.iOS.AVFoundation;
...
ISN_AVAudioSession.OnAudioSessionRouteChange.AddListener((reason) => {
    if(reason == ISN_AVAudioSessionRouteChangeReason.CategoryChange) {
        ISN_AVAudioSession.SetActive(true);
        ISN_AVAudioSession.SetCategory(ISN_AVAudioSessionCategory.Playback);
    }
});

To be hornets it's absolutely not advisable, but who am I to say you can't do it. You may have a good reason to be playing sounds or music when a user device is in a "silent" mode.

About

Foundation

AV Foundation

App Tracking Transparency

Game Kit

Store Kit

UI Kit

Social

Replay Kit

Contacts

AVKit

Photos

App Delegate

User Notifications

MediaPlayer

Core Location

AdSupport

EventKit

CloudKit

Authentication Services

XCode

Knowledge Base

Clone this wiki locally