Chapter 15. Audio
iOS provides various technologies that allow your app to produce, record, and process sound. The topic is a large one, so I’ll concentrate on the basics. Suggestions for further exploration appear at the end of this chapter.
None of the classes discussed in this chapter provides any interface for allowing the user to stop and start playback of sound — also known as transport interface. If you want transport interface, here are some options:
-
You can create your own interface.
-
You can associate the built-in “remote control” buttons with your app, as I’ll explain in this chapter.
-
A web view (Chapter 12) supports the HTML5
<audio>
tag; this can be a simple, lightweight way to play audio and to allow the user to control playback (including use of AirPlay). -
You could treat the sound as a movie and use the interface-providing classes that I’ll discuss in Chapter 16; this can also be a good way to play a sound file located remotely over the internet.
System Sounds
The simplest form of sound is system sound, which is the iOS equivalent of the basic computer “beep.” This is implemented through System Sound Services, part of the Audio Toolbox framework; you’ll need to import AudioToolbox
. The API for playing a system sound comes in two forms — the old form, and a new form that was introduced in iOS 9.
The old form involves calling one of two C functions, which behave very similarly to one another:
AudioServicesPlayAlertSound
-
On an iPhone, may also vibrate ...
Get Programming iOS 14 now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.