标签:blog os strong 文件 io for art cti
//导入上面2个框架 #import <AVFoundation/AVFoundation.h> #import <CoreFoundation/CoreFoundation.h> //声明下面4个属性 @property (nonatomic ,assign)BOOL recording; //判断是否可以录制 @property (nonatomic ,strong)NSString *fileName; //音频文件存储时的文件名 @property (nonatomic ,strong)AVAudioRecorder *recorder; //声明一个录制器 @property (nonatomic ,strong)AVAudioPlayer *player; //声明一个音频播放器 - (void)startRecording{ if (self.recording) { return; } self.recording =YES; // 设置录音格式 AVFormatIDKey==kAudioFormatLinearPCM // 设置录音采样率(Hz) 如:AVSampleRateKey==8000/44100/96000(影响音频的质量) // 录音通道数 1 或 2 AVNumberOfChannelsKey // 线性采样位数 8、16、24、32 AVLinearPCMBitDepthKey // 录音的质量 AVEncoderAudioQualityKey == AVAudioQualityHigh // AVLinearPCMIsBigEndianKey 大端还是小端,是内存的组织方式 // AVLinearPCMIsFloatKey 采样信号是整数还是浮点数 NSDictionary *audioSettingDict =[NSDictionary dictionaryWithObjectsAndKeys: [NSNumber numberWithFloat:8000],AVSampleRateKey, [NSNumber numberWithInt:kAudioFormatLinearPCM], AVFormatIDKey, [NSNumber numberWithInt:1],AVNumberOfChannelsKey, [NSNumber numberWithInt:16],AVLinearPCMBitDepthKey, [NSNumber numberWithBool:NO],AVLinearPCMIsBigEndianKey, [NSNumber numberWithBool:NO],AVLinearPCMIsFloatKey, [NSNumber numberWithInt:AVAudioQualityHigh],AVEncoderAudioQualityKey, nil]; NSError *error =nil; AVAudioSession * audioSession = [AVAudioSession sharedInstance]; [audioSession setCategory:AVAudioSessionCategoryPlayAndRecord error:&error];//设置音频类别,这里表示当应用启动,停掉后台其他音频 [audioSession setActive:YES error:&error];//设置当前应用音频活跃性 NSDate *now =[NSDate date]; //实例化一个NSDateFormatter对象 NSDateFormatter *dateFormater = [[NSDateFormatter alloc] init]; //设定时间格式,这里可以设置成自己需要的格式 [dateFormater setDateFormat:@"yyyy-MM-dd HH-mm-ss"]; NSString *fileName = [NSString stringWithFormat:@"rec_%@.wav",[dateFormater stringFromDate:now]]; self.fileName =fileName; NSString *filePath =[NSString documentPathWith:fileName]; NSURL *fileUrl =[NSURL URLWithString:filePath]; // 初始化录制的类 self.recorder =[[AVAudioRecorder alloc]initWithURL:fileUrl settings:audioSettingDict error:&error]; [self.recorder prepareToRecord]; [self.recorder peakPowerForChannel:0]; [self.recorder setMeteringEnabled:YES]; [self.recorder record]; }
标签:blog os strong 文件 io for art cti
原文地址:http://www.cnblogs.com/zuolovedu/p/3876716.html