Music Play in iPhoneOS4

This is the very simple example . In this application we will see how to play music in iPhone oS4.

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

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 MusicPlay_OS4ViewController class for you. Expand Resources and notice the template generated a separate nib, MusicPlay_OS4ViewController.xib, for the “MusicPlay_OS4”.

Step 4: We need to add sound files in the Resources folder. Give the name of the sound files “music.mp3”,”sound.aif”. And also add a backgroung image.

Step 5: We need to add also two frameworks.So select Frameworks -> Add -> Existing Framework -> then select AVFoundation.framework and AudioToolbox.framework.

Step 6: In the MusicPlay_OS4ViewController.h file, we have  importAVFoundation.framework and AudioToolbox.framework. Create a instance of  AVAudioPlayer and UIButton class and create two IBAction method. So make the following changes in the file.

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

@interface MusicPlay_OS4ViewController : 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 MusicPlay_OS4ViewController.xib file and open it to the Interface Builder. First drag the Image View from the library and place it to the View window. Select the image view and bring up Attribute Inspector, select the background image. Drag two Round Rect button from the library and place it to the view window.Give the name of the button “Play Sound “and “Play Song” Connect File’s Owner icon to the View icon and select View. Drag File’s Owner icon to the Play Song button and select StartStopSound. Select the Play Sound button and bring up Connection Inspector and drag from the Touch Up Inside to the File’s Owner icon, select playSound: action. Do the same thing with PlaySong button and select the playSong: action. Now save the MusicPlay_OS4ViewController.xib file, close it and go back to the Xcode.

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

(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 {
       
        NSLog(@"In Sample");
        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.

You can Download Source Code from here MusicPlay_OS4

Leave a Reply

Your email address will not be published. Required fields are marked *