Tech spotlight: Audio Session Management in Sonocent Recorder for iOS
Dan Fry is the lead developer for our UK-based development team. Dan has a degree in Computing and has been instrumental to the development of our desktop software and app since the release of Sonocent Audio Notetaker 2.0 in 2008. Here he explains a key aspect of the thinking that went into the development of our newly-updated Sonocent Recorder app: namely, how the iOS version ensures that users’ recordings aren’t lost or negatively affected when multiple applications are running or recording is unexpectedly interrupted.
Without further ado, here’s Dan.
One way in which smartphone apps differ from traditional desktop software is that they need to react to a wider range of events that are outside their control.
An app must be able to handle interruptions such as incoming phone calls, and must be able to respond appropriately to conditions such as a shortage of memory or battery power.
This is especially true of apps that deal with audio, which is used in many different ways on modern phones, including:
• Music, streaming radio or podcasts – this type of audio often plays in the background while we carry out another task. We usually only want one of these audio streams to play at once.
• Alert sounds, such as text message and email notifications – we need to be able to hear these over any background audio, but without interrupting it if they’re not urgent.
• Voice recording – during which we need to silence all but the most critical sounds so that they don’t affect the recording.
• Phone calls – all recording and playback must stop to ensure the ringtone can be heard if it’s enabled, and to avoid interfering with the call once it is underway.
As all recent phone operating systems support multi-tasking, apps also have to co-operate to share access to the audio system, or request or relinquish exclusive access when it’s needed.
iOS deals with these issues by allowing apps to define audio sessions, which inform the operating system when they need access to the audio input or output, and what type of audio is being played.
This allows the system to determine whether to interrupt any other audio playing, or to mix the audio with anything playing in the background. It also allows apps to be notified by the OS when its session is about to be interrupted by another app.
In Sonocent Recorder, we have these requirements when playing back:
• The audio played back should stop any other background sound in other apps, such as music players, which might otherwise make the speech difficult to hear.
• We need to be able to continue playing sound when the app is in the background. It’s useful to be able to listen to recordings in Sonocent Recorder while using another app (that doesn’t play sounds!) or with the screen off if we don’t need to see the accompanying notes.
• We need important system sounds such as text message and email notifications to still be audible, especially if listening without looking at the phone screen. As these sounds are usually short, they can be mixed in with the audio from our app without drowning out the speech being played back.
iOS provides a number of standard audio session modes that cover different requirements, and fortunately the standard playback mode provides exactly the behaviour we need. All we have to do is start a playback audio session before we start playing back, and close it down afterwards. iOS takes care of shutting down any audio sessions belonging to other apps, and allows our app to continue running even when the phone screen is off or the user has switched to another app.
Our requirements when recording are very straightforward – we simply need all other sounds to be silenced, to ensure they are not picked up by the microphone. Again, this behaviour is provided by one of the standard audio session modes.
Some conditions always interrupt audio playback and recording, whatever audio session mode is used. The most obvious example on a phone is receiving a phone call – the ringtone has to be audible if it is switched on, and the microphone and speaker must be available to the phone app while the call is in progress.
Another reason the session might be interrupted is if the user starts recording or playing back in another app, and that app needs exclusive access to the audio input or output.
In both cases, iOS notifies Sonocent Recorder via its audio session that the session is being interrupted and we can stop playback or recording.
A phone call is a special case however – it’s only a temporary interruption, and there’s a good chance the user will want recording or playback to resume afterwards, especially if they’ve declined the call. When iOS notifies apps that the interruption has ended, it indicates whether or not the previous activity should be resumed – in Sonocent Recorder, we use this to determine whether to start playing or recording again.
There are some situations where the app has to stop recording without warning – for example, when the phone is low on memory.
iOS only keeps a certain number of apps running at any one time. If you switch quickly between a number of memory-hungry apps, it will sometimes automatically quit apps that are in the background to free up space. If this happens to Sonocent Recorder, there’s no way that we can continue recording, and because the app is in the background, it might not be obvious that it’s stopped. In this case, we show a pop-up notification just before the app quits – it looks like a text message notification and is displayed even if the phone is locked. Another situation where this is useful is when switching to another app that interrupts the recording session, since it might not be obvious that only one app can record at a time.
An added bonus of using audio sessions is that it allows Sonocent Recorder to display ‘now playing’ information in the lock screen – we can supply information about the recording including the title, speaker, the current playback position and even a slide image:
Useful if you’re listening on headphones while doing something else and just need to glance occasionally at the current slide!
Try it for yourself
Our Sonocent Recorder app is free and available for iOS and Android. Discover more and download now.
Sonocent Audio Notetaker free trial
Select the Windows or Mac icon, enter your email address and click ‘Go’