Monday, 25 March 2013

How to add background music in iOS app


Adding background music in iOS app is very simple. You will have to do following steps.

Step 1

Add music file(which you want to play) into your resources or supporting files folder in your project

Right click on Resources folder or Supporting files and chose "Add Files to ..." 


Now Chose the file which you want to play and click on "Add" button. Remember "copy items into destination group's" should be checked


 After this your file will be added in supporting files or resources folder.

  


Step 2

Add AVFoundation framework into your project.

For this single click on your project name present in upper left corner of project navigator.
 The following window will be opened

Now do according to no given in following pic. 1 click on build phases.2- click on arrow of link binary with libraries.3-click on "+" button.


After 3 in above pic following window will be open. Now you have to chose AVFoundation.framework from the list of frameworks.

 Write "AVFoundation" in search bar & chose AVFoundation.framework and click "Add" button


AVFoundation framework has successfully added into your project 

Step 3 

Now go to .h file and   import AVFoundation frame work and make property of AVAudioPlayer
 



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

@interface HomeViewController : UIViewController
@property (nonatomic, retain) AVAudioPlayer *player;

@end

Now go to .m file and synthesize the property, also write code to play music in viewDidLoad().




@synthesize player;

- (void)viewDidLoad
{
    [super viewDidLoad];
    //AVAudioPlayer *player;
    NSURL *url = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"a" ofType:@"mp3"]];
    
    NSError *error;
    player = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:&error];
    player.numberOfLoops = -1;
    
    if (error)
    {
        NSLog(@"Error in audioPlayer: %@",[error description]);
    } else {
        [player play];
    }
}  

Now run the app it will work fine


NOTE  Format of file which you want to play should be accurate. Some time music did not played while file format is mp3, in that case there is a problem with that file. Use another file to solve the problem.

2 comments:

  1. Hey there... I appreciate your post and info. I was able to add a background sound easily.

    My problem is that the sound now plays two and three times at the same time.. as many times as I press the play button. Do you have code so it only plays once no matter how many times you restart the game? I'd like it to loop as well.
    Thanks,
    Rick

    ReplyDelete
    Replies
    1. Hi Rick,
      I think you write the code of playing music in an action method which is called when you press the play button. In this situation when you press the play button first time the music works fine but when you press it more than once, music plays multiple time equal to no of taps on play button. Am I right?

      Actually when you press the button again again the code of playing music executes again again due to which music plays multiple time. If you want to play it only once then you will have to stop the previous running music before playing it again. You can stop it by calling the stop method of AVAudioPlayer like this

      [AVAudioPlayer stop];

      If you have still issues then you can send me code I will solve the issue OR I will send you code having this functionality.

      Delete