2009-10-30 7 views
3

J'essaye de configurer un contrôleur de base qui enregistrera l'entrée audio d'utilisateur (voix). Cependant, la méthode prepareToRecord de AVAudioRecorder échoue et je n'arrive pas à comprendre pourquoi. J'ai installé la session audio dans mon délégué app et je ne reçois pas une erreur quand j'instancier l'instance AVAudioRecorder:Pourquoi AVAudioRecorder prepareToRecord échoue?

// extrait délégué App

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"); 
} 

// VUE SAVIEZ CHARGE DANS RECORDERCONTROLLER

- (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]; 
} 

// CREATE 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; 
} 

Répondre

5

Il échoue parce que vous n'a pas initialisé l'objet AVAudioRecorder en utilisant les paramètres appropriés. Pour ce faire, avant de l'initialiser:

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

alors vous pouvez instancier en utilisant

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

Cela a certainement à voir avec mes paramètres. http://blog.corywiles.com/avaudiorecorder-preparetorecord-silent-error –

21

Le prepareToRecord échoue également (en silence, sans erreur) si le répertoire dans lequel vous essayez d'enregistrer le fichier ne exister. Utilisez NSFileManager pour vérifier si le répertoire existe déjà.

+1

+1 J'ai foiré dans mon gestionnaire de fichiers en créant le répertoire (j'ai oublié le slash) et bien sûr, c'était le problème! – RileyE