Play MP3 Files with iPhone SDK

For short sounds or when the MP3 does not play well on the suggested code you can always use:

SystemSoundID soundID; 
NSURL *url = [NSURL fileURLWithPath:[NSString stringWithFormat:@"%@/sound.mp3", [[NSBundle mainBundle] resourcePath]]];

AudioServicesCreateSystemSoundID((CFURLRef)url, &soundID); 
AudioServicesPlaySystemSound (soundID);

Don't forget to add:

#import <AudioToolbox/AudioToolbox.h>

I'm afraid the answer stated no longer works in iOS 7 and above. You will need to use the following code:

in the header file (.h)

In order to handle the delegate methods like when the playing of the audio has finished audioPlayerDidFinishPlaying:, inherit from AVAudioPlayerDelegate .

@property (nonatomic, strong) AVAudioPlayer *player;

in the implementation file (.m)

 NSString *soundFilePath = [[NSBundle mainBundle] pathForResource: resourceName
                                                          ofType: @"mp3"];
 NSURL *fileURL = [[NSURL alloc] initFileURLWithPath: soundFilePath];

 AVAudioPlayer *newPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL: fileURL
                                                                  error: nil];
_player = newPlayer;
[_player prepareToPlay];
[_player setDelegate: self];
[_player play];

well here is a good tutorial available.

The theme is

NSURL *url = [NSURL fileURLWithPath:[NSString stringWithFormat:@"%@/audiofile.mp3", [[NSBundle mainBundle] resourcePath]]];

    NSError *error;
    AVAudioPlayer *audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:&error];
    audioPlayer.numberOfLoops = -1;

[audioPlayer play];

and when you want to pause;

[audioPlayer pause];

hope this helps.


These are the codes for the requested actions, appSoundPlayer is a property of AVAudioPlayer declared in h file. Also this example plays a song in the resource folder.

#pragma mark -
    #pragma mark *play*
    - (IBAction) playaction {

        NSString *soundFilePath = [[NSBundle mainBundle] pathForResource:@"songname" ofType:@"mp3"];
        NSURL *newURL = [[NSURL alloc] initFileURLWithPath: soundFilePath];
        self.soundFileURL = newURL;
        [newURL release];
        [[AVAudioSession sharedInstance] setDelegate: self];
        [[AVAudioSession sharedInstance] setCategory: AVAudioSessionCategoryAmbient error: nil];

    // Registers the audio route change listener callback function
    AudioSessionAddPropertyListener (
                                     kAudioSessionProperty_AudioRouteChange,
                                     audioRouteChangeListenerCallback,
                                     self
                                     );

    // Activates the audio session.

    NSError *activationError = nil;
    [[AVAudioSession sharedInstance] setActive: YES error: &activationError];

    AVAudioPlayer *newPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL: soundFileURL error: nil];
    self.appSoundPlayer = newPlayer;
    [newPlayer release];
    [appSoundPlayer prepareToPlay];
    [appSoundPlayer setVolume: 1.0];
    [appSoundPlayer setDelegate: self];
    [appSoundPlayer play];


    [stopbutton setEnabled:YES];
    [playbutton setEnabled: NO];
    playbutton.hidden=YES;
    pausebutton.hidden =NO;
}//playbutton touch up inside

#pragma mark -
#pragma mark *pause*
-(IBAction)pauseaction {
    [appSoundPlayer pause];
    pausebutton.hidden = YES;
    resumebutton.hidden = NO;

}//pausebutton touch up inside

#pragma mark -
#pragma mark *resume*
-(IBAction)resumeaction{
    [appSoundPlayer prepareToPlay];
    [appSoundPlayer setVolume:1.0];
    [appSoundPlayer setDelegate: self];
    [appSoundPlayer play];
    playbutton.hidden=YES;
    resumebutton.hidden =YES;
    pausebutton.hidden = NO;

}//resumebutton touch up inside

#pragma mark -
#pragma mark *stop*
-(IBAction)stopaction{

    [appSoundPlayer stop];
    [playbutton setEnabled:YES];
    [stopbutton setEnabled:NO];
    playbutton.hidden=NO;
    resumebutton.hidden =YES;
    pausebutton.hidden = YES;

}//stopbutton touch up inside

Tags:

Ios

Audio