2011-03-12 4 views
0

지나치게 UIImagePicker에 버튼이 있습니다.이 버튼을 클릭하면 소리가납니다. 이렇게하려면 프로젝트 리소스에 m4a 및 mp3 형식의 사운드 (테스트 용)가 있어야합니다.iPhone - AudioServicesPlaySystemSound 및 AVAudioPlayer로 재생되지 않는 사운드

m4a 및 mp3 형식으로 여러 가지 방법으로 사운드를 재생하려고하지만 시뮬레이터가 아닌 iPhone에서는 아무 것도 나가지 않습니다.

프레임 워크는 프로젝트에 포함됩니다.

#import <AudioToolBox/AudioToolbox.h> 
#import <AVFoundation/AVAudioPlayer.h> 

- (IBAction) click:(id)sender { 
// Try 
/* 
    SystemSoundID train; 
    AudioServicesCreateSystemSoundID(CFBundleCopyResourceURL(CFBundleGetMainBundle(), CFSTR("TheSound"), CFSTR("mp3"), NULL), &train); 
    AudioServicesPlaySystemSound(train); 
*/ 

// Try 
/* 
    NSError *error; 
    AVAudioPlayer *audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:[[NSBundle mainBundle] URLForResource: @"TheSound" withExtension: @"mp3"] error:&error]; 
    [audioPlayer play]; 
    [audioPlayer release]; 
*/ 

// Try (works) 
    //AudioServicesPlayAlertSound(kSystemSoundID_Vibrate); 

// Try 
/* 
    SystemSoundID soundID; 
    NSURL *filePath = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"TheSound" ofType:@"mp3"] isDirectory:NO]; 
    AudioServicesCreateSystemSoundID((CFURLRef)filePath, &soundID); 
    AudioServicesPlaySystemSound(soundID); 
*/ 

    NSLog(@"Clicked"); 
} 

당신이 잘못 무슨 일이 일어나고 있는지 말해 줄 수 : 여기

내 샘플 코드인가?

답변

6

오디오 서비스와 함께 MP3를 사용할 수 없습니다. 특정 하위 유형의 WAV 파일이어야합니다. AVAudioPlayer에 대해서는 MP3 파일을 지원하지만 AVAudioPlayer을 할당 해제하면 재생이 중지되고 -play (-release을 전송하여 +alloc을 전송 함)을 보낸 직후 아무 것도 듣지 못합니다.

따라서 파일을 WAV로 변환하거나 재생할 때까지 AVAudioPlayer에 걸어주세요. :)

+0

정말 고마워요. – Oliver

관련 문제