Best practice when dealing with audio sessions for each type of app in iOS

Tram Ho

The game will not have the same audio requirements as a video call app. Each type of application has different requirements for proper audio processing, especially when working with audio session, interuption, route change. Let’s take a look at how each type of app should handle audio!

Games

Most games require users to interact with what is in the game. We use the session category AVAudioSessionCategoryAmbient or AVAudioSessionCategorySoloAmbient when designing audio logic for the game. When users put another app on the foreground or lock screen, they would expect the app to stop. And often users will want audio from other applications that can play during games (for example, playing music while playing games, for example). Here are the best practices:

  • Activate audio session in the applicationDidBecomeActive: function of the app delegate.
  • Allow audio from other applications to be played along with the sound effect.
  • Only play soundtrack when other audio does not play.
  • Always reactivate and resume playback of sound effects after the interruption ends.
  • Check the secondaryAudioShouldBeSilencedHint property of the audio session to determine whether or not to resume playback of the game’s soundtrack.
  • Ignore handling of route changes unless the app has a special problem that needs attention.
  • Set audio category before displaying a video splash when the app runs.

The application has Playback and Recording controlled by the user

Recording and playback applications like Pandora and Netflix have similar behaviors. This type of application uses the AVAudioSessionCategoryRecord , AVAudioSessionCategoryPlayAndRecord , or AVAudioSessionCategoryPlayback and often interrupt other audio systems when its audio session is activated. Interfaces usually include play / pause or record / pause buttons.

Here are the best practices for this type of application:

  • When the application enters the foreground state, wait for the user to press Play or Record to activate the audio session.
  • While the application is in foreground, keep the audio session active until interrupted.
  • If the application does not play or record audio when it returns to the background, deactivate the audio session. This helps prevent audio sessions from being interrupted by nonmixable applications or being suspended by the system.
  • Updated UI to determine whether playback or recording stops when interrupt is encountered but does not deactivate audio session.
  • Observe notification AVAudioSessionInterruptionNotification to get notifications about audio session interruption. When the interruption is over, don’t start playing or recording again unless the app is doing this before the interruption is encountered.
  • Stop playback or recording if a route change occurs due to user unplugging the headset, but keep the audio session active.
  • If the audio session is inactive when switching from suspend state to foreground. Please reactive the audio session again when the user clicks Play or Record.
  • Ensure that UIBackgroundModes audio is set.
  • Register for the remote control event (see the documentation for MPRemoteCommandCenter ) and provide the appropriate Now Playing information for your media (see the documentation for MPNowPlayingInfoCenter).
  • Use the MPVolumeView object to represent the system volume slider and route picker.
  • Use background tasks instead of streaming silence to keep the application from suspending.
  • Require users to record using the requestRecordPermission: function instead of letting the OS ask the user.
  • For record applications, it is possible to use the AVAudioSessionCategoryPlayAndRecord category instead of AVAudioSessionCategoryRecord because the category only the record will not silence most of the system’s output ports and will be quite limited. However in general this depends on the business of the app.

VoIP and Chat application

The VoIP and Chat applications require both input and output to be available. This app type uses AVAudioSessionCategoryPlayAndRecord category and will not mix sound with other AVAudioSessionCategoryPlayAndRecord .

Here are the best practices:

  • Only activate the audio session when the user answers or initiates a call.
  • Remember to update the UI to show that the audio of the call has been interrupted if an interruption notification is received.
  • Do not re-activate the audio session after interruption until user actively answers or initiates a call.
  • Deactivate audio session after the call ends, use the AVAudioSessionSetActiveOptionNotifyOthersOnDeactivation option.
  • Ignore all handling route changes unless the app has a specific point to poke. For example, route change can cause changes to the session sample rate, buffer duration, or latency. If these values ​​are relevant to the app, you need to check after a route change has occurred to get the latest status.
  • For VoIP apps, use Apple’s Voice Processing I / O audio unit.
  • Ensure that UIBackgroundModes audio is set.
  • Use MPVolumeView object for volume slide and route picker.
  • Require users to record using the requestRecordPermission: function instead of letting the OS ask the user.

Starting with iOS 10, to build a VoIP app with the same features and capabilities as an existing app like Phone or FaceTime, you need to use the CallKit framework.

Measurement application

Measurement applications require a minimum amount of signal processing provided by the system to the input and output routes. Set up the AVAudioSessionCategoryPlayAndRecord category and measurement mode to minimize signal processing. This type of app will not mix with other apps.

Here are the best practices:

  • Always reactivate and resume playback after an interruption ends.
  • Ignore all route changes unless the app needs to.
  • Set audio category before displaying splash video when app is running.
  • Require users to record using the requestRecordPermission: function instead of letting the OS ask the user.

Similar browser applications that sometimes play audio

Social media or browser-like apps often play short videos. This type of app uses AVAudioSessionCategoryPlayback category and it doesn’t mix audio with other AVAudioSessionCategoryPlayback .

Here are the best practices:

  • Always wait for the user to start playback.
  • Deactive audio session after a video ends, use the AVAudioSessionSetActiveOptionNotifyOthersOnDeactivation option.
  • Stopping audio due to a route change happens by removing the headset, but keeping the audio session active.
  • Register the remote control event while the video is playing, and unregister when the video ends.
  • Update UI when app receives begin interruption event.
  • Wait for the user to start playback after receiving an end interruption event.

Navigation and workout applications

Navigation and workout applications use the AVAudioSessionCategoryPlayback or AVAudioSessionCategoryPlayAndRecord categories. Audio from these applications usually includes short voice prompts. When playing, the prompts will interrupt the spoken audio, like a podcast or an audio book, but mix it with other audio like playback from the Music app.

Here are the best practices:

  • Activate audio session with both AVAudioSessionCategoryOptionInterruptSpokenAudioAndMixWithOthers and AVAudioSessionCategoryOptionDuckOthers options.
  • Do not activate the audio session until a prompt is needed.
  • Always deactivate audio session after a prompt is played.
  • Do not resume playback of an interrupt prompt.

Remix application

Cooperative music apps are designed to play alongside other apps. The category this category uses is AVAudioSessionCategoryPlayback or AVAudioSessionCategoryPlayAndRecord , and of course its options will mix with other apps.

Here are the best practices:

  • Activate audio session using the AVAudioSessionCategoryOptionMixWithOthers option.
  • If the app UI does not provide a start / stop button, follow the game apps best practices.
  • If the app UI provides a start / stop button, only activate the audio session when the user presses the play button.
  • Do not register remote control events.
  • Ensure that UIBackgroundModes audio is set.
  • If the application has a recording, ask the user for permission to record using the requestRecordPermission: function instead of letting the OS ask the user.

Conclusion

Above are the best practices when we work with audio sessions for each type of app in iOS, hopefully it will be an interesting source of knowledge for you who are working with applications that need audio sessions.


Reference from Audio Session Programming Guide

Share the news now

Source : Viblo