AVAudioRecord prepareRecord&记录返回NO

时间:2013-07-29 02:57:02

标签: ios avaudiorecorder

我目前正在开发一个支持录音的游戏项目。该程序基于WiEngine,一种类似于cocos2d-x的游戏引擎。

这就是事情,我尝试了很多方法,但每次调用[myRecorder Record]时它总是返回NO。然后我明确地调用了PrepareRecord,它也返回No。

这是我如何启动记录器的代码

- (void)initRecorder
{
    if(self.myRecoder!=nil)
    {
        [self.myRecoder release];
        self.myRecoder=nil;
    }


    NSDictionary *myRecorderParam = [[NSDictionary alloc]initWithObjectsAndKeys:
                                     [NSNumber numberWithFloat:44100.0],AVSampleRateKey,
                                     [NSNumber numberWithInt:kAudioFormatLinearPCM],AVFormatIDKey,
                                     [NSNumber numberWithInt:16],AVLinearPCMBitDepthKey,
                                     [NSNumber numberWithInt:1],AVNumberOfChannelsKey,
                                     [NSNumber numberWithInt:AVAudioQualityMedium],AVEncoderAudioQualityKey,
                                     nil];

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

    NSString *DocumentPath = [pathArray objectAtIndex:0];
    DocumentPath = [DocumentPath stringByAppendingString:@"/luyin.wav"];

    self.myRecoder = [[AVAudioRecorder alloc]initWithURL:[NSURL URLWithString:DocumentPath]
                                                settings:myRecorderParam
                                                   error:nil];


    [self.myRecoder setDelegate:self];

}

我打电话给这样的记录

- (void)beginRecord
{
    NSLog(@"begin record");

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

    if(self.myRecoder==nil)
    {
        NSLog(@"recoder null");
    }

    BOOL resultPre = [[self myRecoder] prepareToRecord];
    if(resultPre)
    {
        NSLog(@" record pre yes ");
    }
    else
    {
        NSLog(@" record pre no ");
    }

    BOOL result = [[self myRecoder] record];
    if(result)
    {
        NSLog(@" record yes ");
    }
    else
    {
        NSLog(@" record no ");
    }
}

根据NSLogs,我确信初始化似乎没有失败。

我也尝试初始化一个AudioSession,但它不起作用

请帮助

1 个答案:

答案 0 :(得分:0)

它是AVAudioRecorder吗?

AVAudioPlayer用于播放录音

像这样:

NSMutableDictionary * recordSetting = [[NSMutableDictionary alloc] init];

[recordSetting setValue:[NSNumber numberWithInt:kAudioFormatAppleIMA4] forKey:AVFormatIDKey];

[recordSetting setValue:[NSNumber numberWithFloat:[freq.text floatValue]] forKey:AVSampleRateKey];
[recordSetting setValue:[NSNumber numberWithInt:[value.text intValue]] forKey:AVNumberOfChannelsKey];

recordedTmpFile = [NSURL fileURLWithPath:[NSTemporaryDirectory()stringByAppendingPathComponent:[NSString stringWithFormat:@“%。0f。%@”,[NSDate timeIntervalSinceReferenceDate] * 1000.0,@“caf”]]];
NSLog(@“使用文件调用:%@”,recordedTmpFile);
recorder = [[AVAudioRecorder alloc] initWithURL:recordedTmpFile settings:recordSetting error:& error];