与麦克风通话时录制声音

时间:2012-08-06 08:35:52

标签: objective-c ios ios5

我想编写处理录音的控制器。它应该在麦克风有声时录制声音。

按下按钮时,我有录制声音的工作代码。

-(IBAction) startRecording
{
    NSLog(@"startRecording");
    audioRecorder = nil;

    // Init audio with record capability
    AVAudioSession *audioSession = [AVAudioSession sharedInstance];
    [audioSession setCategory:AVAudioSessionCategoryRecord error:nil];

    NSMutableDictionary *recordSettings = [[NSMutableDictionary alloc] initWithCapacity:10];
   // if(recordEncoding == ENC_PCM)
    //{

        [recordSettings setObject:[NSNumber numberWithInt: kAudioFormatLinearPCM] forKey: AVFormatIDKey];
        [recordSettings setObject:[NSNumber numberWithFloat:500.0] forKey: AVSampleRateKey];//44100.0
        [recordSettings setObject:[NSNumber numberWithInt:2] forKey:AVNumberOfChannelsKey];
        [recordSettings setObject:[NSNumber numberWithInt:16] forKey:AVLinearPCMBitDepthKey];
        [recordSettings setObject:[NSNumber numberWithBool:NO] forKey:AVLinearPCMIsBigEndianKey];
        [recordSettings setObject:[NSNumber numberWithBool:NO] forKey:AVLinearPCMIsFloatKey];

   // NSURL *url = [NSURL fileURLWithPath:[NSString stringWithFormat:@"%@/recordTest.caf", [[NSBundle mainBundle] resourcePath]]];

    NSURL *url = [NSURL URLWithString:[self copyFile:@"recordTest.caf"]];



    NSError *error = nil;
    audioRecorder = [[ AVAudioRecorder alloc] initWithURL:url settings:recordSettings error:&error];
    [audioRecorder setDelegate:self];

    if ([audioRecorder prepareToRecord] == YES){
        [audioRecorder record];
        NSLog(@"recording");
    }else {
        int errorCode = CFSwapInt32HostToBig ([error code]);
        NSLog(@"Error: %@ [%4.4s])" , [error localizedDescription], (char*)&errorCode);

    }

}

因此可以录制或监听来自麦克风输入的声音,并在声音高于1000 Hz时开始录音吗?

我也很欣赏有关这方面的文献。

感谢。

1 个答案:

答案 0 :(得分:0)

希望它会帮助你Download the link l