首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >无法录制

无法录制
EN

Stack Overflow用户
提问于 2010-11-25 00:52:06
回答 1查看 871关注 0票数 1

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

代码语言:javascript
复制
-(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”。

当我调用"stop“方法时,它打印的是"Not recoring...”消息。

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

谢谢。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2010-11-25 01:40:22

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

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

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

编辑:找到真正的问题所在。在创建文件之前,您需要创建目录。替换此行:

代码语言:javascript
复制
[[NSFileManager defaultManager] createFileAtPath:[NSString stringWithFormat:@"%@/Test.caf", soundsDirectoryPath] contents:nil attributes:nil];

使用这一条:

代码语言:javascript
复制
[[NSFileManager defaultManager] createDirectoryAtPath:soundsDirectoryPath withIntermediateDirectories:YES attributes:nil error:NULL];

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

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/4269335

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档