Frage

I am new in Objective-C. Please help me for this. I have one viewController which contains scrollview. scrollView are paging enabled so i have 10 pages on each page i have 30 buttons . My problem is when click on multiple button i want to play multiple sound..

My code : -

-(void)buttonClicked:(UIButton *)sender{
if (sender.tag == 1) {
 NSString *audioPath = [[NSBundle mainBundle]pathForResource:@"v1" ofType:@"mp3"];
NSURL *url = [NSURL fileURLWithPath:audioPath];
    audioPlayer = [[AVAudioPlayer alloc]initWithContentsOfURL:url error:nil];
    audioPlayer.delegate = self;
    [audioPlayer play];
}
 if (sender.tag == 2) {
 NSString *audioPath = [[NSBundle mainBundle]pathForResource:@"v2" ofType:@"mp3"];
NSURL *url = [NSURL fileURLWithPath:audioPath];
    audioPlayer = [[AVAudioPlayer alloc]initWithContentsOfURL:url error:nil];
    audioPlayer.delegate = self;
    [audioPlayer play];
}
 if (sender.tag == 3) {
 NSString *audioPath = [[NSBundle mainBundle]pathForResource:@"v3" ofType:@"mp3"];
NSURL *url = [NSURL fileURLWithPath:audioPath];
    audioPlayer = [[AVAudioPlayer alloc]initWithContentsOfURL:url error:nil];
    audioPlayer.delegate = self;
    [audioPlayer play];
}

problem is when i pressed on buttons only one sound is enable. how to enable multiple sounds ?? please help me... thanks in advance

War es hilfreich?

Lösung

First import #import <AudioToolbox/AudioServices.h>

    NSString *path  = [[NSBundle mainBundle] pathForResource:@"soundeffect" ofType:@"m4a"];
    NSURL *pathURL = [NSURL fileURLWithPath : path];

    SystemSoundID audioEffect;
    AudioServicesCreateSystemSoundID((CFURLRef) pathURL, &audioEffect);
    AudioServicesPlaySystemSound(audioEffect);

In each UIButton you need change the path with the sound that you need play. Hope this help :)

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top