原理:
进入界面,先遍历文件目录,将所有的文件名,显示在uitableview中。在录音时需要设置session以及录音采样率。
1.ios录音主要使用ios自带的类,是工程中需要手动添加这俩个framework
#import <AVFoundation/AVFoundation.h>
#import <CoreAudio/CoreAudioTypes.h>
2.在录音前,先监测文件存放目录是否存在,不存在就创建目录
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,audioPath,dateaudioPath];
if (![fileMgr fileExistsAtPath:audioRecoderSavePath]) {
[fileMgr createDirectoryAtPath:audioRecoderSavePath withIntermediateDirectories:YES attributes:nil error:nil];
}
3.点击录音,开始录音
if(!isRecording)
{
isRecording = YES;
stateLabel.text=@"录音中";
[startRecoderBtn setTitle:@"停止录音" forState:(UIControlStateNormal)];
// startRecoderBtn.titleLabel.text=@"停止录音";
NSDate * date=[NSDate date];
NSDateFormatter *dateformatter=[[NSDateFormatter alloc] init];
[dateformatter setDateFormat:@"YYYYMMddHHYYSS"];
recoderName= [NSString stringWithFormat:@"%@%@",[dateformatter stringFromDate:date],@".caf"];
//tempRecoderPath=[NSHomeDirectory() stringByAppendingString:[NSString stringWithFormat:@"%@%@",audioPath,recoderName]];
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
{
isRecording = NO;
stateLabel.text=[NSString stringWithFormat:@"%@%@",@"录音完成",recoderName];
//startRecoderBtn.titleLabel.text=@"开始录音";
[startRecoderBtn setTitle:@"开始录音" forState:(UIControlStateNormal)];
[recorder stop];
recorder = nil;
}
4.在列表展示文件目录中,遍历所有的录音文件
-(NSMutableArray *)getFilenamelistOfType:(NSString *)type fromDirPath:(NSString *)dirPath
{
NSMutableArray *filenamelist = [[NSMutableArray alloc]init];
NSArray *tmplist = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:dirPath error:nil];
for (NSString *filename in tmplist) {
NSString *fullpath = [dirPath stringByAppendingPathComponent:filename];
if ([self isFileExistAtPath:fullpath]) {
if ([[filename pathExtension] isEqualToString:type]) {
AudioObject *ob=[[AudioObject alloc]init];
ob.audioRecoderName=filename;
ob.audioRecoderPath=fullpath;
ob.audioRecoderIsChecked=NO;
[filenamelist addObject:ob];
}
}
}
return filenamelist;
}
-(BOOL)isFileExistAtPath:(NSString*)fileFullPath {
BOOL isExist = NO;
isExist = [[NSFileManager defaultManager] fileExistsAtPath:fileFullPath];
return isExist;
}
完整代码
头文件
#import <UIKit/UIKit.h>
#import <AVFoundation/AVFoundation.h>
#import <CoreAudio/CoreAudioTypes.h>