#import "ViewController.h"
#import <AVFoundation/AVFoundation.h>
@interface ViewController ()<AVAudioPlayerDelegate>
@property (nonatomic, strong) NSURL *recordedFile;//store path
@property (nonatomic, strong) AVAudioPlayer *player;//play
@property (nonatomic, strong) AVAudioRecorder *recorder;
@property (nonatomic, strong) NSString *recoderName;
@property (weak, nonatomic) IBOutlet UIButton *soundBtn;
@property (nonatomic, strong) NSMutableDictionary *paramters;
@end
@implementation ViewController
- (NSMutableDictionary *)paramters
{
if(!_paramters)
{
self.paramters = [NSMutableDictionary new];
//设置录音格式
[self.paramters setValue:[NSNumber numberWithInt:kAudioFormatMPEG4AAC] forKey:AVFormatIDKey];
//设置录音采样率
[self.paramters setValue:[NSNumber numberWithFloat:8000] forKey:AVSampleRateKey];
//录音的质量
[self.paramters setValue:[NSNumber numberWithInt:AVAudioQualityHigh] forKey:AVEncoderAudioQualityKey];
//线性采样位数 8、16、24、32
[self.paramters setValue:[NSNumber numberWithInt:16] forKey:AVLinearPCMBitDepthKey];
//录音通道数 1 或 2
[self.paramters setValue:[NSNumber numberWithInt:1] forKey:AVNumberOfChannelsKey];
}
return _paramters;
}
- (void)viewDidLoad {
[super viewDidLoad];
[self.soundBtn addTarget:self action:@selector(recordingSound) forControlEvents:UIControlEventTouchDown];
[self.soundBtn addTarget:self action:@selector(endSound) forControlEvents:UIControlEventTouchUpInside];
}
- (void)recordingSound
{
[self setRecordPath];
self.recorder = [[AVAudioRecorder alloc] initWithURL:self.recordedFile settings:self.paramters error:nil];
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryRecord error:nil];
//缓冲录音
[self.recorder prepareToRecord];
//开始录音
[self.recorder record];
//反复录音需要设置为空
self.player = nil;
}
- (void)endSound
{
[self.recorder stop];
self.recorder = nil;
}
- (void)setRecordPath
{
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
NSString *dateStr = [formatter stringFromDate:[NSDate date]];
NSString *path = [[NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, true) lastObject] stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.aac", dateStr]];
self.recoderName = [NSString stringWithFormat:@"%@.aac", dateStr];
self.recordedFile = [NSURL fileURLWithPath:path];
NSLog(@"%@", path);
}
- (IBAction)play:(UIButton *)sender
{
//初始化播放器的时候如下设置
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];
NSError *playerError;
self.player = [[AVAudioPlayer alloc] initWithContentsOfURL:self.recordedFile error:&playerError];
self.player.delegate = self;
//开始播放
[self.player play];
}
-(void)sensorStateChange:(NSNotificationCenter *)notification;
{
//如果此时手机靠近面部放在耳朵旁,那么声音将通过听筒输出,并将屏幕变暗(省电啊)
if ([[UIDevice currentDevice] proximityState] == YES)
{
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayAndRecord error:nil];
}
else
{
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:nil];
}
}
@end