Join John Nastos for an in-depth discussion in this video Advantages and disadvantages of AVAudioPlayer, part of Creating Audio Apps for iOS.
- View Offline
- Next, I'll look at AVFoundation and AVAudioPlayer specifically. AVFoundation is one of the most commonly used audio frameworks thanks to its flexibility and relative ease of use. It sits between System Sound Services, and audio cues, and audio units in terms of its complexity and features. First, let's take a look at its advantages. Unlike System Sound Services and Core Audio, Audio Foundation is an Objective-C API. Being able to use all of the conveniences offered by Objective-C such as ARC and familiar object types can be a huge advantage.
AVAudioPlayer can handle files of any duration unlike System Sound Services that has a 30 second limit. The source for Audio data and AVAudioPlayer can either be files or in-memory sources. This is very useful for you if you create sounds dynamically, or loading from a streaming source, etc. AVAudioPlayer can be set to loop a certain number of times or infinitely. If you have background music for your app, for example, you may want to have it keep looping as long as the user is within the app.
AVAudioPlayer also has some timing ability. Although the timing functions it provides aren't perfect and you can get more precision out of Core Audio, AVAudioPlayer has at least some limited functionality to play sounds at a certain time. AVAudioPlayer can also handle playing multiple sounds from multiple sources simultaneously. AVAudioPlayer can handle playing audio in the background assuming your audio session is configured correctly and your app is registered for background audio, which I'll cover later in the course.
You also have some limited access to information about the sound data, which can be used for metering and other applications. AVFoundation allows you to record sound input through its AVAudioRecorder object. Like AVAudioPlayer, its limited compared to Core Audio but it gives you some functionality and some tie-in to be able to play and record audio through the same framework. On to the disadvantages. As I mentioned before, although AVAudioPlayer has some timing ability, it's not precise.
It may be good enough to play a timer sound when it level ends on a game, for example, but it's not precise enough to make a metronome. AVAudioPlayer has a limited ability to alter the data stream. If you need full control of the stream, Core Audio is a much better choice.
- Playing sounds with System Sound Services
- Setting up audio sessions
- Playing sounds with AVFoundation and AVAudioPlayer/AVAudioRecorder
- Recording audio with an audio input queue
- Playing back audio with an audio output queue
- Setting up audio units
- Changing input and output levels
- Responding to events
- Working with third-party frameworks