This is the small iPhone SDK example that will show you how to play, pause and stop the music on button clicks.
This is the small iPhone SDK example that will show you how to play, pause and stop the music on button clicks.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> @interface buttonSoundViewController : UIViewController <AVAudioPlayerDelegate> { -(IBAction) playSound:(id)sender; @end |
buttonSoundViewController.h
#import "buttonSoundViewController.h" @implementation buttonSoundViewController
-(IBAction) playSound:(id)sender{ -(IBAction) stop:(id)sender{ -(IBAction) pause:(id)sender{ // Implement viewDidLoad to do additional setup after loading the view, typically from a nib. theAudio=[[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL]; theAudio.delegate=self; theAudio.numberOfLoops = -1; [super viewDidLoad]; - (void)didReceiveMemoryWarning { - (void)viewDidUnload { } - (void)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 { [super viewDidLoad]; |
Button Actions
-(IBAction) playSound:(id)sender{ -(IBAction) pause:(id)sender{ |
The final application will look like this..
Just click on the buttons to play, pause and stop the music.