#import "ViewController.h"
#import
#import
@interface ViewController ()
@property (strong, nonatomic)AVAudioRecorder *audioRecorder;
@property (strong, nonatomic)AVAudioPlayer *audioPlayer;
@end
@implementation ViewController
-(AVAudioRecorder*)audioRecorder{
if (!_audioRecorder) {
//保存路径
NSString *path=[NSHomeDirectory() stringByAppendingString:@"Documents/record"];
//录音设置
NSMutableDictionary *dict=[[NSMutableDictionary alloc]init];
//采样率
[dictsetObject:[NSNumber numberWithInt:44100] forKey:AVSampleRateKey];
//格式
[dictsetObject:[NSNumber numberWithInt:kAudioFormatMPEG4AAC] forKey:AVFormatIDKey];
//通道
[dictsetObject:[NSNumber numberWithInt:1] forKey:AVNumberOfChannelsKey];
//质量
[dictsetObject:[NSNumber numberWithInt:AVAudioQualityHigh] forKey:AVEncoderAudioQualityKey];
_audioRecorder=[[AVAudioRecorder alloc]initWithURL:[NSURL fileURLWithPath:path] settings:dict error:nil];
}
return _audioRecorder;
}
-(AVAudioPlayer*)audioPlayer{
if (!_audioPlayer) {
NSString *path=[NSHomeDirectory() stringByAppendingString:@"Documents/record"];
_audioPlayer=[[AVAudioPlayer alloc]initWithContentsOfURL:[NSURL fileURLWithPath:path] error:nil];
[_audioPlayer prepareToPlay];
}
return _audioPlayer;
}
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (IBAction)caijiAction:(UIButton*)sender {
if(sender.selected) {
[self.audioRecorder stop];
}else{
[self.audioRecorder record];
}
sender.selected=!sender.selected;
}
- (IBAction)playAction:(UIButton*)sender {
if(sender.selected) {
[self.audioPlayerstop];
}else{
[self.audioPlayerplay];
}
sender.selected=!sender.selected;
}
@end
注:必要的参数设置
AVFormatIDKey 录制音频的格式。
kAudioFormatLinearPCM: lpcm格式
kAudioFormatAC3: ac-3格式
kAudioFormatMPEG4AAC: aac格式
kAudioFormatMPEG4CELP: celp格式
kAudioFormatMPEG4HVXC: hvxc格式
kAudioFormatMPEG4Layer1: mp1格式
kAudioFormatMPEG4Layer2: mp2 格式
kAudioFormatMPEG4Layer3: mp3 格式
kAudioFormatTimeCode: time格式
kAudioFormatMIDIStream: midi格式
kAudioFormatAppleLossless:alac格式
AVSampleRateKey 录制音频时的采用视频
AVNumberOfChannelsKey 录制音频时的通道数量
AVEncoderAudioQualityKey 录制音频的质量
AVAudioQualityMin
AVAudioQualityLow
AVAudioQualityMedium
AVAudioQualityHigh
AVAudioQualityMax
网友评论