IPhoneEasy to useAVAudioPlayer audio playbackIs the content to be introduced in this article,IPhoneThe AVFoundation framework in the SDK includesAVAudioPlayerIt is an easy-to-use and powerful player for playing audio files based on Object-c. This tutorial shows how to useAVAudioPlayer. This tutorial will create a simple program that can play an mp3 audio file cyclically.
Source code/Guithub
The source code of the tutorial is on GitHub. You can clone or directly download the zip file from the repository.
Create a project
- Launch Xcode and create a new View-Based iPhone application called AudioPlayer:
Start Xcode and create a "View-Based iPhone application" project named AudioPlayer:
1. Select "File> New Project…" from the Xcode menu ..."
2. Select "View-based Application" from the "iPhone OS> Application" section, and then press "Choose ..."
3. Name the project "AudioPlayer" and press "Save"
Add AVFoundation framework
To use the SDK AVAudioPlayer class, we need to add the AVFoundation framework to the project:
1. on the "Groups & Files" Panel of the project, expand "Targets"
2. Click Control + or right-click AudioPlayer.
3. Select "Add> Existing Frameworks ..."
4. Click the + button in the lower left of Linked Libraries.
5. Select "AVFoundation Framework" and press Add
6. "AVFoundation framewoks" will appear under "Linked Libraries" and close the window.
Next, we will introduce the header file category of AVFoundation
1. Expand the AudioPlayer project under the "Group & Files" Panel of the project.
2. Open the Classes folder
3. Select AudioPlayerViewController. h for editing.
4. Change the file. Change the following bold text section:
- #import <UIKit/UIKit.h>
- #import <AVFoundation/AVFoundation.h>
-
- @interface AudioPlayerViewController : UIViewController
- {
- AVAudioPlayer *audioPlayer;
- }
- @end
Add audio files
We need an audio file for playback. The file is audiofie.mp3. We add it to the project:
Press Control and then left-click or right-click the "Resources" folder in the "Group & Files" Panel of the project.
Select "Add> Existing Files…" from the context menu ..."
Find and select the audio file to be imported, and press "Add"
If necessary, select the Copy items into destination group's folder box and press "Add"
Start playing audio
We start audio playback in ViewDidLoad:
1. Remove the annotation of the ViewDidLoad Method
2. The changes are as follows, see the bold Section:
- - (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 through a URL. Therefore, we must first create a URL pointing to the audio file in the resource folder. Set the numberOfLoops attribute of the audio player to a negative number to enable infinite loop of playback. After the audio player is configured, we send a playback message to the Player Object to start playing.
Remember to release audioPlayer in the dealloc method. For changes, see the bold Section:
- - (void)dealloc
-
- [audioPlayer release];
- [super dealloc];
- }
More functions
You can adjust the player volume, view/set the playback Time, And pause or stop the 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 implementAVAudioPlayerThe Delegate Protocol, for exampleAudio PlaybackYou will be notified at the end, so that you may move to the next song in the playlist.
Summary:IPhoneEasy to useAVAudioPlayer audio playbackI hope this article will help you.