简体   繁体   中英

iphone: AVAudioRecorder recording always return FALSE

I am using AVAudioRecorder to record sound, but I have a problem.

After calling the method [recorder record], the value of recorder.recording always returns FALSE, so that I can not stop the recording.

I am using XCode 4.2 with iOS 5, my iPad is ipad 2 on iOS 5 also.

This is my code:

    self.currentFileName = [GlobalRecording newRecordingFileName];
    NSURL *url = [NSURL fileURLWithPath:[[GlobalRecording recordingDirectory]    stringByAppendingPathComponent:currentFileName]];
NSError *err = nil;

AVAudioRecorder *aRecorder = [[AVAudioRecorder alloc] initWithURL:url settings:recordingSettings error:&err];
if( err ){
    NSLog(@"could not create a recorder: %@ %d %@", [err domain], [err code], [[err userInfo] description]);
    return;
}

self.startRecordingTime = [NSDate date];
recordingLength = 0.0f;

//  setup recorder and start recording
[aRecorder setDelegate:self];
aRecorder.meteringEnabled = YES;
[aRecorder record];

self.recorder = aRecorder;
BOOL test = recorder.recording;
[aRecorder release];

try using AVAudioSession after [audioRecord prepareToRecord]

    NSError *err = nil;
audioSession = [AVAudioSession sharedInstance];
[audioSession setCategory :AVAudioSessionCategoryPlayAndRecord error:&err];

// route to speaker
UInt32 ASRoute = kAudioSessionOverrideAudioRoute_Speaker;
AudioSessionSetProperty(kAudioSessionProperty_OverrideAudioRoute, sizeof(ASRoute), &ASRoute);

if (err)
{
    NSLog(@"audioSession: %@ %d %@", [err domain], [err code], [[err userInfo] description]);
}

[audioSession setActive:YES error:&err];

An audio session is the intermediary between your app and iOS used to configure your app's audio behavior. Upon launch, your app automatically gets a singleton audio session.

so activate your session before start recording

For Swift 3.0

let session = AVAudioSession.sharedInstance()
        do {
            try session.setCategory(AVAudioSessionCategoryPlayAndRecord)
        } catch let error as NSError {
            print("could not set session category")
            print(error.localizedDescription)
        }
        do {
            try session.setActive(true)
        } catch let error as NSError
        {
            print("could not make session active")
            print(error.localizedDescription)
        }
self.recorder.record() // recorder is my AVAudioRecorder

I had a similar symptom, because I was using a library to playback that was resetting AVAudioSession global settings. I needed to reset the settings each time I wanted to record.

do {
  try session.setCategory(AVAudioSessionCategoryPlayAndRecord)
  try session.setActive(true)
} catch {
  return error
}

The library I'm using resets it to AVAudioSessionCategoryPlay . Sure would be nice if there was an error to access other than false to let you know what caused the failure of record() .

I encountered the same problem on Simulator, and after investigation I found that it was because my computer didn't set up any audio input sources. Once I connected an input source (eg, AirPod) and record() finally returned true and started to work. Sadly, record() never gives any error to indicate this type of failure.

Are you also seeing a delay when you invoke the record method? I'm seeing an issue when using iOS5 on iPhone4 hardware where the record method has a 3-4 second delay and occasionally will fail to start recording. I'm calling prepareToRecord as recommended with the same issue.

This is not an issue on iOS4/iPhone4 or iOS5/iPhone4S...so it seems to be an issue running the new OS on older hardware.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM