Playing Audio file in iPad

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
 <avaudioPlayerDelegate>
 {
   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"]]
  error:nil];

  [player prepareToPlay];

}

 -(IBAction) playSound:(id) sender {
  AudioServicesPlaySystemSound(systemSoundID);
}

 -(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

  • Rick
  • April 21, 2010
  • iOS

About the Author

Leave a Reply 11 comments

Balaji - June 2, 2010 Reply

It is helped a lot.Thank You Very much.

Mary - August 12, 2010 Reply

I was trying this tutorial with just a song and not a sound file and when I click the button Play Song nothing happens. I have a music.mp3 under resources folder and I also connected the button to the action in xib.

Sushant - August 12, 2010 Reply

I think you missed something.. Do you add AudioToolbox and AVFoundation framework in your project?Just check it. If there is any other problem then please let us know.

Mary - August 12, 2010 Reply

OK, I found my problem that I did not make the connection for StartStopSound. that was resolved, but you have this &systemSoundID); where &amp was giving me error of &amp not declared so I downloaded your code and saw that you don’t have it there and it is just &systemSoundID); .

Mary - August 12, 2010 Reply

OK, I found my problem that I did not make the connection for StartStopSound. that was resolved, but you have this &amp;systemSoundID); where &amp was giving me error of &amp not declared so I downloaded your code and saw that you don’t have it there and it is just &systemSoundID); .

Sushant - August 17, 2010 Reply

Thanks Mary..!!! I have changes in the post, actually there was some publishing problem..!!!

IPAAC - September 16, 2010 Reply

I’ve been trying to modify the code so that I could have a screen with multiple buttons, which would play a brief sound clip when pressed.

I took out all the code for the Play Song button and replicated the steps for the Play Sound button, but for some reason the buttons don’t play sound when they are pressed. Any help with this would be appreciated.

Lee Craven - October 7, 2010 Reply

This doesn’t seem to work with iOS 4

iasasolution - January 31, 2011 Reply

how do i play multiple sounds?
i declared them in .h file, but i dont know how to add them in .m?
Do i have to create an array of sounds?
Thank you

stevel - September 8, 2011 Reply

Copied source; updated build to ios4.2; complied (release, device, arm7); ssh to ipad:/applications; ran uicache; tried but didn\’t launch.

This is my 3rd attempt at trying to get any example using sound to work on my ipad. :(

online casino - May 11, 2013 Reply

An additional issue is that video games are generally serious as the name indicated with the primary focus on mastering rather than amusement. Although, there is an entertainment factor to keep children engaged, every single game is frequently designed to focus on a specific group of skills or course, such as math or science. Thanks for your post.

Leave a Reply: