An instance of the
AVAudioPlayer class, called an audio player, provides playback of audio data from a file or memory.
- iOS 2.2+
- macOS 10.7+
- tvOS 2.2+
- watchOS 3.0+
Apple recommends that you use this class for audio playback unless you are playing audio captured from a network stream or require very low I/O latency. For an overview of audio technologies, see Audio & Video Starting Point and Using Audio in Multimedia Programming Guide.
Using an audio player you can:
Play sounds of any duration
Play sounds from files or memory buffers
Play multiple sounds simultaneously, one sound per audio player, with precise synchronization
Control relative playback level, stereo positioning, and playback rate for each sound you are playing
Seek to a particular point in a sound file, which supports such application features as fast forward and rewind
Obtain data you can use for playback-level metering
AVAudioPlayer class lets you play sound in any audio format available in iOS and macOS. You implement a delegate to handle interruptions (such as an incoming phone call on iOS) and to update the user interface when a sound has finished playing. The delegate methods are described in
To play, pause, or stop an audio player, call one of its playback control methods, described in Configuring and Controlling Playback.
This class uses the Objective-C declared properties feature for managing information about a sound—such as the playback point within the sound’s timeline, and for accessing playback options—such as volume and looping.
To configure an appropriate audio session for playback on iOS, refer to
AVAudioSessionDelegate. To learn how your choice of file formats impacts the simultaneous playback of multiple sounds, refer to iOS Hardware and Software Audio Codecs in Multimedia Programming Guide.