Playing Sounds and Videos

iOS is capable of complex real-time sound synthesis. But a full introduction to all the features in Core Audio, Media Player, and the other media frameworks would need a much bigger book.

Fortunately, you don’t need to be an expert on synthesis to play a simple sound. You can use a class called AVAudioPlayer to load a sound and play it with just a few lines of code.

Playing a sound with AVAudioPlayer

The AVAudioPlayer class provides simple playback of sampled audio files. The class can do much, and it’s easy to work with. But you must add extra setup code to load a file you package with your app and optionally to handle errors.

caution_exclamation_2c.eps

AVAudioPlayer is part of the AVFoundation framework. Don’t forget to add this framework to your build and include its headers in your project before you work with it.

Get iOS App Development Portable Genius 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.