2009-12-19 4 views
0

AVAudioPlayer를 사용하여 사운드를 재생하려고합니다. 간단해야하지만, 이상한 결과가 나타납니다.iPhone에서 AVAudioPlayer로 이상한 동작이 발생했습니다.

코드 :

NSString *path = [[NSBundle mainBundle] pathForResource:@"pop" ofType:@"wav"]; 
NSURL *url = [NSURL fileURLWithPath:path]; 
AVAudioPlayer *sound = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:nil]; 
[sound play]; 
[sound release]; 

무엇 제가 보는 것은 일반적으로 응용 프로그램을 사용할 때 소리가 재생되지 않는다는 것입니다.

그것은 ... 내가 디버거를 사용하여 코드를 단계별 경우 다른 방법을 실행할 때 , 그것은 재생되지 않습니다 내가 내 응용 프로그램 내에서 새로운 스레드를 실행하거나 루프를 생성하고 있지 않다

을한다 , 이것은 모두 주 스레드에서 실행되어야합니다. 적어도 [NSThread isMainThread]은 true를 반환합니다.

여기에 무슨 일이 벌어지고 있는지에 대한 의견이 있으십니까?

답변

4

AVAudioPlayer의 play 메서드는 비동기식이므로 사운드를 재생하고 즉시 해제합니다. 이것이 디버거에서 단계별로 수행 할 때 작동하는 이유입니다. 디버거를 죽이기 전에 재생할 시간을줍니다. AVAudioPlayerDelegate의 -(void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)flag 메서드를 구현하고 사운드 재생이 끝나면 오디오 플레이어를 릴리스합니다.

0

파파 존이 질주합니다 터치는

AVAudioPlayer *classLevelPlayer; 

합성이 객체와 같은 오디오 플레이어의 클래스 레벨의 변수이

처럼해야 할 권리가있다. 그리고 플레이어 방법

-(void)playTheSong{ 

if(classLevelPlayer!=nil){ 
[classLevelPlayer stop]; 
[self setClassLevelPlayer:nil]; 
} 

NSString *path = [[NSBundle mainBundle] pathForResource:@"pop" ofType:@"wav"]; 
NSURL *url = [NSURL fileURLWithPath:path]; 

AVAudioPlayer *sound = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:nil]; 

if(sound){ 
[self setClassLevelPlayer:sound]; 
[classLevelPlayer play]; 
} 
[sound release]; 
} 

에 대한 호출과

-(void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)flag { 
[self setClassLevelPlayer:nil]; 
} 

희망이 도움이됩니다.