标签:
废话不说了 不知道aac可以百度一下 下面直接上代码,一个h文件 一个m文件 搞定!
#import <AVFoundation/AVFoundation.h> #import <UIKit/UIKit.h> @interface AudioRecord : NSObject<AVAudioRecorderDelegate, AVAudioPlayerDelegate> /** * 获取单例对象 * * @return 返回数据库对象 */ +(AudioRecord *)shareAudioRecord; /** * 将要录音 * * @return <#return value description#> */ - (BOOL)canRecord; /** * 停止录音 */ - (void)stopRecord; /** * 开始录音 */ - (void)onStatrRecord; /** * 初始化音频检查 */ -(void)initRecordSession; /** * 初始化文件存储路径 * * @return <#return value description#> */ - (NSString *)audioRecordingPath; /** * 录音器 */ @property (nonatomic, retain) AVAudioRecorder *audioRecorder; /** * 录音播放器 */ @property (nonatomic, retain) AVAudioPlayer *audioPlayer; @end
下面是m文件
//
// AudioRecord.m
// audio
#import "AudioRecord.h"
@implementation AudioRecord : NSObject
+(AudioRecord *)shareAudioRecord{
static AudioRecord *sharedAccountManagerInstance = nil;
static dispatch_once_t predicate; dispatch_once(&predicate, ^{
sharedAccountManagerInstance = [[self alloc] init];
});
return sharedAccountManagerInstance;
}
/**
* 设置录制的音频文件的位置
*
* @return <#return value description#>
*/
- (NSString *)audioRecordingPath{
NSString *result = nil;
NSArray *folders = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsFolde = [folders objectAtIndex:0];
result = [documentsFolde stringByAppendingPathComponent:@"Recording.aac"];
return (result);
}
/**
* 在初始化AVAudioRecord实例之前,需要进行基本的录音设置
*
* @return <#return value description#>
*/
- (NSDictionary *)audioRecordingSettings{
NSDictionary *settings = [[NSDictionary alloc] initWithObjectsAndKeys:
[NSNumber numberWithFloat:44100.0],AVSampleRateKey , //采样率 8000/44100/96000
[NSNumber numberWithInt:kAudioFormatMPEG4AAC],AVFormatIDKey, //录音格式
[NSNumber numberWithInt:16],AVLinearPCMBitDepthKey, //线性采样位数 8、16、24、32
[NSNumber numberWithInt:2],AVNumberOfChannelsKey, //声道 1,2
[NSNumber numberWithInt:AVAudioQualityLow],AVEncoderAudioQualityKey, //录音质量
nil];
return (settings);
}
/**
* 停止音频的录制
*
* @param recorder <#recorder description#>
*/
- (void)stopRecordingOnAudioRecorder:(AVAudioRecorder *)recorder{
AVAudioSession *session = [AVAudioSession sharedInstance];
[session setCategory:AVAudioSessionCategoryPlayback error:nil]; //此处需要恢复设置回放标志,否则会导致其它播放声音也会变小
[session setActive:YES error:nil];
[recorder stop];
}
/**
* @param recorder <#recorder description#>
* @param flag <#flag description#>
*/
- (void)audioRecorderDidFinishRecording:(AVAudioRecorder *)recorder successfully:(BOOL)flag{
if (flag == YES) {
NSLog(@"录音完成!");
NSError *playbackError = nil;
NSError *readingError = nil;
NSData *fileData = [NSData dataWithContentsOfFile:[self audioRecordingPath] options:NSDataReadingMapped error:&readingError];
AVAudioPlayer *newPlayer = [[AVAudioPlayer alloc] initWithData:fileData
error:&playbackError];
self.audioPlayer = newPlayer;
if (self.audioPlayer != nil) {
self.audioPlayer.delegate = self;
if ([self.audioPlayer prepareToPlay] == YES &&
[self.audioPlayer play] == YES) {
NSLog(@"开始播放音频!");
} else {
NSLog(@"不能播放音频!");
}
}else {
NSLog(@"播放失败!");
}
} else {
NSLog(@"录音过程意外终止!");
}
self.audioRecorder = nil;
}
/**
* 初始化音频检查
*/
-(void)initRecordSession
{
AVAudioSession *session = [AVAudioSession sharedInstance];
[session setCategory:AVAudioSessionCategoryPlayAndRecord error:nil];
[session setActive:YES error:nil];
}
/**
* 开始录音
*/
- (void)onStatrRecord
{
/**
* 检查权限
*/
if (![self canRecord])
{
[[[UIAlertView alloc] initWithTitle:nil
message:[NSString stringWithFormat:@"应用需要访问您的麦克风。请启用麦克风!"]
delegate:nil
cancelButtonTitle:@"同意"
otherButtonTitles:nil] show];
return;
}
[self initRecordSession];
NSError *error = nil;
NSString *pathOfRecordingFile = [self audioRecordingPath];
NSURL *audioRecordingUrl = [NSURL fileURLWithPath:pathOfRecordingFile];
AVAudioRecorder *newRecorder = [[AVAudioRecorder alloc]
initWithURL:audioRecordingUrl
settings:[self audioRecordingSettings]
error:&error];
self.audioRecorder = newRecorder;
if (self.audioRecorder != nil) {
self.audioRecorder.delegate = self;
if([self.audioRecorder prepareToRecord] == NO){
return;
}
if ([self.audioRecorder record] == YES) {
NSLog(@"录音开始!");
[self performSelector:@selector(stopRecordingOnAudioRecorder:)
withObject:self.audioRecorder
afterDelay:10.0f];
} else {
NSLog(@"录音失败!");
self.audioRecorder =nil;
}
} else {
NSLog(@"auioRecorder实例录音器失败!");
}
}
/**
* 停止录音
*/
- (void)stopRecord{
if (self.audioRecorder != nil) {
if ([self.audioRecorder isRecording] == YES) {
[self.audioRecorder stop];
}
self.audioRecorder = nil;
}
if (self.audioPlayer != nil) {
if ([self.audioPlayer isPlaying] == YES) {
[self.audioPlayer stop];
}
self.audioPlayer = nil;
}
}
/**
* 将要录音
*
* @return <#return value description#>
*/
- (BOOL)canRecord
{
__block BOOL bCanRecord = YES;
if ([[[UIDevice currentDevice] systemVersion] compare:@"7.0"] != NSOrderedAscending)
{
AVAudioSession *audioSession = [AVAudioSession sharedInstance];
if ([audioSession respondsToSelector:@selector(requestRecordPermission:)]) {
[audioSession performSelector:@selector(requestRecordPermission:) withObject:^(BOOL granted) {
if (granted) {
bCanRecord = YES;
} else {
bCanRecord = NO;
}
}];
}
}
return bCanRecord;
}
@end
转载请注明:http://www.cnblogs.com/wangmars/ 以上也综合网上大牛的智慧
感谢http://www.cnblogs.com/hanjun/archive/2012/10/30/2747159.html顺便也解决了 录音后播放声音小的问题。
标签:
原文地址:http://www.cnblogs.com/wangmars/p/5045570.html