IPhone apps can easily use AVAudioPlayer for Audio Playback

Source: Internet
Author: User

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

 
 
  1. 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:

 
 
  1.  #import <UIKit/UIKit.h> 
  2. #import <AVFoundation/AVFoundation.h> 
  3.  
  4. @interface AudioPlayerViewController :  UIViewController   
  5. {  
  6.     AVAudioPlayer  *audioPlayer;  
  7. }  
  8. @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:

 
 
  1. - (void)viewDidLoad  
  2.  
  3.    [super  viewDidLoad];  
  4.  
  5.    NSURL  *url = [NSURL fileURLWithPath:[NSString    
  6.        stringWithFormat:@"%@/audiofile.mp3",  [[NSBundle mainBundle]  resourcePath]]];  
  7.    NSError  *error;  
  8.    audioPlayer  = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:&error];  
  9.    audioPlayer.numberOfLoops  = -1;  
  10.    if  (audioPlayer == nil)  
  11.        NSLog([error  description]);  
  12.    else  
  13.         [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:

 
 
  1. - (void)dealloc   
  2.  
  3.    [audioPlayer  release];  
  4.    [super  dealloc];  
  5.  }  

More functions

You can adjust the player volume, view/set the playback Time, And pause or stop the playback:

 
 
  1. audioPlayer.volume = 0.5; // 0.0 - no  volume; 1.0 full volume   
  2. NSLog(@"%f seconds played so  far", audioPlayer.currentTime);  
  3. audioPlayer.currentTime = 10; // jump to  the 10 second mark  
  4. [audioPlayer pause];  
  5. [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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.