iPhone SDK:记录 iPhone 发出的声音

Posted

技术标签:

【中文标题】iPhone SDK:记录 iPhone 发出的声音【英文标题】:IPhone SDK: recording the sounds the iPhone's making 【发布时间】:2011-05-11 19:01:52 【问题描述】:

我正在开发一个鼓应用程序

在许多其他应用程序中,我看到一个录音机习惯于录制鼓声并稍后对其进行采样。

我怎样才能制作和/或使用这样的录音机?

注意:我正在使用 Audioservices 播放声音。

【问题讨论】:

您要录制设备音频吗? 【参考方案1】:

您可能想阅读AVFoundation;您可以使用它来录制音频和视频。

【讨论】:

【参考方案2】:

AVAudioRecorder 在iPhone for Programmers: An App-Driven Approach 一书中提到,示例VoiceRecorder 中有源代码。

http://www.deitel.com/bookresources/iPhoneFP/UpdatedExamples.zip

- (IBAction)record:sender

   // if we’re currently recording
   if (recorder.recording)
   
      [timer invalidate]; // stop the timer from generating events
      timer = nil; // set time to nil
      [recorder stop]; // stop recording

      // set the category of the current audio session
      [[AVAudioSession sharedInstance] setCategory:
         AVAudioSessionCategorySoloAmbient error:nil];

      // load the record image
      UIImage *recordImage = [UIImage imageNamed:@"record.png"];

      // set the image on the record button
      [recordButton setImage:recordImage forState:UIControlStateNormal];

      // create a new NameRecordingViewController
      NameRecordingViewController *controller = 
      [[NameRecordingViewController alloc] init];
      controller.delegate = self; // set controller's delegate to self

      // show the NameRecordingViewController
      [self presentModalViewController:controller animated:YES];
    // end if
   else
   
      // set the audio session's category to record
      [[AVAudioSession sharedInstance] setCategory:
       AVAudioSessionCategoryRecord error:nil];  

      // find the location of the document directory
      NSArray *paths = NSSearchPathForDirectoriesInDomains(
         NSDocumentDirectory, NSUserDomainMask, YES);

      // get the first directory
      NSString *dir = [paths objectAtIndex:0];

      // create a name for the file using the current system time
      NSString *filename = [NSString stringWithFormat:@"%f.caf", 
                            [[NSDate date] timeIntervalSince1970]];

      // create the path using the directory and file name
      NSString *path = [dir stringByAppendingPathComponent:filename];

      // create a new NSMutableDictionary for the record settings        
      NSMutableDictionary *settings = [[NSMutableDictionary alloc] init];

      // record using the Apple lossless format
      [settings setValue: [NSNumber
         numberWithInt:kAudioFormatAppleLossless] forKey:AVFormatIDKey];

      // set the sample rate to 44100 Hz
      [settings setValue:[NSNumber 
                          numberWithFloat:44100.0] forKey:AVSampleRateKey]; 

      // set the number of channels for recording
      [settings setValue:[NSNumber numberWithInt:1] 
                  forKey:AVNumberOfChannelsKey];

      // set the bit depth
      [settings setValue:[NSNumber numberWithInt:16] 
                  forKey:AVLinearPCMBitDepthKey];

      // set whether the format is big endian
      [settings setValue:[NSNumber numberWithBool:NO] 
                  forKey:AVLinearPCMIsBigEndianKey];

      // set whether the audio format is floating point
      [settings setValue:[NSNumber numberWithBool:NO] 
                  forKey:AVLinearPCMIsFloatKey];
      [visualizer clear]; // clear the visualizer

      [recorder release]; // release the recorder AVAudioRecorder

      // initialize recorder with the URL and settings
      recorder =
         [[AVAudioRecorder alloc] initWithURL:[NSURL fileURLWithPath:path]
         settings:settings error:nil];
      [recorder prepareToRecord]; // prepare the recorder to record
      recorder.meteringEnabled = YES; // enable metering for the recorder
      [recorder record]; // start the recording                          

      // start a timer
      timer = [NSTimer scheduledTimerWithTimeInterval:0.05 target:self
         selector:@selector(timerFired:) userInfo:nil repeats:YES];

      // create the stop recording image
      UIImage *stopImage = [UIImage imageNamed:@"stop.png"];

      // change the image on recordButton to the stop image
      [recordButton setImage:stopImage forState:UIControlStateNormal];
    // end else
 // end method record:

【讨论】:

以上是关于iPhone SDK:记录 iPhone 发出的声音的主要内容,如果未能解决你的问题,请参考以下文章

如何开始在 iPhone Sdk 中自动录制视频

在 iphone 中访问通话记录。? [复制]

iPhone SDK:从核心数据填充表格视图

iPhone 应用程序的声音字节

iPhone:如何为自定义键盘制作按键声音?

iPhone SDK 3.2 测试版和 iPhone SDK 3.1.2