錄音

////? 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:@"開(kāi)始錄音" 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是電話采樣率,對(duì)于一般錄音已經(jīng)夠了

[dicM setObject:@(8000) forKey:AVSampleRateKey];

//設(shè)置通道,這里采用單聲道

[dicM setObject:@(1) forKey:AVNumberOfChannelsKey];

//每個(gè)采樣點(diǎn)位數(shù),分為8、16、24、32

[dicM setObject:@(8) forKey:AVLinearPCMBitDepthKey];

//是否使用浮點(diǎn)數(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 - 錄音機(jī)代理方法

/**

*? 錄音完成,錄音完成后播放錄音

*

*? @param recorder 錄音機(jī)對(duì)象

*? @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(@"開(kāi)始播放錄制的音頻!");

} else {

NSLog(@"不能播放錄制的音頻!");

}

}

}

}

@end

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容