IOS开发之录音与播放功能
基于AVFoundation.framework的录音与播放
首先添加以来库AVFoundation.framework
在项目中到导入<AVFoundation/AVFoundation.h>
具体代码如下:
#import "ViewController.h"
#import <AVFoundation/AVFoundation.h>
@interface ViewController ()<AVAudioRecorderDelegate>
{
NSURL* recordUrl;
AVAudioPlayer *audioPlayer;
AVAudioRecorder *audioRecorder;
AVAudioSession * audioSession;
}
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
[self setViewUI];
}
- (void)setViewUI
{
UIButton *recordButton = [[UIButton alloc] initWithFrame:CGRectMake(200,200, 100, 100)];
[recordButton setImage:[UIImage imageNamed:@"record_button"] forState:UIControlStateNormal];
[recordButton addTarget:self action:@selector(recordButtonClick) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:recordButton];
UIButton *playButton = [[UIButton alloc] initWithFrame:CGRectMake(200,400, 100, 100)];
[playButton setImage:[UIImage imageNamed:@"play_button"] forState:UIControlStateNormal];
[playButton addTarget:self action:@selector(playButtonClick) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:playButton];
//录音设置
NSMutableDictionary *recordSetting = [[NSMutableDictionary alloc] init];
//设置录音格式 AVFormatIDKey==kAudioFormatLinearPCM
[recordSetting setValue:[NSNumber numberWithInt:kAudioFormatLinearPCM] forKey:AVFormatIDKey];
//设置录音采样率(Hz) 如:AVSampleRateKey==8000/44100/96000(影响音频的质量), 采样率必须要设为11025才能使转化成mp3格式后不会失真
[recordSetting setValue:[NSNumber numberWithFloat:8000.0] forKey:AVSampleRateKey];
//录音通道数 1 或 2 ,要转换成mp3格式必须为双通道 如果录制wav格式需要把通道改成1
[recordSetting setValue:[NSNumber numberWithInt:2] forKey:AVNumberOfChannelsKey];
//线性采样位数 8、16、24、32
[recordSetting setValue:[NSNumber numberWithInt:16] forKey:AVLinearPCMBitDepthKey];
//录音的质量
[recordSetting setValue:[NSNumber numberWithInt:AVAudioQualityHigh] forKey:AVEncoderAudioQualityKey];
//存储录音文件
recordUrl = [NSURL URLWithString:[NSTemporaryDirectory() stringByAppendingString:@"selfRecord.caf"]];
NSLog(@"recordUrl===%@",recordUrl);
//初始化
audioRecorder = [[AVAudioRecorder alloc] initWithURL:recordUrl settings:recordSetting error:nil];
//开启音量检测
audioRecorder.meteringEnabled = YES;
audioRecorder.delegate = self;
}
- (void)recordButtonClick
{
if (![audioRecorder isRecording]) {
[audioSession setCategory:AVAudioSessionCategoryPlayAndRecord error:nil];//设置类别,表示该应用同时支持播放和录音
[audioSession setActive:YES error:nil];//启动音频会话管理,此时会阻断后台音乐的播放.
[audioRecorder prepareToRecord];
[audioRecorder peakPowerForChannel:0.0];
[audioRecorder record];
//此处录制五秒中录音 执行录音停止的方法
[self performSelector:@selector(stopRecord) withObject:nil afterDelay:5];
}
}
- (void)stopRecord
{
//录音停止
[audioRecorder stop];
//一定要在录音停止以后再关闭音频会话管理(否则会报错),此时会延续后台音乐播放
[audioSession setActive:NO error:nil];
}
- (void)playButtonClick
{
//播放录音文件
[audioSession setCategory:AVAudioSessionCategoryPlayback error:nil];
[audioSession setActive:YES error:nil];
if (recordUrl != nil){
audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:recordUrl error:nil];
}
[audioPlayer prepareToPlay];
audioPlayer.volume = 1;
[audioPlayer play];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}
@end