录制:
1.设置全局属性
NSURL *recordedFile;//存放路径
AVAudioPlayer *player;//播放
AVAudioRecorder *recorder;//录制
NSString *recoderName;//文件名
2.设置存储路径
NSDateFormatter *formater = [[NSDateFormatter alloc] init];
[formater setDateFormat:@"yyyy-MM-dd-HH:mm:ss"];
NSString * dateStr = [formater stringFromDate:[NSDate date]];
NSString * path = [[NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, true) lastObject] stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.aac",dateStr]];
recoderName = [NSString stringWithFormat:@"%@.aac",dateStr];
recordedFile = [NSURL fileURLWithPath:path];
3.设置音频会话分类
AVAudioSession *session = [AVAudioSession sharedInstance];
NSError *sessionError;
[session setCategory:AVAudioSessionCategoryPlayAndRecord error:&sessionError];
if(session == nil)
{
}
else
{
[session setActive:YES error:nil];
}
4.开始录音
NSMutableDictionary *recordSetting = [[NSMutableDictionary alloc]init];
//设置录音格式
[recordSetting setValue:[NSNumber numberWithInt:kAudioFormatMPEG4AAC] forKey:AVFormatIDKey];
//设置录音采样率
[recordSetting setValue:[NSNumber numberWithFloat:] forKey:AVSampleRateKey];
//录音的质量
[recordSetting setValue:[NSNumber numberWithInt:AVAudioQualityHigh] forKey:AVEncoderAudioQualityKey];
//线性采样位数 8、16、24、32
[recordSetting setValue:[NSNumber numberWithInt:] forKey:AVLinearPCMBitDepthKey];
//录音通道数 1 或 2
[recordSetting setValue:[NSNumber numberWithInt:] forKey:AVNumberOfChannelsKey];
recorder = [[AVAudioRecorder alloc] initWithURL:recordedFile settings:recordSetting error:nil];
//缓冲录音
[recorder prepareToRecord];
//开始录音
[recorder record];
//如果是反复录制需要置空
player = nil;
5.停止录音
//停止录制
[recorder stop];
recorder = nil;
NSError *playerError;
player = [[AVAudioPlayer alloc] initWithContentsOfURL:recordedFile error:&playerError];
播放
1.播放录音
//初始化播放器的时候如下设置
UInt32 sessionCategory = kAudioSessionCategory_MediaPlayback;
AudioSessionSetProperty(kAudioSessionProperty_AudioCategory,
sizeof(sessionCategory),
&sessionCategory);
UInt32 audioRouteOverride = kAudioSessionOverrideAudioRoute_Speaker;
AudioSessionSetProperty (kAudioSessionProperty_OverrideAudioRoute,
sizeof (audioRouteOverride),
&audioRouteOverride);
AVAudioSession *audioSession = [AVAudioSession sharedInstance];
//默认情况下扬声器播放
[audioSession setCategory:AVAudioSessionCategoryPlayback error:nil];
[audioSession setActive:YES error:nil];
//建议播放之前设置yes,播放结束设置no,这个功能是开启红外感应
[[UIDevice currentDevice] setProximityMonitoringEnabled:YES];
//添加监听
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(sensorStateChange:)
name:@"UIDeviceProximityStateDidChangeNotification"
object:nil];
//开始播放
[_myPlayer play];
2.处理监听事件
-(void)sensorStateChange:(NSNotificationCenter *)notification;
{
//如果此时手机靠近面部放在耳朵旁,那么声音将通过听筒输出,并将屏幕变暗(省电啊)
if ([[UIDevice currentDevice] proximityState] == YES)
{
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayAndRecord error:nil];
}
else
{
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:nil];
}
}
3.暂停播放
//关闭红外感应
[[UIDevice currentDevice] setProximityMonitoringEnabled:NO];
//暂停播放
[_myPlayer stop];