废话不说了 不知道aac可以百度一下 下面直接上代码,一个h文件 一个m文件 搞定!

#import <AVFoundation/AVFoundation.h>
#import <UIKit/UIKit.h> @interface AudioRecord : NSObject<AVAudioRecorderDelegate, AVAudioPlayerDelegate> /**
* 获取单例对象
*/
+(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:];
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:],AVLinearPCMBitDepthKey, //线性采样位数 8、16、24、32 [NSNumber numberWithInt:],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顺便也解决了 录音后播放声音小的问题。

 
 
 

IOS 实现 AAC格式 录音 录音后自动播放的更多相关文章

  1. egret 篇——关于ios环境下微信浏览器的音频自动播放问题

    前段时间公司突然想用egret(白鹭引擎)做一个金币游戏,大半个月边看文档边写吭哧吭哧也总算是弄完了.期间遇到一个问题,那就是ios环境下微信浏览器的音频自动播放问题. 个人感觉吧,egret自己封装 ...

  2. JavaScript一个页面中有多个audio标签,其中一个播放结束后自动播放下一个,audio连续播放

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  3. 解决ios下的微信页面背景音乐无法自动播放问题

    在做各种html5场景页面的时候,插入背景音乐是一个很普遍的需求,我们都知道,ios下的safari是无法自动播放音乐的,以至于现在行程一种认知,ios是没有办法自动播放媒体资源的,这个认知其实是错误 ...

  4. 在ios中微信video和audio无法自动播放解决方案

    WeixinJSBridgeReady页面初始化的时候会执行 document.addEventListener("WeixinJSBridgeReady", function ( ...

  5. iOS 疑难杂症— — 收到推送显示后自动消失的问题

    声明 欢迎转载,但请保留文章原始出处:) 博客园:http://www.cnblogs.com 农民伯伯: http://over140.cnblogs.com 问题 正在支持 Remote Noti ...

  6. ios上视频与音乐合成后出现播放兼容问题的解决方法

    近期EasyDarwin开源流媒体团队EasyVideoRecorder小组同学Carl在支持一款短视频应用上线时,遇到一个问题:我们在IOS上合成"图片+音乐"成为视频之后,在P ...

  7. 解决audio 在部分移动端浏览器不能自动播放(目前包括ios、微博)

    问题描述:项目需要在页面加载完成后自动播放音乐,但在ios中却无法自动播放,需要用户主动触发 解决办法: $('html').one('touchstart',function(){ document ...

  8. iOS背景音乐不自动播放

    iOS 内置浏览器safari不允许自动播放音乐.我们需要通过WeixinJSBridgeReady()函数实现自动触发 document.addEventListener("WeixinJ ...

  9. video 安卓ios系统 浏览器 全屏播放以及自动播放的问题

    ios自动播放 <body onload="load()"> <div class="result_box"> <div clas ...

随机推荐

  1. SNF开发平台WinForm之十四-站内发送系统信息-SNF快速开发平台3.3-Spring.Net.Framework

    1运行效果: 2开发实现: .组装站内信息发送实体对象. SNFService SNFService = new SNFService(); if (this.ucUser.SelectedIds ! ...

  2. 测试GeoGebra博客

    已知函数 \(\textit{f}(\textit{x})=2\textit{m}\ln\textit{x}-\textit{x}^2\), \(\textit{g}(\textit{x})=\tex ...

  3. 重写js alert

    Window.prototype.alert = function(){ //创建一个大盒子 var box = document.createElement("div"); // ...

  4. 玩转spring boot——properties配置

    前言 在以往的java开发中,程序员最怕大量的配置,是因为配置一多就不好统一管理,经常出现找不到配置的情况.而项目中,从开发测试环境到生产环境,往往需要切换不同的配置,如测试数据库连接换成生产数据库连 ...

  5. 红黑树(五)之 Java的实现

    概要 前面分别介绍红黑树的理论知识.红黑树的C语言和C++的实现.本章介绍红黑树的Java实现,若读者对红黑树的理论知识不熟悉,建立先学习红黑树的理论知识,再来学习本章.还是那句老话,红黑树的C/C+ ...

  6. Office Web Apps Server 概述

    Office Web Apps Server 是新的 Office 服务器产品,它提供 Word.PowerPoint.Excel 和 OneNote 的基于浏览器的版本.单个 Office Web ...

  7. iOS-修改Status Bar

    一.在info.plist文件中添加一行不但要在 Status bar is initially hidden一行,选择为 YES. 二.在didFinishLaunchingWithOptions方 ...

  8. IT人的自我导向型学习:学习的4个层次

    谈起软件开发一定会想到用什么技术.采用什么框架,然而在盛行的敏捷之下,人的问题逐渐凸显出来.不少企业请人来培训敏捷开发技术,却发现并不能真正运用起来,其中一个主要原因就是大家还没有很好的学习能力.没有 ...

  9. 微软必应词典客户端的案例分析——个人Week3作业

    第一部分 调研,评测 Bug探索 Bug No1.高亮语义匹配错位 环境: windows8,使用必应词典版本PC版:3.5.0 重现步骤: 1. 搜索"funny face"这一 ...

  10. ThroughRain第一次冲刺个人总结

    团队名:ThroughRain 项目确定:<餐厅到店点餐系统> 项目背景:本次项目是专门为餐厅开发的一套订餐系统.大家有没有发现在节假日去餐厅吃饭会超级麻烦,人很多, 热门的餐厅基本没有座 ...