在 iphone 应用程序中录制音频,带麦克风保存到文档目录

Posted

技术标签:

【中文标题】在 iphone 应用程序中录制音频,带麦克风保存到文档目录【英文标题】:recording audio in iphone app w/mike save to Documents Directory 【发布时间】:2012-03-07 22:07:22 【问题描述】:

我正在尝试在我的应用中使用麦克风录制音频,然后将其保存到文档目录

这是我的代码;

//I set up my button and recorder
-(IBAction)recordAudio:(id)sender;
    NSLog(@"recordAudio");
    recorder = nil;
    if ( isNotRecording)
       isNotRecording=NO;
        [record setTitle:@"Record Greeting" forState:UIControlStateNormal];

        [recorder setDelegate:self];
        [recorder prepareToRecord];
        [recorder record];
         
    else 
        isNotRecording = YES;
        [record setTitle:@"Stop Recording" forState:UIControlStateNormal];
        [recorder stop];
    

    AVAudiosession *session = [AVAudioSession sharedInstance];
    [session setCategory:AVAudioSessionCategoryRecord error:nil];
    //I get the path to the Documents Directory
    NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
    NSString *documentsDirectory = [documentPaths objectAtIndex:0];
//I name the file with the text that is entered into the textfield
    NSString *fullPath = [documentsDirectory stringByAppendingPathComponent:[[self imageNameTextField]text]];
// I add extension .m4a to the file so it will be recognized as a audio file
     fullPath = [fullPath stringByAppendingFormat:@".m4a"];
  //I establish the setting I want the audio to have 
    NSMutableDictionary *recordSettings = [[NSMutableDictionary alloc] initWithCapacity:10];

    if(recordEncoding==ENC_AAC)

        [recordSettings setObject:[NSNumber numberWithInt:kAudioFormatMPEG4AAC]forKey:AVFormatIDKey];
        [recordSettings setObject:[NSNumber numberWithFloat:44100.0] forKey: AVSampleRateKey];
        [recordSettings setObject:[NSNumber numberWithInt:2] forKey:AVNumberOfChannelsKey];
        [recordSettings setObject:[NSNumber numberWithInt:6400] forKey:AVEncoderBitRateKey];
        [recordSettings setObject:[NSNumber numberWithInt:16] forKey:AVLinearPCMBitDepthKey];
        [recordSettings setObject:[NSNumber numberWithInt: AVAudioQualityHigh] forKey: AVEncoderAudioQualityKey];
    
//I give the url path
     NSURL *url = [NSURL fileURLWithPath:fullPath];
    NSError *error = nil;
//I allocate memory for the recording with the setting at the path
    recorder = [[ AVAudioRecorder alloc] initWithURL:url settings:recordSettings error:&error];
       

不幸的是,这不起作用。有人可以看看它,看看我哪里出错了吗?

【问题讨论】:

【参考方案1】:
- (IBAction) startRecording:(id)sender 
    if (isRecording) 

        isAudioRecord = 1;

        recordSetting = [[NSMutableDictionary alloc] init];
        [recordSetting setValue :[NSNumber numberWithInt:kAudioFormatLinearPCM] forKey:AVFormatIDKey];
        [recordSetting setValue:[NSNumber numberWithFloat:44100.0] forKey:AVSampleRateKey];
        [recordSetting setValue:[NSNumber numberWithInt: 2] forKey:AVNumberOfChannelsKey];

        [recordSetting setValue :[NSNumber numberWithInt:16] forKey:AVLinearPCMBitDepthKey];
        [recordSetting setValue :[NSNumber numberWithBool:NO] forKey:AVLinearPCMIsBigEndianKey];
        [recordSetting setValue :[NSNumber numberWithBool:NO] forKey:AVLinearPCMIsFloatKey];

        recordedFile = [NSURL fileURLWithPath:[[self audioPath] stringByAppendingPathComponent:[NSString stringWithFormat:@"%@",[NSDate date]]]];

        self.mediaPath=[NSString stringWithFormat:@"%@",[NSDate date]];

        recorder=[[AVAudioRecorder alloc]init];
        recorder = [[ AVAudioRecorder alloc] initWithURL:recordedFile settings:recordSetting error:nil];

        [recorder setDelegate:self];
        [recorder prepareToRecord];
        [recorder record];
    
    else
        isRecording=YES;
        btnPlay.hidden=NO;
        currentTimeLabel.text=@"Not Recording";
        [recorder stop];
    
    // start recording

【讨论】:

以上是关于在 iphone 应用程序中录制音频,带麦克风保存到文档目录的主要内容,如果未能解决你的问题,请参考以下文章

在 iPhone 上录制音频并使用 NSOutputStream 通过网络发送

在 iOS 中使用耳机插孔引脚同时播放和录制音频?

如何以编程方式在 iphone 中执行应用程序音频的内部录制

仅从 iphone 扬声器(不包括麦克风)录制音频输出

如何录制iphone扬声器用完的音频?

同时录制音频和播放 iPod?