录音
来源:互联网 发布:网络封包 编辑:程序博客网 时间:2024/04/27 18:51
简单实现录音功能
声明全局成员变量
@property(nonatomic,strong)AVAudioRecorder *recorder ;
@property (nonatomic,strong) AVAudioPlayer * audioPlayer;
@property (nonatomic,strong) CADisplayLink *link;
@property (nonatomic,assign) double slientDuration;
- (CADisplayLink *)link
{
if (!_link) {
self.link = [CADisplayLinkdisplayLinkWithTarget:selfselector:@selector(update)];
}
return_link;
}
//开始录音
- (IBAction)startRecording:(id)sender {
NSString *path = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES)lastObject] stringByAppendingString:@"tesst.caf"];
NSURL *url = [NSURLfileURLWithPath:path];
NSMutableDictionary *setting = [NSMutableDictionarydictionary];
//音频格式
setting[AVFormatIDKey] =@(kAudioFormatAppleIMA4);
//音频采样率
setting[AVSampleRateKey]=@(8000.0);
//音频通道数
setting[AVNumberOfChannelsKey] =@(1);
//线性音频的位深度
setting[AVLinearPCMBitDepthKey] =@(8);
// 创建录音器
AVAudioRecorder *recorder = [[AVAudioRecorderalloc] initWithURL:urlsettings:setting error:nil];
//允许测量分贝
recorder.meteringEnabled =YES;
//缓冲
[recorder prepareToRecord];
//录音
[recorder record];
self.recorder = recorder;
}
- (IBAction)stopRecording:(id)sender {
[self.recorderstop];
}
//真机中文件读取不到 可以在模拟器中取到声音文件
- (IBAction)playRecording:(id)sender {
//加载音乐文件
NSString *path = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES)lastObject] stringByAppendingString:@"tesst.caf"];
// NSURL *url = [[NSBundle mainBundle] URLForResource:path withExtension:nil];
NSData *data = [[NSFileManagerdefaultManager] contentsAtPath:path];
NSError *error =nil;
_audioPlayer = [[AVAudioPlayeralloc] initWithData:dataerror:&error];
// _audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:&error];
NSLog(@"%@", error);
//缓冲
[_audioPlayerprepareToPlay];
[_audioPlayerplay];
}
- 录音
- 录音
- 录音
- 录音
- 录音
- 录音
- 录音
- 录音
- 录音
- 录音
- 网页录音,html5录音
- DirectSound 录音
- Android 录音
- MCI录音
- android 录音
- Android 录音
- caf录音
- android 录音
- MS14-058(CVE-2014-4113)利用代码
- spring框架整合ibatis的项目实例代码
- (1)ActivityThread分析
- ThinkPHP基础(一)搭建环境
- NwCreate之创建nwc文件
- 录音
- 标题
- SqueezeNet: AlexNet-level accuracy with 50x fewer parameters and <0.5MB model size
- eclipse 复制 粘贴 保存 等快捷键失效 原因及解决方案
- 我们的殊途 还会不会同归?
- github一般使用流程
- Dijkstra算法 ---求最短路径
- iOS浅析蓝牙设备之服务器(外围设备)
- DOM学习之事件处理和事件对象