【问题标题】:Cocoa: Simple audio recordingCocoa:简单的录音
【发布时间】:2014-01-28 19:40:06
【问题描述】:

我正在尝试在我的应用程序中实现一些简单的音频录制功能,但我不知道该怎么做。我被指向this example,但我无法让它在 XCode 中运行,而且它似乎是用 C++ 编写的。

我需要做的是将音频录制到文件中,然后能够在录制时获取录制的当前时间戳。我将不胜感激。谢谢!

【问题讨论】:

  • 他从不展示自己的一行代码。查看他最近打开的 4 个主题。
  • 所以,首先,这实际上是不正确的。我问了 77 个问题,其中大多数都有我自己的代码。对于这样的一般性问题,我不确定您希望看到什么样的代码。

标签: objective-c macos cocoa core-audio audio-recording


【解决方案1】:

您可以使用AVFoundation 框架录制和播放音频。首先,您需要在您的 .h 文件中实现它,并在您的 xcode 项目设置中添加一个框架或库,如下所示:

添加到您的项目设置后,将AVFoundation 导入到您的.h 文件中,如下所示:

#import <AVFoundation/AVFoundation.h>

现在在您的 .h 文件中实现您的委托:

@interface ViewController : UIViewController <AVAudioRecorderDelegate, AVAudioPlayerDelegate>

在此之后,在您的 .h 文件中声明您的 AVAudioRecorderAVAudioPlayer,如下所示:

@interface ViewController () {
     AVAudioRecorder *recorder;
     AVAudioPlayer *player;
IBOutlet UIButton *stopButton;
IBOutlet UIButton *playButton ;
}

- (IBAction)recordPauseTapped:(id)sender;
- (IBAction)stopTapped:(id)sender;
- (IBAction)playTapped:(id)sender;

现在在-(Void)ViewDidLoad{} 中设置所有内容:

- (void)viewDidLoad
{
     [super viewDidLoad];

     // Disable Stop/Play button when application launches
     [stopButton setEnabled:NO];
     [playButton setEnabled:NO];

     // Set the audio file
     NSArray *pathComponents = [NSArray arrayWithObjects:
                                [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject],
                                @"MyAudioMemo.m4a",
                                nil];
     NSURL *outputFileURL = [NSURL fileURLWithPathComponents:pathComponents];

     // Setup audio session
     AVAudioSession *session = [AVAudioSession sharedInstance];
     [session setCategory:AVAudioSessionCategoryPlayAndRecord error:nil];

     // Define the recorder setting
     NSMutableDictionary *recordSetting = [[NSMutableDictionary alloc] init];

     [recordSetting setValue:[NSNumber numberWithInt:kAudioFormatMPEG4AAC] forKey:AVFormatIDKey];
     [recordSetting setValue:[NSNumber numberWithFloat:44100.0] forKey:AVSampleRateKey];
     [recordSetting setValue:[NSNumber numberWithInt: 2] forKey:AVNumberOfChannelsKey];

     // Initiate and prepare the recorder
     recorder = [[AVAudioRecorder alloc] initWithURL:outputFileURL settings:recordSetting error:NULL];
     recorder.delegate = self;
     recorder.meteringEnabled = YES;
     [recorder prepareToRecord];
}

现在像这样实现录制按钮...

- (IBAction)recordPauseTapped:(id)sender {
     // Stop the audio player before recording
     if (player.playing) {
         [player stop];
     }

     if (!recorder.recording) {
         AVAudioSession *session = [AVAudioSession sharedInstance];
         [session setActive:YES error:nil];

         // Start recording
         [recorder record];
         [recordPauseButton setTitle:@"Pause" forState:UIControlStateNormal];

     } else {

         // Pause recording
         [recorder pause];
         [recordPauseButton setTitle:@"Record" forState:UIControlStateNormal];
     }

     [stopButton setEnabled:YES];
     [playButton setEnabled:NO];
}

现在实现StopButton IBAction:

- (IBAction)stopTapped:(id)sender {
     [recorder stop];

     AVAudioSession *audioSession = [AVAudioSession sharedInstance];
     [audioSession setActive:NO error:nil];
}

接下来像这样实现playTapped IBAction:

- (IBAction)playTapped:(id)sender {
     if (!recorder.recording){
         player = [[AVAudioPlayer alloc] initWithContentsOfURL:recorder.url error:nil];
         [player setDelegate:self];
         [player play];
     }
}

最后通过这样做实现所需的AVPlayer Delegate

- (IBAction)playTapped:(id)sender {
     if (!recorder.recording){
         player = [[AVAudioPlayer alloc] initWithContentsOfURL:recorder.url error:nil];
         [player setDelegate:self];
         [player play];
     }
}

就是这样!成品应该看起来像这样......

有关更多信息,请查看以下链接:

Link1

Link2

Link3

文档链接:

Link 1

希望这会有所帮助。

【讨论】:

  • 谢谢!这将非常有用。你对我的问题的第二部分有答案吗?在录制过程中,如何确定录制的当前时间是多少?
  • 您将- (IBAction)playTapped:(id)sender 段复制粘贴了两次。你能展示一下委托的实现吗?
【解决方案2】:

上述 AVFoundation 框架仅适用于 iOS。在 OS X 上处理音频一开始是相当痛苦的。 CoreAudio,虽然它是我使用过的最好的组件之一,但确实需要一些时间来学习和理解。例如,您可能需要考虑使用 https://github.com/syedhali/EZAudio 来完成您的任务。

【讨论】:

  • 正是我想要的!谢谢!
猜你喜欢
  • 1970-01-01
  • 2011-12-27
  • 2011-02-17
  • 1970-01-01
  • 1970-01-01
  • 2011-09-03
  • 2016-12-17
  • 1970-01-01
  • 2011-04-08
相关资源
最近更新 更多