How to Play Music




How to Play an Audio File in iOS6


*** Step 1: ***

Add the AVFoundation.framework class to your project:
(since the AVAudioPlayer class that you will need is in this framework)

1) Click on the project in the Project Navigator Panel (on the left at the top)
2) Click on the Build Phases tab in the main panel
3) Click on the + in the Link Binary with Libraries section 
4) Select the AVFoundation.framework and click on the Add button


*** Step 2: ***

Inside your ViewController.h file:

#import <AVFoundation/AVFoundation.h> 

@interface ViewController : UIViewController

@property (strong, nonatomic) AVAudioPlayer *audioPlayer;


*** Step 3: ***

Inside your ViewController.m file:
(probably inside your viewDidLoad method)
(replace @"MyFilename" with the filename)


NSURL *url = [NSURL fileURLWithPath:
      [[NSBundle mainBundle] pathForResource:@"MyFilename" ofType:@"mp3"]]; 

NSError *error;

_audioPlayer = [[AVAudioPlayer alloc] 
       initWithContentsOfURL:url error:&error]; 


if (error) 
{ 
    NSLog(@"Error in AudioPlayer"); 
} 
else 
{
    [_audioPlayer prepareToPlay];
	[_audioPlayer play]; 
}








How to play an audio file in iOS5

#import <AVFoundation/AVFoundation.h>
// you need to add the AudioToolbox.framework to use this
// and you need to add the sound files to the project
#include <AudioToolbox/AudioToolbox.h>


// this is for playing mp3's etc.
// define these variables in your .h file
AVAudioPlayer *player;
NSString *pathForExplosionSound;


// use this code for setting up the player
pathForExplosionSound = [[NSBundle mainBundle] pathForResource:@"explosion" ofType:@"mp3"];

NSError *error;

if (![[NSFileManager defaultManager] fileExistsAtPath:self.pathForExplosionSound])
    return NO;

player = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:self.pathForExplosionSound] error:&error];

[player prepareToPlay];



// methods to call
[player play];

[player pause];

// use this to reset it (don't call stop)
if ([player isPlaying]==YES)
    [player setCurrentTime:0];

// stops the player and releases it from memory
[player stop];

// use this to reset it
[player setCurrentTime:0];


// Don't forget to release it
// at the very end
// since we did an alloc
[player release];





How to Play an Audio File in iOS6 using the delegate


*** Step 1: ***

Add the AVFoundation.framework class to your project:
(since the AVAudioPlayer class that you will need is in this framework)

1) Click on the project in the Project Navigator Panel (on the left at the top)
2) Click on the Build Phases tab in the main panel
3) Click on the + in the Link Binary with Libraries section 
4) Select the AVFoundation.framework and click on the Add button


*** Step 2: ***

Inside your ViewController.h file:

#import <AVFoundation/AVFoundation.h> 


@interface ViewController : UIViewController 

@property (strong, nonatomic) AVAudioPlayer *audioPlayer;


*** Step 3: ***

Inside your ViewController.m file:
(probably inside your viewDidLoad method)


NSURL *url = [NSURL fileURLWithPath:
      [[NSBundle mainBundle] pathForResource:@"MyFilename" ofType:@"mp3"]]; 

NSError *error;

_audioPlayer = [[AVAudioPlayer alloc] 
       initWithContentsOfURL:url error:&error]; 


if (error) 
{ 
    NSLog(@"Error in audioPlayer"); 
} 
else 
{
    _audioPlayer.delegate = self; 
    [_audioPlayer prepareToPlay];
	[_audioPlayer play];
}


*** Step 4: ***

You can add these methods to the ViewController.m file:

-(void)audioPlayerDidFinishPlaying: (AVAudioPlayer *)player successfully:(BOOL)flag 
{ 

} 

-(void)audioPlayerDecodeErrorDidOccur: (AVAudioPlayer *)player error:(NSError *)error 
{ 

} 

-(void)audioPlayerBeginInterruption:(AVAudioPlayer *)player 
{ 

} 

-(void)audioPlayerEndInterruption:(AVAudioPlayer *)player 
{ 

}


*** Step 5: ***


To Stop it:

[_audioPlayer stop];