In this application we will see how to audio file playing in the iPad. Basically this is the very easy application. So let see how it will be worked.

Step 1: Create a new project in Xcode using View base application. Give the application name “MusicPlayiPad”.

Step 2: Xcode automatically creates the directory structure and adds essential frameworks to it. You can explore the directory structure to check out the content of the directory.

Step 3: xpand classes and notice Interface Builder created the MusicPlayiPadViewController class for you. Expand Resources and notice the template generated a separate nib, MusicPlayiPadViewController.xib, for the “MusicPlayiPad”.

Step 4: In this application, we need to add two frameworks.So select Frameworks -> Add -> Existing Framework -> then select AVFoundation.framework and AudioToolbox.framework.

Step 5: We need to add sound files in the Resources folder. Give the name of the sound files “music.mp3”,”sound.aif”.

Step 6: Open the MusicPlayiPadViewController.h file, we have added in this file AVAudioPlayerDelegate protocol. They allow a delegate to respond to audio interruptions , to audio decoding errors , and to the completion of a sound’s playback. Added SystemSoundID for playing sound, AVAudioPlayer for playing song, UIButton for display the button and mention two IBAction. So make the following changes in the file.

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

 @interface MusicPlayiPadViewController : UIViewController
   SystemSoundID systemSoundID;
   AVAudioPlayer *player;
   UIButton *StartStopSound;

 @property (nonatomic, retain) IBOutlet AVAudioPlayer *player;
 @property (nonatomic, retain) IBOutlet UIButton *StartStopSound;

 - (IBAction) playSound: (id) sender;
 - (IBAction) playSong: (id) sender;

Step 7: Double click the MusicPlayiPadViewController.xib file and open it to the Interface Builder. Open the view window and first drag two Round Rect from the library and place it to the view window change the Round Rect name into “Play Sound” and “PlaySong”. Next connect File’s Owner icon to the view icon and select view. Select the Play Sound button from view window and bring up connection inspector, next drag from the TouchUpInside to the File’s Owner icon and select playSound: action. Do it once more time for Play Song button and select playSong: action and drag from the File’s Owner icon to the Play Sound button and select StartStopSound. Done all connection, now save NIB file, close it and go back to the Xcode.

Step 8: Open the MusicPlayiPadViewController.m file and make the following changes:

- (void)viewDidLoad {
  - (void)viewDidLoad {
        NSLog(@"InView did load");
        AudioServicesCreateSystemSoundID((CFURLRef)[NSURL fileURLWithPath:[[NSBundle mainBundle]
                                                                                                                                           pathForResource: @"sound" ofType:@"aif"]],                                                            &systemSoundID);

  player = [[AVAudioPlayer alloc]
  initWithContentsOfURL:[NSURL fileURLWithPath:
  [[NSBundle mainBundle] pathForResource:@"music" ofType:@"mp3"]]

  [player prepareToPlay];


 -(IBAction) playSound:(id) sender {

 -(IBAction) playSong:(id) sender {
  if ([[NSString stringWithFormat:@"%@", [StartStopSound     titleForState:UIControlStateNormal]] isEqualToString:@"Play Song"]) {
  [player play];
  [StartStopSound setTitle:@"Stop Song" forState:UIControlStateNormal];         }
else {
  [player stop];
  [StartStopSound setTitle:@"Play Song" forState:UIControlStateNormal];

Step 9: Now compile and run the application in the Simulator.

download the SourceCode from here MusicPlayiPad

Did you enjoy this article?
Get Free Updates