Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When to use AVAudioSession.setActive?

My understanding is that you can only have 1 AVAudioSession (since it is a singleton and therefore you receive always the same instance) in your app.

So, what is the purpose of the setActive() function? Since I can only have one session, I don't see a proper use case for that function.

AVAudioSession documentation: https://developer.apple.com/documentation/avfoundation/avaudiosession

like image 684
rsc Avatar asked Jan 01 '18 22:01

rsc


1 Answers

From Apple doc:

With setActive your app is asking the system to use audio. For example you can make your request when the Music app is already playing, and the system depending on the priority state could interrupt it to play your audio.

Activating and Deactivating Your Audio Session

Although the AVFoundation playback and recording classes automatically activate your audio session, manually activating it gives you an opportunity to test whether activation succeeded. However, if your app has a play/pause UI element, write your code so that the user must press Play before the session is activated. Likewise, when changing your audio session’s active/inactive state, check to ensure that the call is successful. Write your code to gracefully handle the system’s refusal to activate your session.

The system deactivates your audio session for a Clock or Calendar alarm or an incoming phone call. When the user dismisses the alarm, or chooses to ignore a phone call, the system allows your session to become active again.

like image 182
Francesco Deliro Avatar answered Sep 20 '22 21:09

Francesco Deliro