I’m currently developing an app which supports in-app audio recording. The user can get a standard tableview of locally saved audio files he/she have already recorded through the app, or press a button to go to a new recording view, from where it is possible to record a new audio session, which will automatically get saved to the apps sandbox. Now this function works well most of the time. I can record new files and play them from the app, but at random times the audio recorder will fail to start a recording in this method:
-(void) startRecording
{
if (!isRecording)
{
isRecording = YES;
BOOL recordingSucess = [audioRecorder record];
if (recordingSucess)
NSLog(@"WE STARTED THE RECORDING!");
else
NSLog(@"WE DID NOT START THE RECORDING!");
// Start the timer
recordTiming = [NSDate date];
timeCheck = [NSTimer scheduledTimerWithTimeInterval:1.0f target:self selector:@selector(timeCheck) userInfo:nil repeats:YES]
}
That is, the NSLog will print out “We did not start the recording”, indicating that it failed to start the recording. This results in an empty audio file still being saved to the app with a constant file size of exactly 4096 bytes. This is how the audio recorder object is initialized:
-(id) initWithContentURL:(NSURL *)contentURL
{
self = [super init];
if (self)
{
NSLog(@"Initializing audio recorder!");
// Specific settings for the audio recording
NSDictionary *recordSettings = [NSDictionary
dictionaryWithObjectsAndKeys:
[NSNumber numberWithInt:AVAudioQualityMin],
AVEncoderAudioQualityKey,
[NSNumber numberWithInt:8],
AVEncoderBitRateKey,
[NSNumber numberWithInt: 2],
AVNumberOfChannelsKey,
[NSNumber numberWithFloat:22000.0],
AVSampleRateKey,
nil];
NSError *error = nil;
audioRecorder = [[AVAudioRecorder alloc] initWithURL:contentURL settings:recordSettings error:&error];
audioRecorder.delegate = self;
}
There is also NSLog checks which checks to see if the error is nil or not, so at this point I’m sure that the initialization does not seem to fail. What exactly could cause the record method to fail at seemingly random times, while it works perfectly good at most other times?
Before the recording is started an audio session should be initialized:
AVAudioSession *audioSession = [AVAudioSession sharedInstance];
NSError *err = nil;
[audioSession setCategory :AVAudioSessionCategoryPlayAndRecord error:&err];
if(err){
NSLog(@"audioSession: %@ %d %@", [err domain], [err code], [[err userInfo] description]);
return;
}
err = nil;
[audioSession setActive:YES error:&err];
if(err){
NSLog(@"audioSession: %@ %d %@", [err domain], [err code], [[err userInfo] description]);
return;
}
See the accepted answer for reference.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With