录音与播放
2016-06-28 17:00
543 查看
#import "ViewController.h" //导入AVFoundation框架 #import <AVFoundation/AVFoundation.h> @interface ViewController () { NSURL *recordURL; AVAudioPlayer *player; AVAudioRecorder *recorder; } @end
@implementation ViewController
设置button状态为UIControlEventTouchDown开始录音
- (IBAction)startRecord:(UIButton *)sender { //把之前录音机设为空 recorder = nil; //删除已有的声音文件 NSFileManager *manager = [NSFileManager defaultManager]; //通过地址 [manager removeItemAtURL:_soundFileURL error:nil]; //把音频回话设置为录音模式 [[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryRecord error:nil]; //创建设置 /* 1 声音的格式 2 采样率 3 声道数 2 4 采样位数 5 内存的组织方式(大端,小端) 6 编码质量 */ NSDictionary *setting = @{AVFormatIDKey : [NSNumber numberWithInt:kAudioFormatMPEG4AAC], AVSampleRateKey : @1000, AVNumberOfChannelsKey : @2, AVLinearPCMBitDepthKey : @16, AVLinearPCMIsBigEndianKey : @NO, AVLinearPCMIsFloatKey : @NO, AVEncoderAudioQualityKey : [NSNumber numberWithInt:AVAudioQualityMedium] }; //创建录音器 recorder = [[AVAudioRecorder alloc] initWithURL:recordURL settings:setting error:nil]; [recorder prepareToRecord]; //录音器开始录音 [recorder record]; }
设置button状态为UIControlEventTouchUpInside停止录音
- (IBAction)stopRecord:(UIButton *)sender { //停止录音,是录音机停止 [recorder stop]; //把录音机设为空 recorder = nil; //还原音频会话为播放模式 [[AVAudioSession sharedInstance]setCategory:AVAudioSessionCategorySoloAmbient error:nil]; }
播放录音
- (IBAction)playRecord:(UIButton *)sender { //把之前播放器设为空 player = nil; //创建播放器 player = [[AVAudioPlayer alloc] initWithContentsOfURL:recordURL error:nil]; [player prepareToPlay]; [player play]; }
- (void)viewDidLoad { [super viewDidLoad]; //创建录音文件 NSString *recordString = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents/sound.acc"]; //获取文件地址 recordURL = [NSURL fileURLWithPath:recordString]; }
@end
相关文章推荐
- 前端组件化开发实践
- 一些常用的正则表达式(手机号、身份证号、邮箱等判断)
- 元素分类
- kernel/timeconst.pl line 373.
- oracle自动备份
- 被动操作系统扫描软件 p0f 指纹库文件分析
- java.lang.RuntimeException: Unable to instantiate application com.android.tools.fd.runtime.Bootstrap
- [改善Java代码]使用CyclicBarrier让多线程齐步走
- git clone和fork的区别
- 简单的登入验证
- 13.追我的男生辣么多
- j2ee部分jar包的作用
- Java的位运算符详解实例
- Ubuntu终端常用的快捷键
- spring mvc里的验证码
- Android动画复习(包括缩放动画、平移动画、旋转动画、透明度动画)
- 重新认识HashMap
- Android开发 解决AlertDialog中的EditText无法调出输入法的问题
- sysinternals
- iOS .tdb代替.dylib