2011-04-14 7 views
0

한 화면에 10 개의 사운드가 있습니다. 그리고 그들은 모두 연주하지만 한 번에 하나의 사운드 만 재생할 수 있습니다. 나는 당신이 연주하고 싶은 소리를 두들 기울 수 있기를 원하고 동시에 모두 연주한다. 그러나 연주하는 소리를 누를 때 다른 소리를 누르면 이전에 소리가 멈 춥니 다.iOS 기기에서 동시에 사운드 재생

는 여기에 내가 소리

- (IBAction)oneSound:(id)sender; { 
    NSString *path = [[NSBundle mainBundle] pathForResource:@"1" ofType:@"wav"]; 
    if (theAudio) [theAudio release]; 
    NSError *error = nil; 
    theAudio = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:&error]; 
    if (error) 
     NSLog(@"%@",[error localizedDescription]); 
    theAudio.delegate = self; 
    [theAudio play]; 


} 



- (IBAction)twoSound:(id)sender; { 
    NSString *path = [[NSBundle mainBundle] pathForResource:@"2" ofType:@"wav"]; 
    if (theAudio) [theAudio release]; 
    NSError *error = nil; 
    theAudio = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:&error]; 
    if (error) 
     NSLog(@"%@",[error localizedDescription]); 
    theAudio.delegate = self; 
    [theAudio play]; 

} 



- (IBAction)threeSound:(id)sender; { 
    NSString *path = [[NSBundle mainBundle] pathForResource:@"3" ofType:@"wav"]; 
    if (theAudio) [theAudio release]; 
    NSError *error = nil; 
    theAudio = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:&error]; 
    if (error) 
     NSLog(@"%@",[error localizedDescription]); 
    theAudio.delegate = self; 
    [theAudio play]; 

} 

덕분에 사용한 코드입니다!

답변

0

theAudio가 글로벌 인 것처럼 보이고 각 메소드에서 다시 인스턴스를 생성합니다. 지역 변수로 변환하거나 3을 정의하십시오.

+0

어떻게하면 그 – LAA

2

이 아마 문제가 발생합니다

if (theAudio) [theAudio release]; 

출시있어 후 AVAudioPlayer 재생을 계속할 수 없습니다. theAudio이
초기화 - - 클릭
버튼 - theAudio을
재생을 시작합니다 - 또 다른 버튼을
을 클릭 - theAudio이 출시 및 그이
재생 정지 - theAudio 다른 소리로 초기화됩니다 가
: 기본적으로 어떻게됩니까


- theAudio는 재생 시작

+0

을 삭제할 수 있습니까? – LAA

+0

아니요, 간단히 헤더 파일에'theAudio' 대신'oneAudio','twoAudio' 및'threeAudio'을 만듭니다. 이제'oneAudio'를'- (IBAction) oneSound : (id) sender' 등으로 재생할 수 있습니다. – Anne