////? LKRecordAudioViewController.m//? H264DecodeDemo////? Created by 聯(lián)坤科技 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:@"結(jié)束錄音" 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"]];? // 關(guān)聯(lián)賬戶 account 文件夾
// 創(chuàng)建路徑
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;
}
/**
*? 取得錄音文件設(shè)置
*
*? @return 錄音設(shè)置
*/
-(NSMutableDictionary *)getAudioSetting{
NSMutableDictionary *dicM=[NSMutableDictionary dictionary];
//設(shè)置錄音格式
[dicM setObject:@(kAudioFormatLinearPCM) forKey:AVFormatIDKey];
//設(shè)置錄音采樣率,8000是電話采樣率,對于一般錄音已經(jīng)夠了
[dicM setObject:@(8000) forKey:AVSampleRateKey];
//設(shè)置通道,這里采用單聲道
[dicM setObject:@(1) forKey:AVNumberOfChannelsKey];
//每個采樣點位數(shù),分為8、16、24、32
[dicM setObject:@(8) forKey:AVLinearPCMBitDepthKey];
//是否使用浮點數(shù)采樣
[dicM setObject:@(YES) forKey:AVLinearPCMIsFloatKey];
//....其他設(shè)置等
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