iPhone SDK:记录 iPhone 发出的声音

发布于 2024-11-06 08:47:35 字数 178 浏览 0 评论 0原文

我正在开发一个鼓应用

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

我如何制作和/或使用这样的录音机?

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

I'm developing a drum app.

In many other apps, I saw a recorder getting used to record the drum sounds and sample them later.

How can I make and/or use such recorder?

Note: I'm using AudioServices to play the sounds.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

油焖大侠 2024-11-13 08:47:35

您可能想阅读 AVFoundation;您可以使用它作为录制音频和视频的方法。

You might want to read up on AVFoundation; you can use it as a means to record audio and video.

揽月 2024-11-13 08:47:35

iPhone for Programmers: An App-Driven Approach 一书中提到了 AVAudioRecorder,并在示例 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:

AVAudioRecorder is mentioned in the book iPhone for Programmers: An App-Driven Approach with source code in the example 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:
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文