如图所示 首先拖拽 3个button控件与两个lable控件,并写上title,然后将其关联
接下来 进入代码的阶段
首先 要导入AVFoundation框架,并设置代理 以及创建一些全局变量方便以后的调用
#import "ViewController.h"
#import <AVFoundation/AVFoundation.h>
@interface ViewController ()<AVAudioRecorderDelegate>
{
AVAudioRecorder *_audioRecoder;//音频录音机
AVAudioPlayer *_avplayer;//音频播放器
NSTimer *_timer;//定时器
int _count;//保存数量
BOOL _isSwitch;//是否开关
}
此处是关联过来的方法以及文件的存储路径
// 开始录制
@property (weak, nonatomic) IBOutlet UIButton *btnStart;
// 显示时间
@property (weak, nonatomic) IBOutlet UILabel *showTime;
// 回放属性
@property (weak, nonatomic) IBOutlet UIButton *btnPlayBack;
// 文件路径
@property(nonatomic,copy)NSString *documentsPath;
// 开始录制
- (IBAction)startRecording:(id)sender;
// 停止录制
- (IBAction)stopRecording:(id)sender;
// 回放录音
- (IBAction)playBackRecording:(id)sender;
注意在 拖拽按钮的时候 我们需要将其Connection处改成Action在进行关联,这样的操作可以方便我们在按钮的方法里直接书写代码
在viewDidLoad中进行以下的书写
- (void)viewDidLoad
{
[super viewDidLoad];
_isSwitch = YES;
}
开始按钮
// 开始录制
- (IBAction)startRecording:(id)sender
{
// 判断录音控制器是否为空或者正在录制
if (_audioRecoder == nil && _audioRecoder.isRecording)
{
// 设置控制器停止录制
[_audioRecoder stop];
// 设置按钮的标题为开始录制
[_btnStart setTitle:@"开始录制" forState:UIControlStateNormal];
[_timer invalidate];
_timer = nil;
}
else
{
_count = 0;
_timer = [NSTimer scheduledTimerWithTimeInterval:1.0f target:self selector:@selector(repeatShowTime:) userInfo:@"admin" repeats:YES];
#pragma mark 下面设置录音的参数和录音文件的保存路径等信息
//获取音频文件的保存路径
NSString *destinationStr = [[self documentsPath] stringByAppendingPathComponent:@"sound.wav"];
NSURL *destinationUrl = [NSURL fileURLWithPath:destinationStr];
//创建一个Dictionary,用于保存录制属性
NSMutableDictionary *recordSettings = [[NSMutableDictionary alloc] init];
//设置录制音频的格式
[recordSettings setObject:[NSNumber numberWithInt:kAudioFormatLinearPCM]forKey:AVFormatIDKey];
//设置录制音频的采样率
// [recordSettings setObject:[NSNumber numberWithFloat:@"1".floatValue] forKey:AVSampleRateKey];
//设置录制音频的通道数
[recordSettings setObject:[NSNumber numberWithInt:(_isSwitch =/* DISABLES CODE */ (YES) ?2:1)]forKey:AVNumberOfChannelsKey];
//设置录制音频采用高位优先的记录格式
[recordSettings setObject:[NSNumber numberWithBool:YES]forKey:AVLinearPCMIsBigEndianKey];
//设置采样信号采用浮点数
[recordSettings setObject:[NSNumber numberWithBool:YES]forKey:AVLinearPCMIsFloatKey];
NSError *recorderSetupError =nil;
#pragma mark 到这里开始实例化录音对象
//初始化AVAudioRecorder
_audioRecoder = [[AVAudioRecorder alloc] initWithURL:destinationUrl settings:recordSettings error:&recorderSetupError];
_audioRecoder.delegate =self;
[_audioRecoder record];
//设置单个按钮的状态为录音
[_btnStart setTitle:@"正在录音"forState:UIControlStateNormal];
}
}
停止按钮
// 停止录制
- (IBAction)stopRecording:(id)sender
{
[_audioRecoder stop];
[_btnStart setTitle:@"开始录制" forState:UIControlStateNormal];
// 设置计时器为初始值
if (_timer)
{
[_timer invalidate];
_timer =nil;
}
_count = 0;
_showTime.text = @"00:00";
}
回放按钮
// 回放录音
- (IBAction)playBackRecording:(id)sender
{
// 获取音频文件的保存路径
NSString *destinationString = [[self documentsPath] stringByAppendingPathComponent:@"sound.wav"];
NSURL *url = [NSURL fileURLWithPath:destinationString];
//创建AVAudioPlayer对象
_avplayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:nil];
//开始播放
[_avplayer play];
_btnPlayBack.backgroundColor=[UIColor greenColor];
}
获取文件的路径
//获取Documents目录路径
-(NSString *)documentsPath
{
if (!_documentsPath)
{
NSArray *searchPath =NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES);
_documentsPath = searchPath[0];
}
return _documentsPath;
}
录音中的代理方法
#pragma mark- 录制音频的代理方法
- (void)audioRecorderBeginInterruption:(AVAudioRecorder *)recorder
{
NSLog(@"---->被中断!");
}
- (void)audioRecorderDidFinishRecording:(AVAudioRecorder *)aRecorder successfully:(BOOL)flag
{
if(flag)
{
NSLog(@"---->录制完成!!");
}
}
- (void)repeatShowTime:(NSTimer *)tempTimer
{
_count++;
//设置在文本框上显示时间;
_showTime.text = [NSString stringWithFormat:@"%02d:%02d",_count/60,_count%60];
}
- (void)dealloc
{ //销毁NSTimer</span>
if (_timer)
{
[_timer invalidate];
_timer = nil;
}
}