Chapter 25. Playing Audio

In This Chapter

Working with the AV Foundation Framework
Using the AVAudioSession class to set the audio category
Understanding how audio categories affect audio playback and the mute switch
Using AVAudioPlayer to play audio
Handling interruption of audio playback
Playing audio with OpenAL

The iPhone isn't just a phone. It's a media player. Thanks to this capability, it sports some of the best audio hardware in the business, and fortunately that hardware is ripe to be manipulated using code.

In the bad old days of iPhone OS 2.x, playing audio was a black art, limited to code that used the foundational framework, Core Audio. In today's world of iPhone OS 3, however, playing and recording audio has become infinitely easier, and you now have access to a set of shiny, new Objective-C classes that make playing audio a snap.

Using the AV Foundation Framework

The AV Foundation Framework consists of three primary classes — AVAudioPlayer, AVAudioRecorder, and AVAudioSession — which correspond to playing, recording, and configuration, respectively. Each of them also has a delegate protocol that's used to respond to events related to each of the classes.

Note

To use the AV Foundation Framework in your application, be sure to add it as a framework to your target's linked frameworks. The framework name is AVFoundation. Remember also to import the correct header files in code you want to use it in. The header file is AVFoundation/AVFoundation.h.

Setting your audio configuration with ...

Get Cocoa Touch™ for iPhone® OS 3 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.