user2452016
user2452016

Reputation:

How to add multiple AVAudioPlayer in same ViewController

In my app I'm displaying 5 UIView and by touching any view sound will play. So how can I add multiple AVAudioPlayer in same ViewController.

For single View I'm adding this code to implement AVAudioPlayer

NSURL *url = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"Sound" ofType:@"MP3"]];

NSError *error;
audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:&error];
if (error)
{
    NSLog(@"Error in audioPlayer: %@", [error localizedDescription]);
} else {
    audioPlayer.delegate = self;

    //theAudio.numberOfLoops = -1;
    [audioPlayer prepareToPlay];
}

So should I just create 5 AVAudioPlayer instantances and add to every view or any other way to reuse this code for every view?

Upvotes: 1

Views: 1032

Answers (3)

Dilip Manek
Dilip Manek

Reputation: 9143

You can create Multiple AudioPlayer instance using same code like this

In .h file

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

@interface MultipleChoiceViewController : UIViewController<AVAudioPlayerDelegate>
{
    AVAudioPlayer *dingAudioPlayer;
    AVAudioPlayer *buzzAudioPlayer;
}
-(IBAction) playDingAudio:(id) sender;
-(IBAction) playBuzzAudio:(id) sender;

And in your .m file

- (void)viewDidLoad
{
    dingAudioPlayer = [self loadAudio:@"ding Audio" audioType:@"mp3"];
    buzzAudioPlayer = [self loadAudio:@"buzz Audio" audioType:@"mp3"];
   [buzzAudioPlayer play];
}

- (AVAudioPlayer *)loadAudio:(NSString *)filename audioType:(NSString *)audioType{
    NSURL * url = [[NSBundle mainBundle] URLForResource:filename withExtension:audioType];
    NSError * error;
    AVAudioPlayer * player = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:&error];
    if (!player) {
        NSLog(@"Error loading %@: %@", url, error.localizedDescription);
    } else {
        player.delegate = self;
        [player prepareToPlay];
    }
    return player;
}


#pragma mark - AvAudio Player delegate methods

-(void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)flag
{
    if (player != dingAudioPlayer && player != buzzAudioPlayer) {
        player = nil;
    }
    NSLog(@"finished");
}
-(void)audioPlayerDecodeErrorDidOccur:(AVAudioPlayer *)player error:(NSError *)error
{
    NSLog(@"AVAudioPlayer Error : %@",error);
}
-(void)audioPlayerBeginInterruption:(AVAudioPlayer *)player
{
}
-(void)audioPlayerEndInterruption:(AVAudioPlayer *)player
{
}

-(IBAction) playDingAudio:(id) sender{
   [dingAudioPlayer play];
}
-(IBAction) playBuzzAudio:(id) sender{
   [buzzAudioPlayer play];
}

Upvotes: 2

iAhmed
iAhmed

Reputation: 6704

Use only One AVAudioPlayer instance to play sounds. All you have to do is,Assign different sound to each view and assign same playing sound method to all views.

make 5 urls like

NSURL *urlsound1 = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"Sound" ofType:@"MP3"]];
NSURL *urlsound2 = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"Sound" ofType:@"MP3"]];

Pass urls as parameters to play method

Upvotes: 0

Toseef Khilji
Toseef Khilji

Reputation: 17409

You can implement it with one AVAudioPlayer, you have to create one common method for all your 5 view, when your touch method is called according to view you can set the sound file of AVAudioPlayer.

Upvotes: 0

Related Questions