//// LKRecordAudioViewController.m// H264DecodeDemo//// Created by 联坤科技 on 2017/7/13.// Copyright © 2017年 LianKun. All rights reserved.//#import "LKRecordAudioViewController.h"#import#import@interface LKRecordAudioViewController (){
NSString *audioRecoderSavePath;
NSString *savePath;
NSURL *tempRecordedFile;
AVAudioPlayer *avplayer;
AVAudioRecorder *recorder;
NSString * tempRecoderPath;
NSMutableArray *audioRcoderMutableArray;
NSFileManager *fileMgr;
NSString *recoderName;
NSString *dateaudioPath;
NSMutableArray *passAudioMutableArray;
}
@property (nonatomic,strong) AVAudioPlayer *audioplayer;
@end
@implementation LKRecordAudioViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor greenColor];
UIButton *RecordAudio = [[UIButton alloc] initWithFrame:CGRectMake(50, 280, 200, 50)];
[RecordAudio setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[RecordAudio setBackgroundColor:[UIColor redColor]];
[RecordAudio setTitle:@"开始录音" forState:UIControlStateNormal];
[RecordAudio setTitle:@"结束录音" forState:UIControlStateSelected];
[RecordAudio addTarget:self action:@selector(RecordAudio:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:RecordAudio];
// ===========================================================
NSDate * date=[NSDate date];
NSDateFormatter *dateformatter=[[NSDateFormatter alloc] init];
[dateformatter setDateFormat:@"YYYY-MM-dd"];
NSString *datefloder= [dateformatter stringFromDate:date];
dateaudioPath=[NSString stringWithFormat:@"%@/",datefloder];
fileMgr = [NSFileManager defaultManager];
//指向文件目录
NSString *documentsDirectory= [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];
audioRecoderSavePath=[NSString stringWithFormat:@"%@/%@", documentsDirectory,dateaudioPath];
if (![fileMgr fileExistsAtPath:audioRecoderSavePath]) {
[fileMgr createDirectoryAtPath:audioRecoderSavePath withIntermediateDirectories:YES attributes:nil error:nil];
}
NSString *docPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
savePath = [docPath stringByAppendingPathComponent:[NSString stringWithFormat:@"TakeAudio"]]; // 关联账户 account 文件夹
// 创建路径
if (![fileMgr fileExistsAtPath:savePath]) {
[fileMgr createDirectoryAtPath:savePath withIntermediateDirectories:YES attributes:nil error:nil];
}
//
AVAudioSession *session = [AVAudioSession sharedInstance];
NSError *sessionError;
[session setCategory:AVAudioSessionCategoryPlayAndRecord error:&sessionError];
if(session == nil)
NSLog(@"Error creating session: %@", [sessionError description]);
else
[session setActive:YES error:nil];
}
-(void)RecordAudio:(UIButton *)sender{
if(!sender.selected)
{
NSDate * date=[NSDate date];
NSDateFormatter *dateformatter=[[NSDateFormatter alloc] init];
[dateformatter setDateFormat:@"YYYYMMddHHYYSS"];
recoderName= [NSString stringWithFormat:@"%@%@",[dateformatter stringFromDate:date],@".caf"];
tempRecoderPath=[NSString stringWithFormat:@"%@%@",audioRecoderSavePath,recoderName];
tempRecordedFile = [NSURL fileURLWithPath:tempRecoderPath];
recorder = [[AVAudioRecorder alloc] initWithURL:tempRecordedFile settings:[self getAudioSetting] error:nil];
recorder.delegate=self;
[recorder prepareToRecord];
[recorder record];
avplayer = nil;
}
//If the app is recording, we want to stop recording, enable the play button, and make the record button say "REC"
else
{
[recorder stop];
recorder = nil;
}
sender.selected = !sender.selected;
}
/**
* 取得录音文件设置
*
* @return 录音设置
*/
-(NSMutableDictionary *)getAudioSetting{
NSMutableDictionary *dicM=[NSMutableDictionary dictionary];
//设置录音格式
[dicM setObject:@(kAudioFormatLinearPCM) forKey:AVFormatIDKey];
//设置录音采样率,8000是电话采样率,对于一般录音已经够了
[dicM setObject:@(8000) forKey:AVSampleRateKey];
//设置通道,这里采用单声道
[dicM setObject:@(1) forKey:AVNumberOfChannelsKey];
//每个采样点位数,分为8、16、24、32
[dicM setObject:@(8) forKey:AVLinearPCMBitDepthKey];
//是否使用浮点数采样
[dicM setObject:@(YES) forKey:AVLinearPCMIsFloatKey];
//....其他设置等
return dicM;
}
//保存录音
-(void)SaveAudioRecoder
{
// AudioObject *object=[[AudioObject alloc]init];
// object.audioRecoderName=recoderName;
// object.audioRecoderPath=tempRecoderPath;
// object.audioRecoderIsChecked=NO;
// [audioRcoderMutableArray addObject:object];
// [recoderTableView reloadData];
}
#pragma mark - 录音机代理方法
/**
* 录音完成,录音完成后播放录音
*
* @param recorder 录音机对象
* @param flag 是否成功
*/
-(void)audioRecorderDidFinishRecording:(AVAudioRecorder *)recorder successfully:(BOOL)flag{
if (flag == YES) {
NSLog(@"录音完成!");
NSError *playbackError = nil;
NSError *readingError = nil;
NSData *fileData = [NSData dataWithContentsOfFile:tempRecoderPath options:NSDataReadingMapped error:&readingError];
self.audioplayer = [[AVAudioPlayer alloc] initWithData:fileData
error:&playbackError];
if (self.audioplayer != nil) {
self.audioplayer.delegate = self;
if ([self.audioplayer prepareToPlay] == YES &&
[self.audioplayer play] == YES) {
NSLog(@"开始播放录制的音频!");
} else {
NSLog(@"不能播放录制的音频!");
}
}
}
}
@end
网友评论