IPhone SDK:记录iPhone制作的声音

时间:2011-05-11 19:01:53

标签: iphone audio record

我正在开发鼓应用

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

如何制作和/或使用此类录音机?

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

2 个答案:

答案 0 :(得分:1)

AVAudioRecorder在 iPhone for Programmers: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:

答案 1 :(得分:0)

您可能想要阅读AVFoundation;你可以用它来记录音频和视频。