2013-05-08 4 views
3

나는 많은 해결책을 stackoverflow에 여기에서 찾아 냈다. 그러나 그들 모두는 어떤 소리도 만들어 내지 못했다.iOS에서 사운드를 재생하는 방법은 무엇입니까?

PRE: I've added AudioToolbox.framework and imported <AVFoundation/AVFoundation.h> in my .m file 


- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    NSURL* sndurl = [[NSBundle mainBundle] URLForResource:@"test" withExtension:@"aif"]; 
    SystemSoundID snd; 
    AudioServicesCreateSystemSoundID((__bridge CFURLRef)sndurl, &snd); 
    AudioServicesAddSystemSoundCompletion(snd, nil, nil, SoundFinished, nil); 
    AudioServicesPlaySystemSound(snd); 
} 

void SoundFinished (SystemSoundID snd, void* context) { 
     AudioServicesRemoveSystemSoundCompletion(snd); 
     AudioServicesDisposeSystemSoundID(snd); 
} 

이 솔루션은 바로 책에서입니다 :

여기에 내 현재 코드입니다. 그러나 프로젝트를 실행할 때 소리가 들리지 않습니다. 내 파일은 test.aif이며 길이는 5 초입니다.

왜 작동하지 않는가? 이것이 최선의 해결책이 아니라면 iOS6에서 어떻게 소리를 낼 수 있습니까?

+1

하드웨어 음소거 스위치가 꺼져 있는지 확인하십시오. 자동 모드에서는 시스템 소리가 꺼집니다. –

답변

6

this topic 및 기타에 따르면 오디오 클립 재생은 다음과 같이 간단 할 수 있습니다. 내 방법 (기능)을 사용합니다. 나는 나 자신보다 먼저 해냈다.

- (void)playAudio { 
    [self playSound:@"pageflip1" :@"wav"]; 
} 

- (void)playSound :(NSString *)fName :(NSString *) ext{ 
    SystemSoundID audioEffect; 
    NSString *path = [[NSBundle mainBundle] pathForResource : fName ofType :ext]; 
    if ([[NSFileManager defaultManager] fileExistsAtPath : path]) { 
     NSURL *pathURL = [NSURL fileURLWithPath: path]; 
     AudioServicesCreateSystemSoundID((__bridge CFURLRef) pathURL, &audioEffect); 
     AudioServicesPlaySystemSound(audioEffect); 
    } 
    else { 
     NSLog(@"error, file not found: %@", path); 
    } 
} 
+0

귀하의 코드는 경로 변수에 '(null)'을 반환합니다. 나는 이유를 알 수 없다. –

+0

오디오 파일을 프로젝트 사이드 바에 드래그 앤 드롭하십시오. –

+0

감사합니다. 파일을 추가 할 때 "대상에 추가"체크 상자가 선택되지 않은 것으로 나타났습니다. 그것은 선택되어야한다. –

0

AVFoundation을 사용할 때 시뮬레이터에 문제가 발생할 수 있으므로 실제 장치에서 테스트 해보십시오.

관련 문제