#import "MainViewController.h"
#import <AVFoundation/AVFoundation.h>
@interface MainViewController () <AVAudioRecorderDelegate, AVAudioPlayerDelegate>
@property (strong, nonatomic) IBOutlet UIButton *recButton; // 录音按钮
@property (strong, nonatomic) IBOutlet UIButton *pauseButton; // 暂停按钮
@property (strong, nonatomic) IBOutlet UIButton *playButton; // 播放按钮
@property (strong, nonatomic) IBOutlet UIButton *stopButton; // 停止按钮
@property (strong, nonatomic) IBOutlet UILabel *stateLabel; // 状态标题
@property (strong, nonatomic) IBOutlet UIImageView *soundView; // 音频强度视图
@property (strong, nonatomic) IBOutlet UIProgressView *progressView; // 播放进度
@property (nonatomic, retain) AVAudioPlayer *player; // 播放器对象
@property (nonatomic, retain) AVAudioRecorder *recorder; // 录音机对象
@property (nonatomic, retain) NSTimer *timer; // 定时器
@end
@implementation MainViewController
#pragma mark - 控制器视图方法
- (void)viewDidLoad {
[super viewDidLoad];
[self unableClick:self.pauseButton];
[self unableClick:self.playButton];
[self unableClick:self.stopButton];
self.progressView.progress = 0;
// [self setAudioSession];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}
#pragma mark - 私有方法
/**
* 设置音频会话
*/
-(void)setAudioSession{
AVAudioSession *audioSession=[AVAudioSession sharedInstance];
//设置为播放和录音状态,以便可以在录制完之后播放录音
[audioSession setCategory:AVAudioSessionCategoryPlayAndRecord error:nil];
[audioSession setActive:YES error:nil];
}
/**
* 取得录音文件保存路径
*
* @return 录音文件路径
*/
-(NSURL *)getSavePath{
NSString *urlStr = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
urlStr = [urlStr stringByAppendingPathComponent:[NSString stringWithFormat: @"sound.%@", @"wav"]]; //文件名的设置;
// NSLog(@"录音文件地址 :%@",urlStr);
NSURL *url = [NSURL fileURLWithPath:urlStr];
return url;
}
/**
* 取得录音文件设置
*
* @return 录音设置
*/
-(NSDictionary *)getAudioSetting{
NSDictionary *recordSetting = [[NSDictionary alloc] initWithObjectsAndKeys:
[NSNumber numberWithFloat: 44100.0],AVSampleRateKey, //采样率
[NSNumber numberWithInt: kAudioFormatLinearPCM],AVFormatIDKey, // 录音格式 (仅支持苹果自己的格式)
[NSNumber numberWithInt:16],AVLinearPCMBitDepthKey,//采样位数默认 16
[NSNumber numberWithInt: 2], AVNumberOfChannelsKey,//通道的数目
[NSNumber numberWithBool:NO],AVLinearPCMIsBigEndianKey,//大端还是小端是内存的组织方式
[NSNumber numberWithBool:NO],AVLinearPCMIsFloatKey,nil];//采样信号是整数还是浮点数
return recordSetting;
/*
NSMutableDictionary *dicM = [NSMutableDictionary dictionary];
//设置录音格式 (仅支持苹果自己格式)
[dicM setObject:@(kAudioFormatLinearPCM) forKey:AVFormatIDKey];
//设置录音采样率,8000是电话采样率,对于一般录音已经够了 (扯淡,,,根本不够)
[dicM setObject:@(8000) forKey:AVSampleRateKey];
//设置通道数目,这里采用单声道
[dicM setObject:@(1) forKey:AVNumberOfChannelsKey];
//每个采样点位数,分为8、16、24、32
[dicM setObject:@(8) forKey:AVLinearPCMBitDepthKey];
//是否使用浮点数采样
[dicM setObject:@(YES) forKey:AVLinearPCMIsFloatKey];
//....其他设置等
return dicM;
*/
/*
NSDictionary *recordSetting = [[NSDictionary alloc] initWithObjectsAndKeys:
[NSNumber numberWithFloat: 8000],AVSampleRateKey, //采样率
[NSNumber numberWithInt: kAudioFormatLinearPCM],AVFormatIDKey,
[NSNumber numberWithInt:16],AVLinearPCMBitDepthKey,//采样位数 默认 16
[NSNumber numberWithInt: 1], AVNumberOfChannelsKey,//通道的数目
[NSNumber numberWithBool:NO],AVLinearPCMIsBigEndianKey,//大端还是小端 是内存的组织方式
[NSNumber numberWithBool:NO],AVLinearPCMIsFloatKey,//采样信号是整数还是浮点数
[NSNumber numberWithInt: AVAudioQualityMedium],AVEncoderAudioQualityKey,//音频编码质量
nil];
*/
}
/**
* 获得录音机对象
*
* @return 录音机对象
*/
-(AVAudioRecorder *)audioRecorder{
if (!self.recorder) {
//创建录音文件保存路径
NSURL *url = [self getSavePath];
NSLog(@"录音文件地址 :%@", url);
//创建录音格式设置
NSDictionary *setting = [self getAudioSetting];
//创建录音机
NSError *error=nil;
self.recorder= [[AVAudioRecorder alloc]initWithURL:url settings:setting error:&error];
self.recorder.delegate = self;
self.recorder.meteringEnabled = YES;//如果要监控声波则必须设置为YES
if (error) {
NSLog(@"创建录音机对象时发生错误,错误信息:%@",error.localizedDescription);
return nil;
}
}
return self.recorder;
}
/**
* 创建播放器
*
* @return 播放器
*/
-(AVAudioPlayer *)audioPlayer{
if (!self.player) {
NSURL *url=[self getSavePath];
NSError *error = nil;
self.player = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:&error];
if (error) {
NSLog(@"创建播放器过程中发生错误,错误信息:%@",error.localizedDescription);
return nil;
}
}
return self.player;
}
/**
* 录音声波监控定制器
*
* @return 定时器
*/
-(NSTimer *)timer{
if (!_timer) {
_timer=[NSTimer scheduledTimerWithTimeInterval:0.05f target:self selector:@selector(audioPowerChange) userInfo:nil repeats:YES];
}
return _timer;
}
/**
* 录音声波状态设置
*/
-(void)audioPowerChange{
float power = 0;
if ([self.player isPlaying]) {
[self.player updateMeters];
power = [self.player averagePowerForChannel:0];
// NSLog(@"%f, %f", self.player.currentTime, self.player.duration);
self.progressView.progress = self.player.currentTime / self.player.duration;
} else {
[self.recorder updateMeters];//更新测量值
power = [self.recorder averagePowerForChannel:0];//取得第一个通道的音频,注意音频强度范围时-160到0
}
CGFloat progress = (power + 100) * 3;
self.soundView.bounds = CGRectMake(0, 0, progress, progress);
}
#pragma mark - UI事件
/**
* 点击录音按钮
*
* @param sender 录音按钮
*/
- (IBAction)recordClick:(UIButton *)sender {
if (![self.recorder isRecording]) {
self.recorder = [self audioRecorder];
[self.recorder record];//首次使用应用时如果调用record方法会询问用户是否允许使用麦克风
NSLog(@"录音机设置:%@", self.recorder.settings);
self.timer.fireDate = [NSDate distantPast];
}
[self unableClick:self.recButton];
[self unableClick:self.playButton];
[self ableClick:self.pauseButton];
[self ableClick:self.stopButton];
self.stateLabel.text = @"录音中...";
self.stateLabel.textColor = [UIColor redColor];
}
/**
* 点击暂定按钮
*
* @param sender 暂停按钮
*/
- (IBAction)pauseClick:(UIButton *)sender {
if ([self.recorder isRecording]) {
[self.recorder pause];
self.timer.fireDate = [NSDate distantFuture];
[self ableClick:self.recButton];
} else if ([self.player isPlaying]) {
[self.player pause];
self.timer.fireDate = [NSDate distantFuture];
[self ableClick:self.playButton];
}
[self unableClick:self.pauseButton];
self.stateLabel.text = @"暂停中...";
self.stateLabel.textColor = [UIColor orangeColor];
}
/**
* 点击恢复按钮
* 恢复录音只需要再次调用record,AVAudioSession会帮助你记录上次录音位置并追加录音
*
* @param sender 恢复按钮
*/
- (IBAction)resumeClick:(UIButton *)sender {
[self recordClick:sender];
}
/**
* 点击停止按钮
*
* @param sender 停止按钮
*/
- (IBAction)stopClick:(UIButton *)sender {
if ([self.recorder isRecording]) {
[self.recorder stop];
self.timer.fireDate = [NSDate distantFuture];
self.soundView.bounds = CGRectMake(0, 0, 57, 57);
} else if ([self.player isPlaying]) {
[self.player stop];
self.player.currentTime = 0;
// NSLog(@"%f", self.player.currentTime);
self.timer.fireDate = [NSDate distantFuture];
self.soundView.bounds = CGRectMake(0, 0, 57, 57);
}
[self unableClick:self.stopButton];
[self ableClick:self.playButton];
[self unableClick:self.pauseButton];
[self ableClick:self.recButton];
self.stateLabel.text = @"等待中...";
self.stateLabel.textColor = [UIColor blackColor];
}
/**
* 点击播放按钮
*
* @param player 播放按钮
*/
- (IBAction)playClick:(id)sender {
if (self.player.currentTime != 0) {
[self.player play];
self.timer.fireDate = [NSDate distantPast];
} else {
if (!self.player) {
[self audioPlayer];
self.player.volume = 1;
}
NSLog(@"播放器:%@", self.player.settings);
self.player = [self.player initWithContentsOfURL:[self getSavePath] error:nil];
self.player.meteringEnabled = YES;
self.player.delegate = self;
[self.player play];
self.timer.fireDate = [NSDate distantPast];
}
[self unableClick:self.recButton];
[self unableClick:self.playButton];
[self ableClick:self.pauseButton];
[self ableClick:self.stopButton];
self.stateLabel.text = @"播放中...";
self.stateLabel.textColor = [UIColor colorWithRed:0 green:156 / 255.0 blue:1 alpha:1];
}
#pragma mark - 录音机代理方法
/**
* 录音完成,录音完成后播放录音
*
* @param recorder 录音机对象
* @param flag 是否成功
*/
-(void)audioRecorderDidFinishRecording:(AVAudioRecorder *)recorder successfully:(BOOL)flag
{
self.soundView.bounds = CGRectMake(0, 0, 57, 57);
NSLog(@"录音完成!");
}
#pragma mark - 播放器代理方法
/**
* 播放完成,播放完成后停止计时器
*
* @param player 播放器对象
* @param flag 是否成功
*/
- (void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)flag
{
self.soundView.bounds = CGRectMake(0, 0, 57, 57);
self.progressView.progress = 0;
NSLog(@"播放完毕");
self.timer.fireDate = [NSDate distantFuture];
[self unableClick:self.pauseButton];
[self unableClick:self.stopButton];
[self ableClick:self.recButton];
[self ableClick:self.playButton];
self.stateLabel.text = @"等待中...";
self.stateLabel.textColor = [UIColor blackColor];
}
#pragma mark - 按扭状态方法
/**
* 设置按钮可以被点击
*/
- (void)ableClick:(UIButton *)button
{
button.alpha = 1;
button.userInteractionEnabled = YES;
}
/**
* 设置按钮不可被点击
*/
- (void)unableClick:(UIButton *)button
{
button.alpha = 0.2;
button.userInteractionEnabled = NO;
}
@end
此篇博客是一网友给发的demo, 在这里谢谢他。