http://blog.sina.com.cn/s/blog_a573f7990101f5j0.html
录音回话设置
1 NSError *error = nil; 2 AVAudioSession * audioSession = [AVAudioSession sharedInstance]; //得到AVAudioSession单例对象 3 [audioSession setCategory:AVAudioSessionCategoryPlayAndRecord error: &error];//设置类别,表示该应用同时支持播放和录音 4 [audioSession setActive:YES error: &error];//启动音频会话管理,此时会阻断后台音乐的播放. 5 6 // 设置成扬声器播放 7 UInt32 doChangeDefault = 1; 8 AudioSessionSetProperty(kAudioSessionProperty_OverrideCategoryDefaultT oSpeaker, sizeof(doChangeDefault), &doChangeDefault);
1-4主要设置程序启动录音状态,7-8设置的是默认为采用扬声器状态播放,如果你插入耳机,自动改变为耳机状态。
录音之前需要,先关闭播放音乐,如果不关闭,就开始录音,就会出现红色提示框,造成整个view抖动。不过不影响使用。所以在使用上面的代码之前先调用代码关闭音乐播放。
1 NSMutableDictionary *recordSetting = [NSMutableDictionary dictionary]; 2 [recordSetting setValue:[NSNumber numberWithInt:kAudioFormatMPEG4AAC] forKey:AVFormatIDKey]; 3 [recordSetting setValue:[NSNumber numberWithFloat:44100.0] forKey:AVSampleRateKey]; 4 [recordSetting setValue:[NSNumber numberWithInt:2] forKey:AVNumberOfChannelsKey]; 5 [recordSetting setValue:[NSNumber numberWithInt:16] forKey:AVLinearPCMBitDepthKey]; 6 [recordSetting setValue:[NSNumber numberWithBool:YES] forKey:AVLinearPCMIsBigEndianKey]; 7 [recordSetting setValue:[NSNumber numberWithBool:YES] forKey:AVLinearPCMIsFloatKey];
我录音的格式是ma4格式,采用这种格式的主要目的是,此格式录音一分钟生成的大小小于1M,刚好满足苹果官方的审核。这里单声道和双声道大小还是有有一点区别的,但不太多。我非专业人士,听不出来好坏。你也可以录制完毕转换成mp3格式。github上有一个开源的项目
接下来就是初始化,
1 NSError *error = nil; 2 AVAudioRecorder *recorder = [[AVAudioRecorder alloc] initWithURL:_recordFileURL settings:recordSettings error:&error];
_recordFileURL 是NSURL的文件路径。
recordSettings 是上面的音频格式设置。
设置录音时间。得到输入大小。完成委托。
1 [_recorder recordForDuration:(NSTimeInterval) 30.0 * 60.0]; 2 _recorder.meteringEnabled = YES; 3 [_recorder setDelegate:self];
1设置最长的录音时间。2得到输入大小,最大为60,最小为-60。3设置设置委托,委托里面有两个方法,一个当录音完成调用,一个当出现中断调用。
当准备好了就开始录音,千万不要只调用
然后就是设置动态显示的东西,如动态的录音时间,_recorder.currentTime;
得到波形图。
1 - (void)updateMeters; 2 3 - (float)peakPowerForChannel:(NSUInteger)channelNumber; 4 - (float)averagePowerForChannel:(NSUInteger)channelNumber;
使用上面的方法得到每个声道的输入大小。每次都必须调用
录音部分,就完成了,当然你可以对应的使用AVAudioPlayer来播放录制的声音。
1 AVAudioSession *audioSession = [AVAudioSession sharedInstance]; 2 NSError *err = nil; 3 [audioSession setCategory :AVAudioSessionCategoryPlayback error:&err];
这是当音频输入后用一个表示音频输入的大小变化的view
这个没有什么可以说的,直接上代码
#import <Foundation/Foundation.h> @interface AVMeterView : UIView { float peakPowerForChannel; float h; } @property(nonatomic,assign) float peakPowerForChannel; - (id)initWithFrame:(CGRect)frame; @end
#import "AVMeterView.h" #import <CoreGraphics/CoreGraphics.h> #import <QuartzCore/QuartzCore.h> @implementation AVMeterView @synthesize peakPowerForChannel; - (void)dealloc { [ super dealloc ]; } - (id)initWithFrame:(CGRect)frame { self = [ super initWithFrame: frame ]; if (self != nil) { self.backgroundColor = [UIColor clearColor]; h = 100; self.layer.cornerRadius = 8; self.layer.masksToBounds = YES; } return self; } - (void)drawRect:(CGRect)rect { CGRect viewBounds = self.bounds; CGContextRef context = UIGraphicsGetCurrentContext(); CGContextClearRect(context, viewBounds); CGContextSetFillColor(context, CGColorGetComponents([UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:0.5].CGColor)); CGContextFillRect(context, viewBounds); CGContextSetLineWidth(context,5); CGContextSetStrokeColorW ithColor(context, [UIColor whiteColor].CGColor); if (peakPowerForChannel > 50) { peakPowerForChannel = 50; } for (int i= 20; i <= peakPowerForChannel * 1.2; i = i+ 10) { float hs = sqrtf(2) * i; float p = h/sqrtf(2); float p1 = (hs)/2; CGContextMoveToPoint(context,p - p1,h - p1); CGContextAddArcToPoint(context,p,h - hs,p + p1,h - p1,i); } CGContextStrokePath(context); // add image CGContextTranslateCTM(context, 0, viewBounds.size.height); CGContextScaleCTM(context, 1, -1); UIImage *image = [UIImage imageNamed:@"mike.png"]; CGFloat x = self.bounds.size.width - image.size.width; CGContextDrawImage(context, CGRectMake(x*0.5,10, 34, 44), image.CGImage); } @end
出来的图片就是中间那部分。其实啪啪和微信都差不多是这样子做的,很简单的。
下面是一些iPhone OS支持的音频回放格式:
AAC
HE-AAC
AMR (Adaptive Multi-Rate,是一种语音格式)
ALAC (Apple Lossless)
iLBC (互联网Low Bitrate Codec,另一种语音格式)
IMA4 (IMA/ADPCM)
线性PCM (无压缩)
µ-law和a-law
MP3 (MPEG-1 音频第3层)
下面是一些iPhone OS支持的音频录制格式:
ALAC (Apple Lossless)
iLBC (互联网Low Bitrate Codec,用于语音)
IMA/ADPCM (IMA4)
线性PCM
µ-law和a-law
下面的列表总结了iPhone OS如何支持单路或多路音频格式:
线性PCM和IMA4 (IMA/ADPCM) 在iPhoneOS上,您可以同时播放多路线性PCM或IMA4声音,而不会导致CPU资源的问题。这一点同样适用于AMR和iLBC语音品质格式,以及µ-law和a-law压缩格式。在使用压缩格式时,请检查声音的品质,确保满足您的需要。
AAC、MP3、和ALAC (Apple Lossless) AAC、MP3、和ALAC声音的回放可以使用iPhoneOS设备上高效的硬件解码,但是这些编解码器共用一个硬件路径,通过硬件,设备每次只能播放上述格式的一种。
AAC、MP3、和ALAC的回放共用同一硬件路径的事实会对“合作播放”风格的应用程序(比如虚拟钢琴)产生影响。如果用户在iPod程序上播放上述三种格式之一的音频,则您的应用程序—如果要和该音频一起播放声音—需要使用软件解码。
录制完毕后调用上面切换成播放模式