IOS中录音后再播放声音太小问题解决
1、AVAudioSessionCategory说明
- 1.1 AVAudioSessionCategoryAmbient 或 kAudioSessionCategory_AmbientSound
- 用于非以语音为主的应用,使用这个category的应用会随着静音键和屏幕关闭而静音。
- 并且不会中止其它应用播放声音,可以和其它自带应用如iPod,safari等同时播放声音。
- 注意:该Category无法在后台播放声音
- 1.2 AVAudioSessionCategorySoloAmbient 或 kAudioSessionCategory_SoloAmbientSound
- 类似于AVAudioSessionCategoryAmbient 不同之处在于它会中止其它应用播放声音。
- 这个category为默认category。该Category无法在后台播放声音
- 1.3 AVAudioSessionCategoryPlayback 或 kAudioSessionCategory_MediaPlayback
- 用于以语音为主的应用,使用这个category的应用不会随着静音键和屏幕关闭而静音。
- 可在后台播放声音
- 1.4 AVAudioSessionCategoryRecord 或 kAudioSessionCategory_RecordAudio
- 用于需要录音的应用,设置该category后,除了来电铃声,闹钟或日历提醒之外的其它系统声音都不会被播放。
- 该Category只提供单纯录音功能。
- 1.5 AVAudioSessionCategoryPlayAndRecord 或 kAudioSessionCategory_PlayAndRecord
- 用于既需要播放声音又需要录音的应用,语音聊天应用(如微信)应该使用这个category。
- 该Category提供录音和播放功能。如果你的应用需要用到iPhone上的听筒,该category是你唯一的选择,
- 在该Category下声音的默认出口为听筒(在没有外接设备的情况下)。
1.6 注意
- 并不是一个应用只能使用一个category,程序应该根据实际需要来切换设置不同的category,
- 举个例子,录音的时候,需要设置为AVAudioSessionCategoryRecord,
- 当录音结束时,应根据程序需要更改category为AVAudioSessionCategoryAmbient,
- AVAudioSessionCategorySoloAmbient或AVAudioSessionCategoryPlayback中的一种。
2、录音后再播放声音太小问题解决
2.1 方法一:录音结束恢复播放模式
/********************** 开始录音 **********************************/
- (void)onRecordSoundStart:(UIButton *)sender {
if (![self canRecord]) {
[[[UIAlertView alloc] initWithTitle:nil message:[NSString stringWithFormat:@"应用需要访问您的麦克风。\n请启用麦克风-设置/隐私/麦克风"]
delegate:nil
cancelButtonTitle:@"好"
otherButtonTitles:nil] show];
return;
}
[self initRecordSession];
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:AVAudioQualityHigh], AVEncoderAudioQualityKey, //录音质量
nil]; NSURL *strURL = [NSURL fileURLWithPath:[self GetRecordSoundFileName:sender.tag]];
_recorder = [[AVAudioRecorder alloc] initWithURL:strURL settings:settings error:nil];
_recorder.meteringEnabled = YES;
_recorder.delegate = self;
[_recorder prepareToRecord];
[_recorder record];
_timerRec = [NSTimer scheduledTimerWithTimeInterval:0 target:self selector:@selector(detectionVoice) userInfo:nil repeats:YES];
} /********************** 结束录音 **********************************/
- (void)onRecordSoundStop:(UIButton *)sender { AVAudioSession *session = [AVAudioSession sharedInstance];
[session setCategory:AVAudioSessionCategoryPlayback error:nil]; //此处需要恢复设置回放标志,否则会导致其它播放声音也会变小
[session setActive:YES error:nil];
[_timerRec invalidate];
if (_recorder.currentTime > 1) {
[_recorder stop];
PlayNodeData *model = _dataOfVideoArrary[sender.tag];
model.hasSound = YES;
[_btnPlay setImage:[UIImage imageNamed:@"simulate_image_play1"] forState:UIControlStateNormal];
}
} /********************** 录音器是否可用检查 **********************************/
- (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;
} /********************** 初始化录音器 **********************************/
- (void)initRecordSession {
AVAudioSession *session = [AVAudioSession sharedInstance];
[session setCategory:AVAudioSessionCategoryPlayAndRecord error:nil];
[session setActive:YES error:nil];
} /********************** 录音中音量更新 **********************************/
- (void)detectionVoice {
return;
[_recorder updateMeters];//刷新音量数据
//获取音量的平均值 [recorder averagePowerForChannel:0];
//音量的最大值 [recorder peakPowerForChannel:0];
double lowPassResults = pow(10, (0.05 * [_recorder peakPowerForChannel:0]));
NSLog(@"%lf",lowPassResults);
//最大50 0
//图片 小-》大
if (0 < lowPassResults <= 0.06) {
;
}
else if (0.06 < lowPassResults <= 0.13) {
;
}
else if (0.13 < lowPassResults <= 0.20) {
;
}
else if (0.20 < lowPassResults <= 0.27) {
;
}
}
2.2 方法二:设置听筒模式
[audioSession setCategory:AVAudioSessionCategoryPlayAndRecord withOptions:AVAudioSessionCategoryOptionDefaultToSpeaker error:&error];
IOS中录音后再播放声音太小问题解决的更多相关文章
- iOS AVAudioPlayer播放音频时声音太小
iOS AVAudioPlayer播放音频时声音太小 //引入AVFoundation类库,设置播放模式就可以了 do { try AVAudioSession.sharedInstance().ov ...
- eclipse下java中凝视字体太小和xml中中文字体太小问题解决方法
我们在win7下进行android应用开发.须要搭建对应的开发环境.如今普遍基本上都是eclipse+adt+sdk,在本人搭建完环境后,发现eclipse下.java中的凝视和xml中的中文字体变得 ...
- iOS中书写代码规范35条小建议
1.精简代码, 返回最后一句的值,这个方法有一个优点,所有的变量都在代码块中,也就是只在代码块的区域中有效,这意味着可以减少对其他作用域的命名污染.但缺点是可读性比较差 NSURL *url = ({ ...
- iOS中转义后的html标签如何还原
最近用swift做一个公司的小项目,遇到一个问题,就是通过api获取的html文本中的标签都已经被转义了, <p class="MsoNormal" align=" ...
- ligerUI布局时,Center中的Tab高度太小问题解决
1.0 引用的js,css <link href="/Content/scripts/ligerUI/skins/Aqua/css/ligerui-all.css" rel= ...
- ORA-06502: PL/SQL: 数字或值错误 : 字符串缓冲区太小解决办法
1.今天写的存储过程在执行过程中,报如下错误. exec PRO_T_008pro_update_add_delete(17,1,1,1,1,45.0,54.0,45.0,45.0,45.0,54.0 ...
- iOS录音后播放声音变小的解决方法
目前需求是录音之后再播放出来.经常会出现播放声音变很小的情况. 解决方法: if (recorder.recording){ [recorder stop]; } [[AVAudioSession s ...
- iOS 多个异步网络请求全部返回后再执行具体逻辑的方法
对于dispatch多个异步操作后的同步方法,以前只看过dispatch_group_async,看看这个方法的说明: * @discussion * Submits a block to a dis ...
- IOS 实现 AAC格式 录音 录音后自动播放
废话不说了 不知道aac可以百度一下 下面直接上代码,一个h文件 一个m文件 搞定! #import <AVFoundation/AVFoundation.h> #import <U ...
随机推荐
- 布同:使用ghost备份或者还原的往事
我大学的时候经常折腾电脑,安装了不少莫名其妙的东西.当时对各种小软件特别感兴趣,本着毕业后可以做客户端开发的初衷去做事情.不过很多小软件会恶意安装各种东西,修改注册表,时间一长就会导致C盘很臃肿,必须 ...
- nginx错误
在开发的时候遇到nginx错误 网上找了半天也没有找到解决方案: 先查看了一下nginx错误日志 cat /usr/local/nginx/logs/error.log 然后发现看不太懂 那么只能重启 ...
- JAX-WS注解
JAX-WS注解: javax.jws.WebService @WebService应用于类或者接口上面,该类便是一个对外访问WebService,默认情况里面所有的public方法都是可以对外提供访 ...
- Doubango简介-sip
Welcome Android http://code.google.com/p/imsdroid/ http://code.google.com/p/imsdroid/source/browse/# ...
- 【转】Inter-eNB S1 切换
本博客介绍Inter-eNB的S1切换的流程 当eNB收到测量报告,或是因为内部负荷分担等原因,触发了切换判决,进行eNB间小区间通过S1口的切换. 源eNB通过S1接口的 HANDOVER REQU ...
- JS:Window
ylbtech-JS:Window 1.返回顶部 1.happy.js ; (function () { var happyUi = { initHappy: function (type) { ut ...
- Mysql 5.6 MHA (gtid) on Kylin
mha on Kylinip hostname repl role mha role192.168.19.69 mysql1 master node192.168.19.73 mysql2 slave ...
- jdk中那些常见的类不能被继承的
对于java中的类,如果是使用final修饰的话,那么这个类就不能够被继承,因为jdk的开发者认为,有一些最基本的类没要必要对开发者开放,如果用户 继承时操作有误,很可能引入很多问题.为了防止用户对基 ...
- 2015.1.15 利用航线id取所有点的函数创建视图
1.根据航路id取所有航路点的函数 create or replace function alinepnts(alid in number) return tab_airline_pnt is --返 ...
- Oracle 10g RAC全库flashback
因业务原因,今天需要做一次全库flashback.以下是操作全过程: 1.确认主库是否能flashback到需要的时间点 在节点1上执行: SQL> alter session set nls_ ...