无法保存录制的音频iOS?

时间:2014-01-22 05:44:05

标签: objective-c ios7 avfoundation nsdocumentdirectory

我想将录制的音频保存在本地文档目录中   之后,我想播放已保存的音频文件

Audiofile 未在文档目录中创建?

我尝试使用以下代码我做错了什么?

NSString *temp1=[NSString stringWithFormat:@"%@.m4a",temp];
NSLog(@"=====>>%@",temp1);
NSArray *pathComponents=[NSArray arrayWithObjects:[NSSearchPathForDirectoriesInDomains(NSDocumentationDirectory, NSUserDomainMask, YES)lastObject],temp1, nil];
NSURL *outputFileURL=[NSURL fileURLWithPathComponents:pathComponents];

if (isRecording==NO) {
    isRecording=YES;
    NSLog(@"recording started");
 session = [AVAudioSession sharedInstance];
[session setCategory:AVAudioSessionCategoryPlayAndRecord error:nil];

// 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
recorder = [[AVAudioRecorder alloc] initWithURL:outputFileURL settings:recordSetting error:NULL];
recorder.delegate = self;
recorder.meteringEnabled = YES;
[recorder prepareToRecord];

[session setActive:YES error:nil];

// Start recording
[recorder record];

}
else
{
    NSLog(@"recording stopped");
    [recorder stop];
    NSData *audioData=[NSData dataWithContentsOfURL:outputFileURL];
    [audioData writeToURL:outputFileURL atomically:YES];

    player = [[AVAudioPlayer alloc] initWithContentsOfURL:outputFileURL error:nil];
    [player prepareToPlay];
    [player play];
    [player setVolume:1.0];
    isRecording=NO;

}

2 个答案:

答案 0 :(得分:0)

试试此代码

-(IBAction)btnS1AudioPressed:(id)sender
{
    audioSession = [AVAudioSession sharedInstance];
    NSError *err = nil;
    [audioSession setCategory :AVAudioSessionCategoryPlayAndRecord error:&err];

    if(err)
    {
        NSLog(@"audioSession: %@ %d %@", [err domain], [err code], [[err userInfo] description]);
        return;
     }
    [audioSession setActive:YES error:&err];
    err = nil;
    if(err)
    {
        NSLog(@"audioSession: %@ %d %@", [err domain], [err code], [[err userInfo] description]);
        return;
    }

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

    NSString *AudioName=[NSString stringWithFormat:@"TestAudio.caf"];
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    audioPath = [documentsDirectory stringByAppendingPathComponent:AudioName];
    NSURL *url = [NSURL fileURLWithPath:audioPath];
    err = nil;
    NSData *audioData = [NSData dataWithContentsOfFile:[url path] options: 0 error:&err];
    if(audioData)
    {
        NSFileManager *fm = [NSFileManager defaultManager];
        [fm removeItemAtPath:[url path] error:&err];
    }
    recorder = [[ AVAudioRecorder alloc] initWithURL:url settings:recordSetting error:&err];
    if(!recorder)
    {
        NSLog(@"recorder: %@ %d %@", [err domain], [err code], [[err userInfo] description]);
        UIAlertView *alert =
        [[UIAlertView alloc] initWithTitle: @"Warning"
                                   message: [err localizedDescription]
                                  delegate: nil
                         cancelButtonTitle:@"OK"
                         otherButtonTitles:nil];
        [alert show];
        return;
    }
    [recorder setDelegate:self];
    [recorder prepareToRecord];
    recorder.meteringEnabled = YES;

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

- (IBAction) stopRecording:(id)sender
{
    [recorder stop];
}

答案 1 :(得分:0)

我发现了我犯的错误

NSArray *pathComponents=[NSArray arrayWithObjects:[NSSearchPathForDirectoriesInDomains(NSDocumentationDirectory, NSUserDomainMask, YES)lastObject],temp1, nil]; 

在上面的代码中,我使用NSDocumentationDirectory代替NSDocumentsDirectory