AVAudioRecorder无法保存到文件

时间:2014-01-23 07:35:58

标签: ios objective-c avaudiorecorder

我正在尝试创建一个简单的AVAudioRecorder包装器。 据我所知,在[recorder stop]之后,音频文件将自动保存到路径中。

然而,在我的课堂上,它不会挽救路径。

控制台日志:

2014-01-23 15:34:55.754 AudioRecorderDemo[4496:60b] contentURL: file:///var/mobile/Applications/C20F5A5A-14A9-45D4-80A3-999976AC4055/Documents/MyAudio-someDemo.m4a
2014-01-23 15:34:55.756 AudioRecorderDemo[4496:60b] fileExist? : no

感谢您的帮助。

GitHub上的类文件: AudioRecorder.h AudioRecorder.m

// Code when start:

-(void)prepare
{
    NSError *error;

    // Setup audio session
    AVAudioSession *session = [AVAudioSession sharedInstance];
    [session setCategory:AVAudioSessionCategoryPlayAndRecord error:&error];

    if (error)
    {
        NSLog(@"Error in AVAudioSession: %@", error.description);
        return;
    }

    // Define the recorder setting
    NSMutableDictionary *recordSetting = [[NSMutableDictionary alloc] init];

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

    // Initiate and prepare the recorder
    NSURL *contentURL = [STLAudioRecorder contentsOfURLFromUniqueID:self.uniqueID fileNameFormat:self.fileNameFormat];
    self.recorder = [[AVAudioRecorder alloc] initWithURL:contentURL settings:recordSetting error:&error];

    if (error)
    {
        NSLog(@"Error in AVAudioRecorder: %@", error.description);
        return;
    }

    self.recorder.delegate = self;
    self.recorder.meteringEnabled = YES;
    [self.recorder prepareToRecord];
}

-(void)startRecording
{
    if (!self.recorder) [self prepare];

    if (!self.recorder.isRecording)
    {
        AVAudioSession *session = [AVAudioSession sharedInstance];
        [session setActive:YES error:nil];
        [self.recorder record];
        NSLog(@"startRecording, url: %@", self.recorder.url.absoluteString);
    }
}

// Code when stop:

-(void)stopRecording
{
    if (self.recorder && self.recorder.isRecording)
    {
        NSError *error;
        [self.recorder stop];
        AVAudioSession *audioSession = [AVAudioSession sharedInstance];
        [audioSession setActive:NO error:&error];
        if (error) NSLog(@"Error in stopRecording: %@", error.description);
    }
    else
    {
        NSLog(@"stopRecording: !(self.recorder && self.recorder.isRecording)");
    }
}

1 个答案:

答案 0 :(得分:0)

试试这个,

-(void) initAudioSession {

AVAudioSession * audioSession = [AVAudioSession sharedInstance];

    [audioSession setCategory:AVAudioSessionCategoryPlayAndRecord error: &error];

    [audioSession setActive:YES error: &error];
}

-(void) recordStart {

NSArray *dirPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);

NSString *docsDir = [dirPaths objectAtIndex:0];

tempFilePath = [NSURL fileURLWithPath:[docsDir stringByAppendingPathComponent:[NSString stringWithFormat: @"%@.%@",audioFileName, @"caf"]]];


NSMutableDictionary* recordSetting = [[NSMutableDictionary alloc] init];
[recordSetting setValue :[NSNumber numberWithInt:kAudioFormatAppleIMA4] forKey:AVFormatIDKey];
[recordSetting setValue:[NSNumber numberWithFloat:44100.0] forKey:AVSampleRateKey]; 
[recordSetting setValue:[NSNumber numberWithInt: 2] forKey:AVNumberOfChannelsKey];
stringByAppendingPathComponent: [NSString stringWithFormat: @"%@.%@",audioFileName, @"caf"]]];


recorder = [[ AVAudioRecorder alloc] initWithURL:tempFilePath settings:recordSetting error:&error];
[recorder setDelegate:self];

[recorder prepareToRecord];

[recorder record];
}

-(void) stopRecording{
[recorder stop];
}

-(void) playRecord{
avPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:tempFilePath error:&error];
[avPlayer prepareToPlay];
[avPlayer play];
}