IOS - Record audio with AVAudioRecorder error without errors

I am having a problem with AVAudioRecorder not working for me, at least from what I can see (or not hear).

I am targeting iOS 5 with ARC.

I have set error objects but none of them fired, so I think I am doing something wrong here.

Here is the part I was setting up the AVAudioRecorder:

NSArray *dirPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *docsDir = [dirPaths objectAtIndex:0];
NSString *soundFilePath = [docsDir stringByAppendingPathComponent:currentTrack.trackFileName];

currentTrack.trackFileURL = [NSURL fileURLWithPath:soundFilePath];

NSLog(@"Chemin : %@", currentTrack.trackFileURL.path);

NSMutableDictionary *recordSetting = [[NSMutableDictionary alloc] init];

[recordSetting setValue :[NSNumber numberWithInt:kAudioFormatLinearPCM] forKey:AVFormatIDKey];
[recordSetting setValue:[NSNumber numberWithFloat:44100.0] forKey:AVSampleRateKey]; 
[recordSetting setValue:[NSNumber numberWithInt: 2] forKey:AVNumberOfChannelsKey];

[recordSetting setValue :[NSNumber numberWithInt:16] forKey:AVLinearPCMBitDepthKey];
[recordSetting setValue :[NSNumber numberWithBool:NO] forKey:AVLinearPCMIsBigEndianKey];
[recordSetting setValue :[NSNumber numberWithBool:NO] forKey:AVLinearPCMIsFloatKey];


currentTrack.trackRecordSettings = recordSetting;


NSError *err = nil;


//[audioSession setDelegate:self];
if(err){
    NSLog(@"audioSession: %@ %d %@", [err domain], [err code], [[err userInfo] description]);
    return;
}
[[AVAudioSession sharedInstance] setActive:YES error:&err];
err = nil;
if(err){
    NSLog(@"audioSession: %@ %d %@", [err domain], [err code], [[err userInfo] description]);
    return;
}
err = nil;
recorder = [[AVAudioRecorder alloc]
                 initWithURL:currentTrack.trackFileURL
                 settings:currentTrack.trackRecordSettings
                 error:&error];

if (err)
{
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle: @"Warning"
                               message: [err localizedDescription]
                              delegate: nil
                     cancelButtonTitle:@"OK"
                     otherButtonTitles:nil];
    [alert show];
    NSLog(@"error: %@", [error localizedDescription]);

} else {
    [recorder setDelegate:self];
    [recorder prepareToRecord];

    BOOL audioHWAvailable = [[AVAudioSession sharedInstance]inputIsAvailable ];
    if (!audioHWAvailable) {
        UIAlertView *cantRecordAlert =
        [[UIAlertView alloc] initWithTitle: @"Warning"
                                   message: @"Audio input hardware not available"
                                  delegate: nil
                         cancelButtonTitle:@"OK"
                         otherButtonTitles:nil];
        [cantRecordAlert show]; 
        return;
    }
}

      

The error was not here.

Then when it's time to start recording:

-(void)startRecordingAudio{
    NSLog(@"Start recording");
    [[AVAudioSession sharedInstance] setCategory :AVAudioSessionCategoryRecord error:nil];
    [recorder record];
}

      

Then when it's time to stop recording:

-(void)stopRecordingAudio{
    NSLog(@"Stop recording");
    [recorder stop];
    NSError *err;
    NSData *audioData = [NSData dataWithContentsOfFile:currentTrack.trackFileURL.path options: 0 error:&err];
    if(!audioData)
        NSLog(@"audio data error: %@ %d %@", [err domain], [err code], [[err userInfo] description]);
    NSLog(@"%d", audioData.length);
    }

      

Is it normal that the audio data length is always 4096? If I understand correctly, this is about 93ms of sound ...

Finally, when it's time to play the recorded audio:

-(void)startPlayingAudio{
    [[AVAudioSession sharedInstance] setCategory :AVAudioSessionCategoryPlayback error:nil];

    NSLog(@"Start playing");
    NSError *error;
    if(player == nil){
        player = [[AVAudioPlayer alloc] initWithContentsOfURL:currentTrack.trackFileURL    
        error:&error];                                                                           
    }


    player.delegate = self;

    if (error)
        NSLog(@"Error: %@", 
              [error localizedDescription]);
    else
        [player  play];
}

      

I have set up delegation methods that never run:

-(void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)flag
{
   NSLog (@"audioRecorderDidFinishRecording:successfully:");
}
-(void)audioPlayerDecodeErrorDidOccur:(AVAudioPlayer *)player error:(NSError *)error
{
    NSLog(@"Decode Error occurred");
}
-(void)audioRecorderDidFinishRecording:(AVAudioRecorder *)recorder successfully:    (BOOL)flag
{
    NSLog (@"audioRecorderDidFinishRecording:successfully: %d", flag);
}
-(void)audioRecorderEncodeErrorDidOccur:(AVAudioRecorder *)recorder error:(NSError *)error
{
    NSLog(@"Encode Error occurred");
}
-(void)beginInterruption{
    NSLog(@"INTERRUPT");
}

      

Also, I saw that the .caf file was created correctly in the application document folder.

Thanks for any help you can bring. At the moment, I cannot hear any audio recorded and I am testing an iPhone 4 device without headphones.

+3


source to share


1 answer


Problem encountered: A record was called before the Record was prepared. Since I used viewDidAppear to set up the recording, the call to start recording was made before the view was visible ...

I used viewDidAppear because viewDidLoad is not being called since I am not using initWithNib. I tried to override loadView, but all the examples I found are not so clear about the object and loadView is never called. Maybe someone can point me in the right direction? However, I can hear the voice on the iphone!



Thank you for your time.

0


source







All Articles