实用知识:录音功能的实现
来源:互联网 发布:java date日期格式化 编辑:程序博客网 时间:2024/06/05 05:49
#import "ViewController.h"#import <AVFoundation/AVFoundation.h>@interface ViewController ()// AVAudioRecorder 实现相关功能@property (strong, nonatomic) AVAudioRecorder *recorder;@end@implementation ViewController- (void)viewDidLoad{ [super viewDidLoad]; /** 如果运行在真机上, 需要做一些额外配置 (麦克风的访问授权) */ // 向系统声明App会使用到哪些音频相关的功能 NSError *sessionError; [[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryRecord error:&sessionError]; if (sessionError) { NSLog(@"AudioSessionError: %@", sessionError); } // URL是录音文件保存的路径 // 只有沙盒可以保存文件, Document NSString *documentPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0]; // 默认是.aac格式 NSString *filePath = [documentPath stringByAppendingPathComponent:@"recod.aac"]; // 两种方式都可以 NSURL *url = [NSURL URLWithString:filePath]; // 带 file:// NSURL *fileURL = [NSURL fileURLWithPath:filePath]; NSLog(@"url: %@, fileURL: %@", url, fileURL); NSError *error; /** NSString *const AVFormatIDKey; 录音文件的格式, .aac, .mp3, .wav NSString *const AVSampleRateKey; 采样率 NSString *const AVNumberOfChannelsKey; 音频通道数 */ // URL在实例化后是不可修改, 一对一关系 NSDictionary *dict = @{ // 录音文件的格式 AVFormatIDKey : @(kAudioFormatMPEG4AAC) //.aac }; // Settings是需要配置的, 不能为nil _recorder = [[AVAudioRecorder alloc] initWithURL:fileURL settings:dict error:&error]; // 准备录音, 创建文件(等待数据写入), 硬件准备 [_recorder prepareToRecord];}- (IBAction)beginRecordAction:(id)sender{ // 开始录音, 向文件写入数据 // 如果没有处于准备状态, 会隐式的调用prepareToRecord [_recorder record];}- (IBAction)pauseRecordAction:(id)sender{ // 是否正在录音 if (_recorder.isRecording) { // 暂停录音 [_recorder pause]; } else { // 开始录音 [_recorder record]; }}- (IBAction)stopRecordAction:(id)sender{ // 停止录音, 释放(关闭)文件, 释放硬件准备 [_recorder stop];}@end
0 0
- 实用知识:录音功能的实现
- 实用知识:指纹识别功能的实现
- ios录音功能的实现
- iOS 录音功能的实现
- ios录音功能的实现
- iOS 录音功能的实现
- iOS录音功能的实现
- iOS录音功能的实现
- android录音功能的实现
- iOS录音功能的实现
- ios录音功能的实现
- android录音功能的实现
- android 录音功能的实现
- Android实现录音的功能
- Android使用MediaRecorder实现录音功能相关知识
- Android 录音、播放功能的实现
- java实现一个录音播放的功能
- Android 实现能够暂停的录音功能
- solution Of 1087. All Roads Lead to Rome (30)
- 字节流 字符流 乱码
- HttpClient 发送HTTP请求
- CyclicBarrier介绍
- 不允许使用循环语句、条件语句,在控制台中打印出1-200这200个数
- 实用知识:录音功能的实现
- UVA10791 Minimum Sum LCM(数论)
- 第10,11周 项目一(3)
- OTCBVS Benchmark Dataset Collection新链接
- 第11周补充项目1-是春哥吗
- JavaScript经典题目汇总
- 使用 NGINX 进行微程序缓存的好处
- 指针和数组名
- 应用服务器知识(5)--Tomcat源码分析