点击Multiple UIButton如何在同一时间播放多个声音?

时间:2014-04-02 12:37:09

标签: ios objective-c uibutton avaudioplayer

我是Objective-C的新手。请帮帮我。 我有一个包含scrollview的viewController。 scrollView是分页启用所以我每页有10页我有30个按钮。 我的问题是当点击多个按钮我想播放多个声音..

我的代码: -

-(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];
}

问题是当我按下按钮时,只启用一个声音。如何启用多个声音? 请帮我... 提前谢谢

1 个答案:

答案 0 :(得分:1)

首先导入#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);

在每个UIButton中,您需要使用您需要播放的声音更改路径。 希望这有帮助:)