iOS give AVFoundation framework for playing audio in iOS app.

The AV Foundation framework provides an Objective-C interface for managing and playing audio-visual media in iOS and OS X applications.

Read Apple Documentation

Steps:

  1. Create a new Xcode Project for (Single View Application).
  2. Give Project a name (Lets say AudioPlayer)
  3. Click next Button and the project will be created.
  4. Now go to Main StoryBoard of the Project and In the Object Library, select the “button” object and drag it into the view. Do it three times.
  5. Create action for three buttons in “ViewController.m”.

ViewController.m  will look something like this after creating actions for button.

– (IBAction)stopAudioButton:(id)sender {

}

– (IBAction)pauseAudioButton:(id)sender {

}

– (IBAction)playAudioButton:(id)sender {

}

6.  In the Content Area, select “AudioPlayer” under Targets and click “Build Phases”. Expand “Link Binary with Libraries” and click the “+” button to add the “AVFoundation.framework”.

7. To use the AVAudioPlayer and AVAudioRecorder class, we need to import in ViewController.m

#import <AVFoundation/AVFoundation.h>

8.Now go to the interface section and create a object of AVAudioPlayer

@interface ViewController (){

AVAudioPlayer *audioPlayer;

}

9 Now in the play audio Button action We will write the method for getting the path of the mp3 player which is in our project folder. Make Sure that you have that mp3 player in the project and you should must add the file in the Project. Now go to playAudioButton Action and write this code their.

– (IBAction)playAudioButton:(id)sender {

NSString *path = [[NSBundle mainBundle]

pathForResource:@”nelly” ofType:@”mp3″];

audioPlayer = [[AVAudioPlayer alloc]initWithContentsOfURL:

[NSURL fileURLWithPath:path] error:NULL];

[audioPlayer play];

}

10. Now to stop and  pause You have to just write a single Line of Code.

#pragma mark – action Methods

– (IBAction)stopAudioButton:(id)sender {

[audioPlayer stop];

}

– (IBAction)pauseAudioButton:(id)sender {

[audioPlayer pause];

}

11. Your ViewController.m will Look finally like this

// ViewController.m

// SimpleAudioVideoPlayer

//

// Created by Nilesh on 4/13/16.

// Copyright © 2016 Nilesh. All rights reserved.

//

#import “ViewController.h”

@interface ViewController (){

AVAudioPlayer *audioPlayer;

}

@end

@implementation ViewController

– (void)viewDidLoad {

[super viewDidLoad];

// Do any additional setup after loading the view, typically from a nib.

}

– (void)didReceiveMemoryWarning {

[super didReceiveMemoryWarning];

// Dispose of any resources that can be recreated.

}

 

 

#pragma mark – action Methods

– (IBAction)playAudioButton:(id)sender {

NSString *path = [[NSBundle mainBundle]

pathForResource:@”nelly” ofType:@”mp3″];

audioPlayer = [[AVAudioPlayer alloc]initWithContentsOfURL:

[NSURL fileURLWithPath:path] error:NULL];

[audioPlayer play];

}

 

 

– (IBAction)stopAudioButton:(id)sender {

[audioPlayer stop];

}

– (IBAction)pauseAudioButton:(id)sender {

[audioPlayer pause];

}

 

@end

12. Now just run the Program and see the desire output. Enjoy Coding. 🙂

Advertisements