.h 文件
@interface ViewController : UIViewController <AVAudioRecorderDelegate>
{
// 用来录音
AVAudioRecorder *recoder;
// 设置定时检测, 用来监听当前音量大小, 控制话筒图片.
NSTimer *timer;
// 音频的 存储的 url 路径
NSURL *urlPlay;
}
// 用来控制录音功能
@property (nonatomic, strong) UIButton *recordButton;
// 音频播放器
@property (nonatomic, strong) AVAudioPlayer *audioPlay;
@end
#####################################################################
.m 文件
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// 进行录音设置
[self xLAudio];
self.recordButton = [UIButton buttonWithType:UIButtonTypeCustom];
_recordButton.backgroundColor = [UIColor purpleColor];
self.recordButton.frame = CGRectMake(self.imageView.frame.origin.x, 250, 50, 40);
[self.recordButton setTitle:@"start" forState:UIControlStateNormal];
// 当按钮被按下
[self.recordButton addTarget:self action:@selector(recordButtonDown:) forControlEvents:UIControlEventTouchDown];
// 当手指抬起时
[self.recordButton addTarget:self action:@selector(recordButtonUp:) forControlEvents:UIControlEventTouchUpInside];
// 当触摸拖动离开控制范围时
[self.recordButton addTarget:self action:@selector(recordButtonDragUp:) forControlEvents:UIControlEventTouchDragExit];
[self.view addSubview:self.recordButton];
// 设置音频会话
[self xLSetAudioSession];
}
// 设置音频会话
- (void)xLSetAudioSession {
AVAudioSession *audioSession = [AVAudioSession sharedInstance];
// 设置为播放和录音状态, 以便可以在录制完之后播放录音
[audioSession setCategory:AVAudioSessionCategoryPlayAndRecord error:nil];
[audioSession setActive:YES error:nil];
}
// 录音设置
- (void)xLAudio {
// 先配置Recorder
NSMutableDictionary *recordSetting = [NSMutableDictionary dictionary];
// 设置录音格式
[recordSetting setValue:[NSNumber numberWithInt:kAudioFormatMPEG4AAC] forKey:AVFormatIDKey];
// 设置录音采样率
[recordSetting setValue:[NSNumber numberWithFloat:44100] forKey:AVSampleRateKey];
// 录音的通道数
[recordSetting setValue:[NSNumber numberWithInt:1] forKey:AVNumberOfChannelsKey];
// 线性采样位数8, 16, 24, 32
[recordSetting setValue:[NSNumber numberWithInt:16] forKey:AVLinearPCMBitDepthKey];
// 录音质量
[recordSetting setValue:[NSNumber numberWithInt:AVAudioQualityHigh] forKey:AVEncoderAudioQualityKey];
NSString *strUrl = [self xlGetSavePathWithFileSuffix:@"/record.aac"];
NSURL *url = [NSURL fileURLWithPath:[NSString stringWithFormat:@"%@", strUrl]];
urlPlay = url;
NSError *error = nil;
recoder = [[AVAudioRecorder alloc] initWithURL:url settings:recordSetting error:&error];
// 开启音量检测
recoder.meteringEnabled = YES;
recoder.delegate = self;
}
- (void)recordButtonDown:(UIButton *)sender {
[sender setTitle:@"stop" forState:UIControlStateNormal];
// 创建录音文件, 准备录音
if ([recoder prepareToRecord]) {
// 开始
[recoder record];
}
// 设置定时检测
timer = [NSTimer scheduledTimerWithTimeInterval:0 target:self selector:@selector(detectionVoice) userInfo:nil repeats:YES];
}
// 检测当前声音
- (void)detectionVoice {
[recoder updateMeters]; // 刷新当前音量数据
double lowPassResults = pow(10, (0.05 * [recoder peakPowerForChannel:0]));
// 取值范围现在是 0 ~ 1
if (0 < lowPassResults < 0.06) {
}
}
- (void)recordButtonUp:(UIButton *)sender {
[sender setTitle:@"start" forState:UIControlStateNormal];
double cTime = recoder.currentTime;
if (cTime > 2) {
NSError *error = nil;
self.audioPlay = [[AVAudioPlayer alloc] initWithContentsOfURL:urlPlay error:&error];
if (!error) {
_audioPlay.currentTime = 0;
[_audioPlay play];
}
self.audioPlay = nil;
NSLog(@"放出去...");
} else {
// 删除记录文件
[recoder deleteRecording];
}
[recoder stop];
[timer invalidate];
}
- (void)recordButtonDragUp:(UIButton *)sender {
[sender setTitle:@"start" forState:UIControlStateNormal];
// 删除录制文件
[recoder deleteRecording];
[recoder stop];
[timer invalidate];
NSLog(@"取消发送");
}
// 获取文件名:由时间 + 后缀组成
- (NSString *)xlGetSavePathWithFileSuffix:(NSString *)suffix {
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentPath = [paths objectAtIndex:0];
NSDate *date = [NSDate date];
// 获取当前时间
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:@"yyyyMMddHHmmss"];
NSString *curretDateAndTime = [dateFormat stringFromDate:date];
// 命名文件
NSString *fileName = [NSString stringWithFormat:@"%@.%@", curretDateAndTime,suffix];
// 指定文件存储路径
NSString *filePath = [documentPath stringByAppendingPathComponent:fileName];
return filePath;
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
if ([self isViewLoaded] && !self.view.window) {
self.view = nil;
}
}
@end