无法记录

无法记录

问题描述:

我为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]; 
} 

当我打电话“开始”的方法,它是印刷“失败”呼吁“prepareToRecord”和“记录”的方法之后。

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

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

谢谢。

+0

也没有得到任何错误; – jfalexvijay 2010-11-24 16:57:21

+0

你有开始按钮吗?您还应该在开始IBAction中记录日志,以确保它甚至可以输入它。 – DerekH 2010-11-24 17:06:11

根据您在问题评论中提供的额外信息,看起来在创建与录像机相关的文件时出现问题。这由prepareToRecord方法返回的NO值表示。

有一点要注意的是,prepareToRecord使用AVAudioRecorder的init方法中提供的URL自行创建录制文件。不需要显式创建它,就像使用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方法自动创建该文件。