iPhone上即时语音播放的最佳API?

时间:2011-05-17 07:56:32

标签: iphone voice audio voice-recording

我想做什么:

  • iPhone继续监控语音输入频道
  • 一旦用户说话(录制音量>一定级别)
  • iPhone开始录制
  • 一旦使用停止说话(录音静音一秒钟)
  • iPhone播放录音

我应该研究哪些iPhone API或示例代码?谢谢!

1 个答案:

答案 0 :(得分:0)

您需要的是AVFoundation。使用它:

  1. 将AVFoundation.framework添加到您的项目
  2. 使您的视图控制器符合AVAudioRecorderDelegate,AVAudioPlayerDelegate,一个用于录制,另一个用于播放
  3. 以下代码段用于录制:

    AVAudioSession *audioSession = [AVAudioSession sharedInstance];
    NSError *err = nil;
    [audioSession setCategory :AVAudioSessionCategoryPlayAndRecord error:&err];
    [audioSession setActive:YES error:&err];
    NSMutableDictionary *settings = [[NSMutableDictionary alloc] init];
    [settings setValue: [NSNumber numberWithInt:kAudioFormatMPEGLayer3] forKey:AVFormatIDKey];
    [settings setValue: [NSNumber numberWithFloat:44100.0] forKey:AVSampleRateKey]; 
    [settings setValue: [NSNumber numberWithInt: 2] forKey:AVNumberOfChannelsKey];
    [settings setValue: [NSNumber numberWithInt:16] forKey:AVLinearPCMBitDepthKey];
    [settings setValue: [NSNumber numberWithBool:NO] forKey:AVLinearPCMIsBigEndianKey];
    [settings setValue: [NSNumber numberWithBool:NO] forKey:AVLinearPCMIsFloatKey];
    NSURL *url = [NSURL fileURLWithPath: filepath];
    recorder = [[ AVAudioRecorder alloc] initWithURL:url settings:settings error:&err];
    [settings release];
    [recorder setDelegate:self];
    [recorder prepareToRecord];
    [recorder record];
    

    另外,你需要实现 - (void)audioRecorderDidFinishRecording :( AVAudioRecorder *)录音机成功:( BOOL)标志释放录音机。

    请在此处查看更多详情:http://developer.apple.com/library/ios/#documentation/AudioVideo/Conceptual/AVFoundationPG/Articles/00_Introduction.html#//apple_ref/doc/uid/TP40010188