如圖所示 首先拖拽 3個(gè)button控件與兩個(gè)lable控件,并寫上title,然后將其關(guān)聯(lián)
接下來 進(jìn)入代碼的階段
首先 要導(dǎo)入AVFoundation框架,并設(shè)置代理 以及創(chuàng)建一些全局變量方便以后的調(diào)用
#import "ViewController.h"
#import <AVFoundation/AVFoundation.h>
@interface ViewController ()<AVAudioRecorderDelegate>
{
AVAudioRecorder *_audioRecoder;//音頻錄音機(jī)
AVAudioPlayer *_avplayer;//音頻播放器
NSTimer *_timer;//定時(shí)器
int _count;//保存數(shù)量
BOOL _isSwitch;//是否開關(guān)
}
此處是關(guān)聯(lián)過來的方法以及文件的存儲(chǔ)路徑
// 開始錄制
@property (weak, nonatomic) IBOutlet UIButton *btnStart;
// 顯示時(shí)間
@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;
注意在 拖拽按鈕的時(shí)候 我們需要將其Connection處改成Action在進(jìn)行關(guān)聯(lián),這樣的操作可以方便我們?cè)诎粹o的方法里直接書寫代碼
在viewDidLoad中進(jìn)行以下的書寫
- (void)viewDidLoad
{
[super viewDidLoad];
_isSwitch = YES;
}
開始按鈕
// 開始錄制
- (IBAction)startRecording:(id)sender
{
// 判斷錄音控制器是否為空或者正在錄制
if (_audioRecoder == nil && _audioRecoder.isRecording)
{
// 設(shè)置控制器停止錄制
[_audioRecoder stop];
// 設(shè)置按鈕的標(biāo)題為開始錄制
[_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 下面設(shè)置錄音的參數(shù)和錄音文件的保存路徑等信息
//獲取音頻文件的保存路徑
NSString *destinationStr = [[self documentsPath] stringByAppendingPathComponent:@"sound.wav"];
NSURL *destinationUrl = [NSURL fileURLWithPath:destinationStr];
//創(chuàng)建一個(gè)Dictionary顽悼,用于保存錄制屬性
NSMutableDictionary *recordSettings = [[NSMutableDictionary alloc] init];
//設(shè)置錄制音頻的格式
[recordSettings setObject:[NSNumber numberWithInt:kAudioFormatLinearPCM]forKey:AVFormatIDKey];
//設(shè)置錄制音頻的采樣率
// [recordSettings setObject:[NSNumber numberWithFloat:@"1".floatValue] forKey:AVSampleRateKey];
//設(shè)置錄制音頻的通道數(shù)
[recordSettings setObject:[NSNumber numberWithInt:(_isSwitch =/* DISABLES CODE */ (YES) ?2:1)]forKey:AVNumberOfChannelsKey];
//設(shè)置錄制音頻采用高位優(yōu)先的記錄格式
[recordSettings setObject:[NSNumber numberWithBool:YES]forKey:AVLinearPCMIsBigEndianKey];
//設(shè)置采樣信號(hào)采用浮點(diǎn)數(shù)
[recordSettings setObject:[NSNumber numberWithBool:YES]forKey:AVLinearPCMIsFloatKey];
NSError *recorderSetupError =nil;
#pragma mark 到這里開始實(shí)例化錄音對(duì)象
//初始化AVAudioRecorder
_audioRecoder = [[AVAudioRecorder alloc] initWithURL:destinationUrl settings:recordSettings error:&recorderSetupError];
_audioRecoder.delegate =self;
[_audioRecoder record];
//設(shè)置單個(gè)按鈕的狀態(tài)為錄音
[_btnStart setTitle:@"正在錄音"forState:UIControlStateNormal];
}
}
停止按鈕
// 停止錄制
- (IBAction)stopRecording:(id)sender
{
[_audioRecoder stop];
[_btnStart setTitle:@"開始錄制" forState:UIControlStateNormal];
// 設(shè)置計(jì)時(shí)器為初始值
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];
//創(chuàng)建AVAudioPlayer對(duì)象
_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(@"---->錄制完成Vā松逊!");
}
}
- (void)repeatShowTime:(NSTimer *)tempTimer
{
_count++;
//設(shè)置在文本框上顯示時(shí)間纪蜒;
_showTime.text = [NSString stringWithFormat:@"%02d:%02d",_count/60,_count%60];
}
- (void)dealloc
{ //銷毀NSTimer</span>
if (_timer)
{
[_timer invalidate];
_timer = nil;
}
}