views:

2694

answers:

3

hi .... what 's the easiest way to play a music file such as Mp3 with pause botton ? very very simple a button play and another button pause that music .. can you write that code !

+3  A: 

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
have you sample code of this tutorial ?
Momeks
i've implemented the same. i created 3 buttons, playbutton, pausebutton, and resume button and aligned them one on top of the other. create outlets for them, as IBOutlet UIbutton *playbutton, and also for other buttons similarly, connect each of them to these outlets. Also create actions as in the code, playaction, pauseaction and resume action, and connect them accordingly. In my view any of these buttons will be appearing at a time. pls inform for any further help..
Nithin
remember to add playbutton.hidden=NO; resumebutton.hidden =YES; pausebutton.hidden = YES;in viewDidLoad fuction
Nithin
those errors are their due to that you haven't declared them in the h file.
Nithin
the relevent parts of the code are here...http://freezpic.com/pics/b8293f8c82671693bf4e6eeda5812e06.jpghttp://freezpic.com/pics/3624e8e565ad67cadafefb1a2c330b29.jpghttp://freezpic.com/pics/6aa3bb211741f8e9dcb59c84e1ec374a.jpghttp://freezpic.com/pics/e5c12b3ca38393c111a36dce5100b851.jpgthe only part missing is the interfacebuilder section which you have to do it yourself.If you are still getting errors, inform me.
Nithin
also note that the class name yellowViewController is given by me, you have to replace it with yours. The first 2 image is for the h file and the other 2 in the m file
Nithin
A: 

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

Mongus Pong
+2  A: 

well there is a good tutorial available at

http://www.mobileorchard.com/easy-audio-playback-with-avaudioplayer/

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.

Madhup
thanks a lot i love http://stackoverflow.com and of course you :D
Momeks
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 :
Momeks