How to play sounds in iPhone ?

The iphone media player is very easy to use.

The iphone media player is very easy to use.

Step 1: First, create a view based iPhone application in xcode and add a button to your view that will play the sound.

Step 2: Drag in the Audio Toolbox framework into your frameworks group in XCode. Just Right click Frameworks from Groups&Files>add>existing framework>Framework>Audio ToolBox.framework>then add to project.

Step 3: Add  a short sound file to your project . Simple,just play the sound in an IBAction “playSound”.

   -(IBAction) playSound:(id) sender {

        AudioServicesPlaySystemSound(sampleSoundID);

   }

Step 4: If you want to play  .mp3 sound to your application, you need to play the sound in an IBAction “playSong”.

      -(IBAction) playSong:(id) sender {
            NSError *error = nil;
            player = [[AVAudioPlayer alloc] initWithContentsOfURL:
            [NSURL fileURLWithPath:[[NSBundle mainBundle]    pathForResource:
            @"music"ofType:@"mp3"]] error:&error];

             player.delegate = self;
             if(error != NULL) {

             NSLog([error description]);

            [error release];
       }
      [player play];
   }

You can downloaded SourceCode from here MusicPlayer

WP Greet Box icon
Hello there! If you are new here, you might want to subscribe to the RSS feed for updates on this topic, and free programming tips and tricks and source code            snippets.

Leave a Reply

Security Code:

learn iphone programming

Looking for something?

Use the form below to search the site:

Still not finding what you're looking for? Drop a comment on a post or contact us so we can take care of it!

Our Mobile Training Courses

EDUmobile.ORG offers the following 4 Mobile Training Courses. Our iPhone Training Course is very popular, with over 200 developers in training.

learn iphone programming
© 2010 EDUmobile.ORG