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];

最新文章

  1. Serial Port Programming using Win32 API(转载)
  2. java selenium (十二) 操作弹出窗口
  3. 关于Jquery中ajax介绍
  4. 怎么找到占用usb的模块,linux下Jlink连接失败
  5. [转]Spring的IOC原理[通俗解释一下]
  6. IOS 作业项目(4)步步完成 画图 程序(剧终)
  7. swift学习初步(四)-- 函数
  8. CSS居中的方法整合--水平居中
  9. Spring DM所提供的Bundle监听接口OsgiBundleApplicationContextListener
  10. IE浏览器-官网下载地址
  11. ps之雪碧图制作
  12. Git中一些远程库操作的细节
  13. apache基础
  14. Qt文档阅读笔记-QGraphicsItem::paint中QStyleOptionGraphicsItem *option的进一步认识
  15. DevExpress 折线图和柱状图的绘制与数据绑定
  16. vscode 好用插件
  17. Codeforces 839C Journey - 树形动态规划 - 数学期望
  18. Python 递归函数 详解
  19. 第二周 Word版面设计
  20. 《DSP using MATLAB》Problem 4.23

热门文章

  1. c++11之一: 基于范围的for循环
  2. svn Can&#39;t revert without reverting children 解决方案
  3. POJ 3728 The merchant(LCA+DP)
  4. 10-20C#基础---一维、二维数组&amp;&amp;冒泡排序
  5. docker 笔记 (7) 限制容器
  6. nested exception is java.net.UnknownHostException: mybatis.org异常处理
  7. DAY17-Django之model查询
  8. python爬虫(7)--Beautiful Soup的用法
  9. 关于jdk1.5之后的自定拆装箱
  10. gym - 101673I Twenty Four, Again (表达式树枚举)