2009-10-30 4 views
3

사용자 오디오 입력 (음성)을 녹음 할 기본 컨트롤러를 설정하려고합니다. 그러나 AVAudioRecorder의 prepareToRecord 메서드가 실패하고 그 이유를 알 수 없습니다. 내 애플 대리자에서 설정을 오디오 세션을 가지고 있고이 AVAudioRecorder 인스턴스를 인스턴스화 할 때 나는 오류가 발생하지 않습니다AVAudioRecorder prepareToRecord가 실패하는 이유는 무엇입니까?

// 앱 위임 조각

AVAudioSession* audioSession = [AVAudioSession sharedInstance]; 
NSError* audioSessionError = nil; 

[audioSession setCategory: AVAudioSessionCategoryPlayAndRecord 
        error: &audioSessionError]; 

    if (audioSessionError) { 
    NSLog (@"Error setting audio category: %@", [audioSessionError localizedDescription]); 
} else { 
    NSLog(@"No session errors for setting category"); 
} 

[audioSession setActive:YES error:&audioSessionError]; 

if (audioSessionError) { 
    NSLog (@"Error activating audio session: %@", [audioSessionError localizedDescription]); 
} else { 
NSLog(@"no session errors for setActive"); 
} 

// VIEW는

을 RECORDERCONTROLLER IN LOAD를 DID
- (void)viewDidLoad { 

self.navigationItem.title = [NSString stringWithFormat:@"%@", [[MyAppDelegate loadApplicationPlist] valueForKey:@"recorderViewTitle"]]; 

self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone 
                        target:self 
                        action:@selector(dismiss)]; 

[self alertIfNoAudioInput]; 

[self createAVAudioRecorder]; 

minutesSecondsFormatter = [[SimpleMinutesSecondsFormatter alloc] init]; 
currentTimeUpdateTimer = [NSTimer scheduledTimerWithTimeInterval:0.1 
                 target:self selector:@selector(updateAudioDisplay) 
                 userInfo:NULL repeats:YES]; 

[super viewDidLoad]; 
} 

// AVAUDIORECORDER

- (NSError *)createAVAudioRecorder { 

NSError *recorderSetupError = nil; 

[audioRecorder release]; 
audioRecorder = nil; 

NSString *timestamp = [NSString stringWithFormat:@"%d", (long)[[NSDate date] timeIntervalSince1970]]; 

NSString *destinationString = [[MyAppDelegate getAppDocumentsDirectory] stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.caf", timestamp]]; 
NSLog(@"destinationString: %@", destinationString); 
NSURL *destinationUrl  = [NSURL fileURLWithPath: destinationString]; 

audioRecorder = [[AVAudioRecorder alloc] initWithURL:destinationUrl 
              settings:[[AVRecordSettings sharedInstance] getSettings] 
              error:&recorderSetupError]; 

if (recorderSetupError) { 

    UIAlertView *cantRecordAlert = 
    [[UIAlertView alloc] initWithTitle:@"Can't record" 
          message:[recorderSetupError localizedDescription] 
          delegate:nil 
       cancelButtonTitle:@"OK" 
       otherButtonTitles:nil]; 
    [cantRecordAlert show]; 
    [cantRecordAlert release]; 
    return recorderSetupError; 
} else { 
    NSLog(@"no av setup error"); 
} 

if ([audioRecorder prepareToRecord]) { 
    recordPauseButton.enabled = YES; 
    audioRecorder.delegate = self; 
} else { 
    NSLog(@"couldn't prepare to record"); 
} 

NSLog (@"recorderSetupError: %@", recorderSetupError); 

return recorderSetupError; 
} 

답변

5

그것은 당신 때문에 실패를 CREATE 적절한 설정을 사용하여 AVAudioRecorder 개체를 초기화하지 않았습니다. 를 초기화하기 전에이 작업을 수행 : 파일을 저장하려고 디렉토리하지 않는 경우

NSDictionary *recordSettings = 
    [[NSDictionary alloc] initWithObjectsAndKeys: 
    [NSNumber numberWithFloat: 44100.0],     AVSampleRateKey, 
    [NSNumber numberWithInt: kAudioFormatAppleLossless], AVFormatIDKey, 
    [NSNumber numberWithInt: 1],       AVNumberOfChannelsKey, 
    [NSNumber numberWithInt: AVAudioQualityMax],   AVEncoderAudioQualityKey, 
    nil]; 

는 당신은 (에러없이 자동)

audioRecorder = [[AVAudioRecorder alloc] initWithURL:destinationUrl 
              settings:recordSettings 
              error:&recorderSetupError]; 
+0

그것은 확실히 내 설정을해야했다. http://blog.corywiles.com/avaudiorecorder-preparetorecord-silent-error –

21

prepareToRecord는 실패 사용하여 인스턴스화 할 수 있다. NSFileManager를 사용하여 디렉토리가 이미 있는지 확인하십시오.

+1

(슬래시 잊었) 나는 디렉토리를 만들어 내 파일 관리자에서 엉망 +1 충분히 있는지,이 문제였다! – RileyE

관련 문제