通过 AVAudioRecorder 录制声音
Posted
技术标签:
【中文标题】通过 AVAudioRecorder 录制声音【英文标题】:Record the sound through AVAudioRecorder 【发布时间】:2013-03-14 09:49:07 【问题描述】:我想录制声音并想将录制的文件保存到文档目录中:- 做过喜欢:-
[[AVAudiosession sharedInstance]
setCategory: AVAudioSessionCategoryRecord
error: nil];
NSMutableDictionary *recordSetting = [[NSMutableDictionary alloc] init];
[recordSetting setValue :[NSNumber numberWithInt:kAudioFormatAppleIMA4] forKey:AVFormatIDKey];
[recordSetting setValue:[NSNumber numberWithFloat:16000.0] forKey:AVSampleRateKey];
[recordSetting setValue:[NSNumber numberWithInt: 1] forKey:AVNumberOfChannelsKey];
recorderFilePath = [[NSString stringWithFormat:@"%@/Audio.caf", DOCUMENTS_FOLDER] retain];
NSURL *soundFileURL=[NSURL URLWithString:recorderFilePath];
recorderFilePath = [[NSString stringWithFormat:@"%@/Audio.caf", DOCUMENTS_FOLDER] retain];
NSURL *soundFileURL = [NSURL fileURLWithPath:recorderFilePath];
NSLog(@"str==>%@ soundurl==>%@",recorderFilePath,soundFileURL);
AVAudioRecorder *newRecorder =
[[AVAudioRecorder alloc] initWithURL: url
settings: settings
error: nil];
newRecorder.delegate = self;
[newRecorder prepareToRecord];
[newRecorder record];
和[录音机停止];当停止录制完成时。
看过这个:- this
并相应编码..但我仍然无法在文档文件夹中创建任何 .caf 文件。
【问题讨论】:
【参考方案1】:一旦完成下面的代码,它可能会对您有所帮助。 首先将 AVAudioRecorder 的对象作为 AVAudioRecorder *audioRecorder;
-(void)viewWillAppear:(BOOL)animated
NSArray *dirPaths;
NSString *docsDir;
dirPaths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
//here i took cache directory as saved directory you can also take NSDocumentDirectory
docsDir = [dirPaths objectAtIndex:0];
NSString *soundFilePath = [docsDir stringByAppendingPathComponent:@"sound.caf"];//at this path your recorded audio will be saved
NSURL *soundFileURL = [NSURL fileURLWithPath:soundFilePath];
NSDictionary *recordSettings = [NSDictionary dictionaryWithObjectsAndKeys:
[NSNumber numberWithInt:AVAudioQualityMin],
AVEncoderAudioQualityKey,
[NSNumber numberWithInt:16],
AVEncoderBitRateKey,
[NSNumber numberWithInt: 2],
AVNumberOfChannelsKey,
[NSNumber numberWithFloat:44100.0],
AVSampleRateKey,
nil];
NSError *error = nil;
audioRecorder = [[AVAudioRecorder alloc]initWithURL:soundFileURL settings:recordSettings error:&error];
if( !audioRecorder )
UIAlertView *alert =
[[UIAlertView alloc] initWithTitle: @"Warning" message: [error localizedDescription] delegate: nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
[alert show];
return;
if ( error )
NSLog( @"error: %@", [error localizedDescription] );
else
[audioRecorder prepareToRecord];
[self recordAudio];//Mehod to Record Audio
现在定义录制音频的方法,
-(void) recordAudio
if (!audioRecorder.recording)
audioRecorder.delegate = self;
[audioRecorder recordForDuration:10];//Here i took record duration as 10 secs
现在您可以将 AvAudio 记录 DelegateMethods 编写为,
-(void)audioRecorderDidFinishRecording:(AVAudioRecorder *)recorder successfully: (BOOL)flag
//Your code after sucessful recording;
-(void)audioRecorderEncodeErrorDidOccur:(AVAudioRecorder *)recorder error:(NSError *)error
NSLog(@"Encode Error occurred");
希望对你有帮助...
【讨论】:
感谢您的回复。我尝试使用这个..声音文件也被创建..但无法从该文件中听到任何声音 抱歉,先录制音频,然后播放缓存目录下名为 sound.caf 的音频文件 是的,我从快速播放器中播放了 sound.caf。但什么也听不到 确保你的声音没有静音。因为我在这里使用了这段代码,对我来说工作正常 你有 10 秒时长的音频吗?以上是关于通过 AVAudioRecorder 录制声音的主要内容,如果未能解决你的问题,请参考以下文章
为啥我无法播放 AVAudioRecorder 录制的声音文件?
iPhone AVAudioRecorder,AudioQueueServices,检测到声音时如何自动录制音频