how to play music in iphone using iphone programming?

Leave a Comment
Start or Play music in iphone application.Don't worry about it. In Iphone Avfoundation framework is there to resolve your tension.  I am giving you complete steps to develop this tutorial.
Step 1:
Just give application name StartStopAudio.


 Step 2: Add Avfoundation framwork in your project looking this snap shot.


Step 3:
Dragged two buttons from object library to view and after it create their actions . for it just press ctrl and drag button in header file so it will give you this snap like pop up where select connection as action and give event name start and stop.


Step 4:
In this snap you have togive delegate <AVAudioPlayerDelegate> likethis..
@interface devangViewController : UIViewController<AVAudioPlayerDelegate>

 Step 5: add one music file in your project.

Step 6:
In controller implementation file add this code snippest to viewdidload method.
NSURL *url = [NSURL fileURLWithPath:[[NSBundle mainBundle]
                                         pathForResource:@"background-music-aac"
                                         ofType:@"caf"]];///////get path of your music file
   
    NSError *error;
    _audioPlayer = [[AVAudioPlayer alloc]
                    initWithContentsOfURL:url
                    error:&error];// initialize audio player
    if (error)
    {
        NSLog(@"Error in audioPlayer: %@",
              [error localizedDescription]);
    } else {
        _audioPlayer.delegate = self; // give delegate
        [_audioPlayer prepareToPlay];// prepare to play audio player
       
    }

 Step 7:
Add button this code snippest in start button action event:
- (IBAction)start:(id)sender {   

    [_audioPlayer play];// start play audio player
    UIAlertView *playalert = [[UIAlertView alloc]
                                    initWithTitle:@"Audio Started" message:@"" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
    [playalert show];  
    [_audioPlayer setNumberOfLoops:20];//this is for 20 times repetation of song
    [_audioPlayer setNumberOfLoops:-1];//infinite loop repetation 


Add this code snippest to stop audioplayer:
- (IBAction)stop:(id)sender {
    [_audioPlayer stop];
    UIAlertView *stopalert = [[UIAlertView alloc]
                              initWithTitle:@"Audio stopped" message:@"" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
    [stopalert show];
}


Step 9: Now run your app and press Start Audio Button


Step 9: Now run your app and press Stop Audio Button

devangcontroller.h
#import <UIKit/UIKit.h>
#import <AVFoundation/AVFoundation.h>
@interface devangViewController : UIViewController<AVAudioPlayerDelegate>
- (IBAction)start:(id)sender;
- (IBAction)stop:(id)sender;

@property (strong, nonatomic) AVAudioPlayer *audioPlayer;
@end


 devangcontroller.m

#import "devangViewController.h"
#import <AVFoundation/AVFoundation.h>
@interface devangViewController ()
@end
@implementation devangViewController
@synthesize audioPlayer=_audioPlayer;
- (void)viewDidLoad
{
    [super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
    NSURL *url = [NSURL fileURLWithPath:[[NSBundle mainBundle]
                                         pathForResource:@"background-music-aac"
                                         ofType:@"caf"]];
 
    NSError *error;
    _audioPlayer = [[AVAudioPlayer alloc]
                    initWithContentsOfURL:url
                    error:&error];
    if (error)
    {
        NSLog(@"Error in audioPlayer: %@",
              [error localizedDescription]);
    } else {
        _audioPlayer.delegate = self;
        [_audioPlayer prepareToPlay];
     
    }
}
- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (IBAction)start:(id)sender {
 
    [_audioPlayer play];
    UIAlertView *playalert = [[UIAlertView alloc]
                                    initWithTitle:@"Audio Started" message:@"" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
    [playalert show];
 
 
    [_audioPlayer setNumberOfLoops:20];//this is for 20 times repetation of song
    [_audioPlayer setNumberOfLoops:-1];//infinite loop repetation
 
}
- (IBAction)stop:(id)sender {
    [_audioPlayer stop];
    UIAlertView *stopalert = [[UIAlertView alloc]
                              initWithTitle:@"Audio stopped" message:@"" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
    [stopalert show];
}

@end

 I hope so you will be now able to run your application in iphone. If you still can not getting or getting any error please send me mail on devang9978@gmail.com or just leave comment on this post..

Devang Patel
Iphone Developer(www.techstudy.net)


0 comments:

Post a Comment