12

What's the easiest way to play a music file such as Mp3 with pause button? very very simple a button play and another button pause that music

Kara
  • 6,115
  • 16
  • 50
  • 57
Mc.Lover
  • 4,813
  • 9
  • 46
  • 80

8 Answers8

33

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
Nithin
  • 6,435
  • 5
  • 43
  • 56
25

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>
Julio Bailon
  • 3,735
  • 2
  • 33
  • 34
7

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.

Jagat Dave
  • 1,643
  • 3
  • 23
  • 30
Madhup Singh Yadav
  • 8,110
  • 7
  • 51
  • 84
  • 1
    thanks a lot i love http://stackoverflow.com and of course you :D – Mc.Lover Dec 29 '09 at 11:19
  • OK , i have some problem again . when i tap the play button several times the music plays on and on ... !! how can play music 1 time ? and about pause button , who works it ? sorry iam amateur : – Mc.Lover Dec 29 '09 at 11:51
5

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];
madLokesh
  • 1,860
  • 23
  • 49
Hashim Akhtar
  • 813
  • 2
  • 11
  • 16
1

I like simple code and here is my solution : (Remember to add switch button to get music play. Have fun)

#import "ViewController.h"
#import <AVFoundation/AVFoundation.h>

@interface ViewController ()
{
    NSURL*                  bgURL;
    AVAudioPlayer*          bgPlayer;
}
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    bgURL = [[NSURL alloc] initFileURLWithPath: [[NSBundle mainBundle] pathForResource:@"sample" ofType:@"mp3"]];
    bgPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:bgURL error:nil];
}

#pragma mark AVAudioPlayer
- (IBAction)toggleMusic:(UISwitch*)sender {
    NSLog(@"togging music %s", sender.on ? "on" : "off");

    if (bgPlayer) {

        if (sender.on) {
            [bgPlayer play];
        }
        else {
            [bgPlayer stop];
        }
    }   
}
Nhat Dinh
  • 3,378
  • 4
  • 35
  • 51
0

DIRAC API is free and quite easy to use. We've used it in my talking robot app http://itunes.apple.com/us/app/daidai/id484833168 and it has been amazing to me how it manages to change the speed and pitch of voice

http://www.dspdimension.com/download/

Jim Huang
  • 401
  • 5
  • 9
0

The Apple documentation here should have everything you need to know.

Mongus Pong
  • 11,337
  • 9
  • 44
  • 72
0

The oalTouch sample code on the Apple iOS developer web site does what you want, and is ready to run.

It also shows how to test if another app (eg ipod) is playing a file already, which is handy if you want to allow your users to listen to their own music instead of yours.

Jagat Dave
  • 1,643
  • 3
  • 23
  • 30
bw1024
  • 1,138
  • 12
  • 15