1

我为 iPhone 创建了一个测试应用程序。我写了以下代码来记录。但它不起作用。


-(IBAction)start:(id)sender{
    recordSetting = [[NSMutableDictionary alloc] init];
    [recordSetting setValue:[NSNumber numberWithInt:kAudioFormatLinearPCM] forKey:AVFormatIDKey];
    [recordSetting setValue:[NSNumber numberWithFloat:44100.0] forKey:AVSampleRateKey];
    [recordSetting setValue:[NSNumber numberWithInt:16] forKey:AVLinearPCMBitDepthKey];
    [recordSetting setValue:[NSNumber numberWithBool:NO] forKey:AVLinearPCMIsBigEndianKey];
    [recordSetting setValue:[NSNumber numberWithBool:NO] forKey:AVLinearPCMIsFloatKey];

    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    soundsDirectoryPath = [documentsDirectory stringByAppendingPathComponent:@"Sounds"];
    [[NSFileManager defaultManager] createFileAtPath:[NSString stringWithFormat:@"%@/Test.caf", soundsDirectoryPath] contents:nil attributes:nil];

    NSURL *url = [NSURL fileURLWithPath:[NSString stringWithFormat:@"%@/Test.caf", soundsDirectoryPath]];
    NSError *err = nil;
    if([recorder isRecording])
    {
        [recorder stop];
        recorder = nil;
    }
    recorder = [[ AVAudioRecorder alloc] initWithURL:url settings:recordSetting error:&err];
    [recorder setDelegate:self];
    recorder.meteringEnabled = YES;
    if(err){
        NSLog(@"Error : %@", [err description]);
    }
    AVAudioSession *audioSession = [AVAudioSession sharedInstance];
    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];
        [cantRecordAlert release]; 
        return;
    }
    BOOL st = [recorder prepareToRecord];
    if(!st){
        NSLog(@"Failed");
    }
    recorder.meteringEnabled = YES;
    BOOL status = [recorder record];
    if(!status)
    NSLog(@"Failed");
}

-(IBAction)stop:(id)sender{
    if([recorder isRecording]){
        NSLog(@"Recording...");
    }else{
        NSLog(@"Not recording...");
    }
    [recorder stop];
}

当我调用“start”方法时,在调用“prepareToRecord”和“record”方法后打印“Failed”。

当我调用“停止”方法时,它正在打印“未录制...”消息。

我在这段代码中犯了什么错误。

谢谢。

4

1 回答 1

2

根据您在问题评论中提供的额外信息,创建与记录器关联的文件时似乎存在问题。这NO由方法返回的值指示prepareToRecord

需要注意的一点是,它使用AVAudioRecorder 方法中prepareToRecord提供的 URL 自行创建录音文件。init不需要像使用 NSFileManager 那样显式创建它。

尝试记录soundsDirectoryPath,并确保它是有效的文件路径。

编辑:发现真正的问题。您需要在创建文件之前创建目录。替换这一行:

[[NSFileManager defaultManager] createFileAtPath:[NSString stringWithFormat:@"%@/Test.caf", soundsDirectoryPath] contents:nil attributes:nil];

有了这个:

[[NSFileManager defaultManager] createDirectoryAtPath:soundsDirectoryPath withIntermediateDirectories:YES attributes:nil error:NULL];

新行只创建目录而不是文件,但这很好,因为该prepareToRecord方法会自动创建文件。

于 2010-11-24T17:40:22.433 回答