如何用 iPad 录制声音?
Posted
技术标签:
【中文标题】如何用 iPad 录制声音?【英文标题】:How to record voice with an iPad? 【发布时间】:2011-04-07 09:05:45 【问题描述】:我需要一个在 iPad 上录制语音的好例子或指南。无论我说什么,我都希望以后能够播放。我该怎么做?
【问题讨论】:
developer.apple.com上的大量示例源代码 【参考方案1】:实际上,根本没有例子。这是我的工作代码。记录由用户按下导航栏上的按钮触发。录音使用 cd 质量(44100 个样本)、立体声(2 通道)线性 pcm。注意:如果您想使用不同的格式,尤其是编码格式,请确保您完全了解如何设置 AVAudioRecorder 设置(仔细阅读音频类型文档),否则您将永远无法正确初始化它。还有一件事。在代码中,我没有展示如何处理计量数据,但您可以轻松搞定。最后,请注意,在撰写本文时,AVAudioRecorder 方法 deleteRecording 会使您的应用程序崩溃。这就是我通过文件管理器删除录制文件的原因。录制完成后,我使用 KVC 将录制的音频保存为 NSData 在当前编辑的对象中。
#define DOCUMENTS_FOLDER [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"]
- (void) startRecording
UIBarButtonItem *stopButton = [[UIBarButtonItem alloc] initWithTitle:@"Stop" style:UIBarButtonItemStyleBordered target:self action:@selector(stopRecording)];
self.navigationItem.rightBarButtonItem = stopButton;
[stopButton release];
AVAudiosession *audioSession = [AVAudioSession sharedInstance];
NSError *err = nil;
[audioSession setCategory :AVAudioSessionCategoryPlayAndRecord error:&err];
if(err)
NSLog(@"audioSession: %@ %d %@", [err domain], [err code], [[err userInfo] description]);
return;
[audioSession setActive:YES error:&err];
err = nil;
if(err)
NSLog(@"audioSession: %@ %d %@", [err domain], [err code], [[err userInfo] description]);
return;
recordSetting = [[NSMutableDictionary alloc] init];
[recordSetting setValue :[NSNumber numberWithInt:kAudioFormatLinearPCM] forKey:AVFormatIDKey];
[recordSetting setValue:[NSNumber numberWithFloat:44100.0] forKey:AVSampleRateKey];
[recordSetting setValue:[NSNumber numberWithInt: 2] forKey:AVNumberOfChannelsKey];
[recordSetting setValue :[NSNumber numberWithInt:16] forKey:AVLinearPCMBitDepthKey];
[recordSetting setValue :[NSNumber numberWithBool:NO] forKey:AVLinearPCMIsBigEndianKey];
[recordSetting setValue :[NSNumber numberWithBool:NO] forKey:AVLinearPCMIsFloatKey];
// Create a new dated file
NSDate *now = [NSDate dateWithTimeIntervalSinceNow:0];
NSString *caldate = [now description];
recorderFilePath = [[NSString stringWithFormat:@"%@/%@.caf", DOCUMENTS_FOLDER, caldate] retain];
NSURL *url = [NSURL fileURLWithPath:recorderFilePath];
err = nil;
recorder = [[ AVAudioRecorder alloc] initWithURL:url settings:recordSetting error:&err];
if(!recorder)
NSLog(@"recorder: %@ %d %@", [err domain], [err code], [[err userInfo] description]);
UIAlertView *alert =
[[UIAlertView alloc] initWithTitle: @"Warning"
message: [err localizedDescription]
delegate: nil
cancelButtonTitle:@"OK"
otherButtonTitles:nil];
[alert show];
[alert release];
return;
//prepare to record
[recorder setDelegate:self];
[recorder prepareToRecord];
recorder.meteringEnabled = YES;
BOOL audioHWAvailable = audioSession.inputIsAvailable;
if (! audioHWAvailable)
UIAlertView *cantRecordAlert =
[[UIAlertView alloc] initWithTitle: @"Warning"
message: @"Audio input hardware not available"
delegate: nil
cancelButtonTitle:@"OK"
otherButtonTitles:nil];
[cantRecordAlert show];
[cantRecordAlert release];
return;
// start recording
[recorder recordForDuration:(NSTimeInterval) 10];
- (void) stopRecording
[recorder stop];
NSURL *url = [NSURL fileURLWithPath: recorderFilePath];
NSError *err = nil;
NSData *audioData = [NSData dataWithContentsOfFile:[url path] options: 0 error:&err];
if(!audioData)
NSLog(@"audio data: %@ %d %@", [err domain], [err code], [[err userInfo] description]);
[editedObject setValue:[NSData dataWithContentsOfURL:url] forKey:editedFieldKey];
//[recorder deleteRecording];
NSFileManager *fm = [NSFileManager defaultManager];
err = nil;
[fm removeItemAtPath:[url path] error:&err];
if(err)
NSLog(@"File Manager: %@ %d %@", [err domain], [err code], [[err userInfo] description]);
UIBarButtonItem *startButton = [[UIBarButtonItem alloc] initWithTitle:@"Record" style:UIBarButtonItemStyleBordered target:self action:@selector(startRecording)];
self.navigationItem.rightBarButtonItem = startButton;
[startButton release];
- (void)audioRecorderDidFinishRecording:(AVAudioRecorder *) aRecorder successfully:(BOOL)flag
NSLog (@"audioRecorderDidFinishRecording:successfully:");
// your actions here
来自这个答案 - How do I record audio on iPhone with AVAudioRecorder?
【讨论】:
-1:哇,太棒了。另一个用户答案的直接逐字复制。我很惊讶你得到一个赞成票。 @FreeAsInBeer 我在我的回答中也提到过......我已经从另一个 SO 答案中得到了答案......所以为什么是 -1??? 至少在我看来,简单地复制别人的答案并不是 SO 的工作方式。相反,写一个小总结并链接到另一个问题会更合适。如果由于任何原因更新了另一个问题,那么您的帖子将包含可能无法正常运行甚至无法编译的旧代码。由于完全缺乏努力,我得到了-1;看来您只是简单地搜索了 OP 的问题并复制了您找到的第一个答案的整个帖子。以上是关于如何用 iPad 录制声音?的主要内容,如果未能解决你的问题,请参考以下文章