Home Tutorial Iphone iPhone play, pause and stop playing music

 
 

iPhone play, pause and stop playing music
Posted on: December 17, 2009 at 12:00 AM
Advertisement
This is the small iPhone SDK example that will show you how to play, pause and stop the music on button clicks.

iPhone play, pause and stop playing music

This is the small iPhone SDK example that will show you how to play, pause and stop the music on button clicks.

You can create the example in view based application. But for playing the music you need to add AVFoundation. framework into the Frameworks folder. After that just drag and paste the music or audio file into the resources folder, which you are going to play on button click.

Once you done, just open your ViewController.xib file and add three buttons for the different actions and hooked them with the appropriate actions. Now just look into the code ... your code must look like the given one.

buttonSoundViewController.h

#import <UIKit/UIKit.h>
#import<AVFoundation/AVAudioPlayer.h>

@interface buttonSoundViewController : UIViewController <AVAudioPlayerDelegate> {
   
    AVAudioPlayer *theAudio;
   
}

-(IBAction) playSound:(id)sender;
-(IBAction) stop:(id)sender;
-(IBAction) pause:(id)sender;

@end

buttonSoundViewController.h

#import "buttonSoundViewController.h"

@implementation buttonSoundViewController

 

-(IBAction) playSound:(id)sender{
   
    [theAudio play];
     
}

-(IBAction) stop:(id)sender{
    [theAudio stop];
}

-(IBAction) pause:(id)sender{
    [theAudio pause];
}

// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
 
NSString *path = [[NSBundle mainBundle] pathForResource:@"awh_man" ofType:@"wav"];

theAudio=[[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL];

theAudio.delegate=self;

theAudio.numberOfLoops = -1;

    [super viewDidLoad];
}

- (void)didReceiveMemoryWarning {
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
   
    // Release any cached data, images, etc that aren't in use.
}

- (void)viewDidUnload {
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;

}

- (void)dealloc {
  [super dealloc];
}  

@end


Unlike the previous example of "sound on button click", we have created the NSBundle class but instead of defining it into the button action, we witted it into the "ViewDidLoad" event. And on the button actions we have just given the variable names with appropriate actions like play, pause and stop. Take a look at the code...

ViewDidLoad event

- (void)viewDidLoad {
 
NSString *path = [[NSBundle mainBundle] pathForResource:@"awh_man" ofType:@"wav"];
theAudio=[[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL];
theAudio.delegate=self;
theAudio.numberOfLoops = -1;

    [super viewDidLoad];
}

Button Actions

-(IBAction) playSound:(id)sender{
   
    [theAudio play];
     
    }
-(IBAction) stop:(id)sender{
    [theAudio stop];
}

-(IBAction) pause:(id)sender{
    [theAudio pause];
}

The final application will look like this..

Just click on the buttons to play, pause and stop the music.

Download code

Advertisement

Related Tags for iPhone play, pause and stop playing music:


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: December 17, 2009

Recommend the tutorial

Advertisements Advertisements
 

 

 

DMCA.com