Record Audio(录音)
来源:互联网 发布:马克斯cms怎么安装 编辑:程序博客网 时间:2024/06/02 17:37
1。导入 #import <AVFoundation/AVFoundation.h>
2。实现协议<AVAudioRecorderDelegate>
e.g.
@property (nonatomic, strong)AVAudioRecorder *audioRecorder;
@property (nonatomic, strong) AVAudioPlayer *audioPlayer;
//录音后保存的音频文件的路径
- (NSURL *) audioRecordingPath{
NSFileManager *fileManager = [[NSFileManager alloc] init];
NSURL *documentsFolderUrl = [fileManager URLForDirectory:NSDocumentDirectory inDomain:NSUserDomainMask
appropriateForURL:nil create:NO error:nil];
NSURL *url =[documentsFolderUrl URLByAppendingPathComponent:@"Recording.m4a"];
return url;
}
/* 录音的选项设置 字典形式 */
- (NSDictionary *) audioRecordingSettings{
NSDictionary *dic = @{
AVFormatIDKey : @(kAudioFormatAppleLossless), //录音格式 kAudioFormatLinearPCM,kAudioFormatAppleLossless
AVSampleRateKey : @(44100.0f), //采样率
AVNumberOfChannelsKey : @1, //信道的数目
AVEncoderAudioQualityKey : @(AVAudioQualityLow), //录音的质量 AVAudioQualityMin,AVAudioQualityLow,
// AVAudioQualityMedium,AVAudioQualityHigh,AVAudioQualityMax
};
return dic;
}
- (IBAction)actionRecord:(id)sender {
/* 请求录音权限 */
AVAudioSession *session = [AVAudioSession sharedInstance];
[session setCategory:AVAudioSessionCategoryPlayAndRecord
withOptions:AVAudioSessionCategoryOptionDuckOthers
error:nil];
[session requestRecordPermission:^(BOOL granted) {
if (granted) {
NSLog(@"permission ok to record audio");
[self startRecordingAudio];
}else {
NSLog(@"We don't have permission to record audio.");
}
}];
}
- (void) startRecordingAudio{
NSError *error = nil;
NSURL *audioRecordingURL = [self audioRecordingPath];
self.audioRecorder = [[AVAudioRecorder alloc] initWithURL:audioRecordingURL
settings:[self audioRecordingSettings] error:&error];
if (self.audioRecorder != nil){
self.audioRecorder.delegate = self;
if ([self.audioRecorder prepareToRecord] &&
[self.audioRecorder record]){
NSLog(@"Successfully started to record.");
/* 5秒后停止录音 */
[self performSelector:@selector(stopRecordingOnAudioRecorder:)
withObject:self.audioRecorder
afterDelay:5.0f];
} else {
NSLog(@"Failed to record.");
self.audioRecorder = nil;
}
} else {
NSLog(@"Failed to create an instance of the audio recorder.");
}
}
- (void) stopRecordingOnAudioRecorder:(AVAudioRecorder *)paramRecorder{
NSLog(@"stop to record");
[paramRecorder stop];
}
//录音被打断
- (void)audioRecorderBeginInterruption:(AVAudioRecorder *)recorder{
NSLog(@"Recording process is interrupted");
}
- (void)audioRecorderEndInterruption:(AVAudioRecorder *)recorder withOptions:(NSUInteger)flags{
if (flags ==AVAudioSessionInterruptionOptionShouldResume){
NSLog(@"Resuming the recording...");
[recorder record];
}
}
//结束时要释放,不论是录音,还是播放
- (void)audioRecorderDidFinishRecording:(AVAudioRecorder *)recorder successfully:(BOOL)flag{
if (flag){
NSLog(@"Successfully stopped the audio recording process.");
} else {
NSLog(@"Stopping the audio recording failed.");
}
self.audioRecorder = nil;
}
//------play---------
- (IBAction)actionPlay:(id)sender {
/* 获取录音 */
NSError *playbackError = nil;
NSError *readingError = nil;
NSData *fileData = [NSData dataWithContentsOfURL:[self audioRecordingPath]
options:NSDataReadingMapped error:&readingError];
self.audioPlayer = [[AVAudioPlayer alloc] initWithData:fileData error:&playbackError];
if (self.audioPlayer != nil){
self.audioPlayer.delegate = self;
if ([self.audioPlayer prepareToPlay] && [self.audioPlayer play]){
NSLog(@"Started playing the recorded audio.");
} else {
NSLog(@"Could not play the audio.");
}
} else {
NSLog(@"Failed to create an audio player.");
}
}
- (void)audioPlayerBeginInterruption:(AVAudioPlayer *)player{
/* The audio session has been deactivated here */
}
- (void)audioPlayerEndInterruption:(AVAudioPlayer *)player withOptions:(NSUInteger)flags{
if (flags == AVAudioSessionInterruptionOptionShouldResume){
[player play];
}
}
- (void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)flag{
if (flag){
NSLog(@"Audio player stopped correctly.");
} else {
NSLog(@"Audio player did not stop correctly.");
}
if ([player isEqual:self.audioPlayer]){
self.audioPlayer = nil;
} else {
/* This is not our player */
}
}
- Record Audio(录音)
- ubuntu skype 录音-Record Audio
- 安装Linux 录音软件 Record Audio
- 安装Linux 录音软件 Record Audio
- 安卓使用Audio Record自定义录音
- Windows Phone 7 – 写一个会Record Audio的录音程式
- Record录音简介
- Stop Audio Record
- Android Audio Record
- audio:mt6236 录音驱动小结
- h5使用audio标签录音
- html5 record 录音实例 使用websocket
- node+express搭建record.js录音实例
- ios audio unit remoteIO playback record
- 实现KTV效果:播放歌曲,录音(存储录音文件),并同步播放录音 & 解释4.3以上audio与media资源冲突问题
- audio录音在JAVA层的调用
- iOS Audio Unit 录音 AudioStreamBasicDescription 详解
- Core Audio running two AudioQueues at once, continuous record/playback
- jstat命令用法
- Selenium2.0 WebDriver入门指南
- 新人学习java导论
- CSS解读之换行属性
- vs2010MFCActiveX控件,连接ORACLE数据库,并显示表名,字段名,并把表格在DataGrid显示
- Record Audio(录音)
- 机器学习基本算
- pentaho 5.0.1 迁移到mysql 数据库
- Linux 容器的建立和简单管理
- 批量Append
- 如何在十天内掌握线性代数
- 越狱后 第三方助手显示未越狱如何解决
- aspx前台页面取后台值
- GDI+库