The iPhone SDK’s AVFoundation framework includes AVAudioPlayer, an easy, feature rich, Objective-C based way of playing audio files.
This tutorial demonstrates how to use AVAudioPlayer. When you’ve finished the tutorial you’ll have created a simple app that plays an MP3 audio file in a loop when the app starts.
Source/Github
The code for this tutorial is available on GitHub. You can either clone the repository or download this zip.
Creating The Project
Launch Xcode and create a new View-Based iPhone application called AudioPlayer:
- Create a new project using File > New Project… from Xcode’s menu
- Select View-based Application from the iPhone OS > Application section, click Choose…
- Name the project as AudioPlayer and click Save
Adding The AVFoundation Framework
In order to use the SDK’s AVAudioPlayer class, we’ll need to add the AVFoundation framework to the project:
- Expand the Targets branch in the Groups & Files panel of the project
- Control-click or right-click the AudioPlayer item
- Choose Add > Existing Frameworks…
- Click the + button at the bottom left beneath Linked Libraries
- Choose AVFoundation.framework and click Add
- AVFoundation.framework will now be listed under Linked Libraries. Close the window
Next, we’ll import the AVFoundation headers in our view controller’s interface file and set up an AVAudioPlayer instance variable:
- Expand the AudioPlayer project branch in the Groups & Files panel of the project
- Expand the Classes folder
- Edit AudioPlayerViewController.h by selecting it
- Update the file. Changes are bold:
#import <UIKit/UIKit.h> #import <AVFoundation/AVFoundation.h> @interface AudioPlayerViewController : UIViewController { AVAudioPlayer *audioPlayer; } @end
Adding An Audio File
We’ll need an audio file for playback. We’ll, unimaginatively, call the fille audiofile.mp3. Add it to the project:
- Control-click or right click on the Resources folder in the Groups & Files panel of the project
- Select Add > Existing Files… from the context menu
- Locate and select the file for import and click Add
- Check the Copy items into destination group’s folder (if needed) box and click Add
Starting Audio Playback
We’ll start the audio playback in ViewDidLoad:
- Uncomment the boilerplate ViewDidLoad method
- Update it as follows. Changes are bold:
- (void)viewDidLoad { [super viewDidLoad]; NSURL *url = [NSURL fileURLWithPath:[NSString stringWithFormat:@"%@/audiofile.mp3", [[NSBundle mainBundle] resourcePath]]]; NSError *error; audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:&error]; audioPlayer.numberOfLoops = -1; if (audioPlayer == nil) NSLog([error description]); else [audioPlayer play]; }
AVAudioPlayer is initialized with a URL, so we create one whose path points to the audio file in our on-the-phone resources directory. Setting the audio player’s numberOfLoops
property to a negative number causes it to loop indefinitely. After configuring the player, we start the playback by sending the play
message to the object.
Remember to release the audioPlayer in dealloc
. Changes are bold:
- (void)dealloc { [audioPlayer release]; [super dealloc]; }
More Capabilities
You can adjust the volume of the player, check/set the time played so far and pause or stop playback:
audioPlayer.volume = 0.5; // 0.0 - no volume; 1.0 full volume NSLog(@"%f seconds played so far", audioPlayer.currentTime); audioPlayer.currentTime = 10; // jump to the 10 second mark [audioPlayer pause]; [audioPlayer stop]; // Does not reset currentTime; sending play resumes
Finally, you can implement the AVAudioPlayerDelegate
protocol to, among other things, be notified when audio finishes playing — perhaps to move onto the next song in a playlist.