2014-04-02 37 views
0

我是Objective-C中的新成員。請幫助我。 我有一個viewController包含scrollview。 scrollView是分頁啓用,所以我有10頁在每頁我有30個按鈕。 我的問題是,當點擊按鈕多我要玩多個聲音..點擊多個UIButton如何在同一時間播放多個聲音?

我的代碼: -

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

問題是,當我按下按鈕上只有一個聲音啓用。如何啓用多個聲音? 請幫我... 由於事先

+0

上面的代碼有什麼問題? – Apurv

+0

你還沒有描述你的問題。當你按下多個按鈕時發生了什麼? –

+0

@Agent不,我想混合多種音頻聲音。 – Vishal16

回答

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。 希望得到這個幫助:)