参考资料:

[简书地址](http://www.jianshu.com/users/23d0ae412e19/latest_articles)

#import <UIKit/UIKit.h>@interface RecordingHub : UIView@property (nonatomic,assign) float peakPower;// 按下
- (void)recordButtonTouchDown;// 移动到外面
- (void)recordButtonTouchUpOutside;@end
#import "RecordingHub.h"@interface RecordingHub()@property (nonatomic,strong) UIView *bgView;
@property (nonatomic,strong) UIImageView *recordVoiceView;
@property (nonatomic,strong) UILabel *textLabel; // 显示文字
@property (nonatomic,strong) NSTimer *timer;@end/***  Description: 麦克风弹出窗口*/
@implementation RecordingHub- (instancetype)initWithFrame:(CGRect)frame{self = [super initWithFrame:frame];if (self) {[self setUpUI];}return self;
}- (void)setUpUI{// 背景_bgView = [[UIView alloc]initWithFrame:self.bounds];_bgView.backgroundColor = [UIColor grayColor];_bgView.layer.cornerRadius = 6;_bgView.layer.masksToBounds= true;_bgView.alpha = 0.5;[self addSubview:_bgView];// 麦克风_recordVoiceView = [[UIImageView alloc]init];_recordVoiceView.frame = CGRectMake(10, 0, self.bounds.size.width - 20, self.bounds.size.height - 10);_recordVoiceView.image = [UIImage imageNamed:@"VoiceSearchFeedback001"];[self addSubview:_recordVoiceView];// 文字_textLabel = [[UILabel alloc]init];_textLabel.frame = CGRectMake(5,self.bounds.size.height - 30,self.bounds.size.width - 10,25);_textLabel.textAlignment = NSTextAlignmentCenter;_textLabel.textColor = [UIColor whiteColor];_textLabel.backgroundColor = [UIColor clearColor];_textLabel.text = @"请说话.....";_textLabel.font = [UIFont systemFontOfSize:12];_textLabel.layer.cornerRadius = 6;_textLabel.layer.masksToBounds = YES;_textLabel.layer.borderColor = [[UIColor redColor] colorWithAlphaComponent:0.5].CGColor;[self addSubview:_textLabel];
}// 按下
- (void)recordButtonTouchDown{_textLabel.text = @"松开发送";_textLabel.backgroundColor = [UIColor clearColor];_timer = [NSTimer scheduledTimerWithTimeInterval:1.0f target:self selector:@selector(updateVoiceImage) userInfo:nil repeats:YES];[[UIApplication sharedApplication].keyWindow addSubview:self];
}// 移动到外面
- (void)recordButtonTouchUpOutside{[_timer invalidate];[self removeFromSuperview];
}// 改变图片的状态变化
- (void)updateVoiceImage{_recordVoiceView.image = [UIImage imageNamed:@"VoiceSearchFeedback001@2x"];float number = fabs(_peakPower);NSLog(@"number is --->%f",number);if (10< number  && number< 20) {[_recordVoiceView setImage:[UIImage imageNamed:@"VoiceSearchFeedback001@2x"]];}else if (20<number && number<=30) {[_recordVoiceView setImage:[UIImage imageNamed:@"VoiceSearchFeedback002@2x"]];}else if (30<number && number<=40) {[_recordVoiceView setImage:[UIImage imageNamed:@"VoiceSearchFeedback003@2x"]];}else if (40<number && number<=50) {[_recordVoiceView setImage:[UIImage imageNamed:@"VoiceSearchFeedback004@2x"]];}else if (50<number && number<=60) {[_recordVoiceView setImage:[UIImage imageNamed:@"VoiceSearchFeedback005@2x"]];}else if (60<number && number<=70) {[_recordVoiceView setImage:[UIImage imageNamed:@"VoiceSearchFeedback006@2x"]];}else if (70<number && number<=80) {[_recordVoiceView setImage:[UIImage imageNamed:@"VoiceSearchFeedback007@2x"]];}else if (80<number && number<=90) {[_recordVoiceView setImage:[UIImage imageNamed:@"VoiceSearchFeedback008@2x"]];}else if (90<number && number<=100) {[_recordVoiceView setImage:[UIImage imageNamed:@"VoiceSearchFeedback009@2x"]];}else {[_recordVoiceView setImage:[UIImage imageNamed:@"VoiceSearchFeedback020@2x"]];}
}
@end
#import "ViewController.h"
#import "RecordingHub.h"
#import <AVFoundation/AVFoundation.h>@interface ViewController ()@property (weak, nonatomic) IBOutlet UIButton *voiderBtn;
@property (nonatomic,strong) RecordingHub *hud;@property (nonatomic,strong) AVAudioRecorder  *recorder;
@property (nonatomic,strong) NSTimer *timer;
@property (nonatomic,strong) NSURL *recordedFile;@end@implementation ViewController- (void)viewDidLoad {[super viewDidLoad];// 设置好文件的存储位置NSFileManager *manager = [NSFileManager defaultManager];NSArray *pathArray = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);NSString *path = [pathArray lastObject];path = [path stringByAppendingPathComponent:@"RecordedFile"];bool existsFlag = [manager fileExistsAtPath:path];if (!existsFlag) {bool isSuccess = [manager createFileAtPath:path contents:nil attributes:nil];if (isSuccess) {NSLog(@"创建成功");}else{NSLog(@"创建失败");}}_recordedFile = [NSURL fileURLWithPath:path];AVAudioSession *session = [AVAudioSession sharedInstance];NSError *error;// 开启录音和播放功能[session setCategory:AVAudioSessionCategoryPlayAndRecord error:&error];if (session == nil) {NSLog(@"session is error ---->%@",error.description);}else{[session setActive:true error:nil];}_hud = [[RecordingHub alloc]initWithFrame:CGRectMake(self.view.bounds.size.width/2-100/2, self.view.bounds.size.height/2, 100, 100)];[_voiderBtn addTarget:self action:@selector(touchDown:) forControlEvents:UIControlEventTouchDown];[_voiderBtn addTarget:self action:@selector(touchOutside:) forControlEvents:UIControlEventTouchUpInside];
}// 按下的时候
- (void)touchDown:(UIButton *)btn{[_hud recordButtonTouchDown];NSMutableDictionary * recordSetting = [NSMutableDictionary dictionary];//设置录音格式[recordSetting setValue :[NSNumber numberWithInt:kAudioFormatMPEG4AAC] forKey:AVFormatIDKey];//设置录音采样率(Hz) 如:AVSampleRateKey==8000/44100/96000(影响音频的质量)[recordSetting setValue:[NSNumber numberWithFloat:16000.0] forKey:AVSampleRateKey];//录音通道数  1 或 2[recordSetting setValue:[NSNumber numberWithInt: 1] forKey:AVNumberOfChannelsKey];// 初始化录音_recorder = [[AVAudioRecorder alloc]initWithURL:_recordedFile settings:recordSetting error:nil];// 准备录音[_recorder prepareToRecord];//开启音量检测_recorder.meteringEnabled = YES;// 开始录音[_recorder record];NSLog(@"开始录音.....");_timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(changeVoider) userInfo:nil repeats:YES];
}// 抬起来的时候
- (void)touchOutside:(UIButton *)btn{[_hud recordButtonTouchUpOutside];[_timer invalidate];
}//  声音变化
- (void)changeVoider{float peakPower = [_recorder averagePowerForChannel:0];NSLog(@"声音的变化--->%f",peakPower);[_recorder updateMeters];_hud.peakPower = peakPower;
}@end

效果如下:

IM模块-声音麦克风监控波动相关推荐

  1. plc热电偶模块模拟量数据波动原因

    要找到模拟数据波动的根本原因: 可能是以下原因: 您可能使用了自供电或隔离的传感器电源,并且两个电源没有相互连接,即模拟输入模块的电源接地和传感器的信号接地没有连接.这会产生上下振动的高共模电压,从而 ...

  2. python不用模块调用麦克风_python调用pyaudio使用麦克风录制wav声音文件的教程

    python的pyaudio可以进行录音,播放,生成wav文件等等,WAVE是录音时用的标准的WINDOWS文件格式,文件的扩展名为WAV,数据本身的格式为PCM或压缩型,属于无损音乐格式的一种.在我 ...

  3. win10系统,主机箱的前置耳麦插孔用不了,“设置——声音”麦克风或者耳机已拔出未修复,“输入设备”无插座信息,更新驱动也无效,控制面板——小图标里查不到realtek高清晰音频管理器——一招解决

    win10系统,有一天忽然固态硬盘坏了,拆了重装系统之后主机箱的前置耳麦插孔里,插了耳机只能听到声音,麦克风无效,微信语音电话打不出去了. 查了一圈,"设置--声音"显示:麦克风或 ...

  4. 微信控制树莓派运行python_Python+树莓派+人体红外感应模块 实现微信监控

    上一篇文章<Python+树莓派 实现微信拍照.摄相机器人>中简单的实现了由手机微信控制树莓派摄像头拍摄照片及录像,并将拍摄文件回传至微信的功能,程序比较简单, 今天要和大家分享的是在上一 ...

  5. python实现树莓派监控_树莓派上安装pyaudio 及 对声音实时监控

    在树莓派上最常用的录音是arecord命令,但是功能有限,不能实现检测到声音做出反应.但是pyaudio能. 本文实现的功能是在树莓派上监听声音,当分贝超过阈值,将会做出反应,例子的反应是结束whil ...

  6. 【AWS云从业者基础知识笔记】——模块7:监控和分析

    01介绍 学习目标 Summarize approaches to monitoring your AWS environment. Describe the benefits of Amazon C ...

  7. mysql性能模块uptime_MySQL性能监控小知识 (转)

    引用http://hi.baidu.com/mygf/blog/item/b9f55d60049a2ed58cb10d64.html 2009-10-24 00:23 一,获取mysql用户下的进程总 ...

  8. 【直播报名】Location Cache 模块浅析及 OCP 监控、报警详解

    深入浅出 OceanBase 社区线上技术沙龙,旨在帮助关注分布式数据库技术的爱好者们提供技术交流.分享和探讨的线上空间. 每月围绕 OceanBase 核心技术和周边工具展开,大胆说出你想听的话题, ...

  9. Win10--解决电脑麦克风没声音的问题

    原文网址:Win10--解决电脑麦克风没声音的问题_IT利刃出鞘的博客-CSDN博客 简介 本文介绍如何解决Windows10电脑麦克风没声音的问题. 问题复现 录制屏幕时,发现自己的麦克风没声音.去 ...

最新文章

  1. Styling with the DataGridColumnStyle
  2. vue组件级路由钩子函数介绍,及实际应用
  3. pyqt5讲解2:QPushButton,QRadioButton,QCheckBox
  4. Entity Framework 与 面向对象
  5. php laravel 理解,程序员-说一下PHP框架Laravel,如何理解她的思想
  6. 【图像处理】【去模糊】代码资源汇总
  7. python regularexpress1
  8. 开发内功修炼CPU篇
  9. Qt:windows下Qt安装教程
  10. win7 professional 英文版 改 中文
  11. 8086CPU 的寻址方式(重点)
  12. 黑客四种常用来攻击云服务器的手段
  13. 【艺术字签名生成器】】试卷家长签字居然被嫌弃了|“我觉得我还能再抢救一下,你看行嘛?“
  14. Gos —— 获取物理内存容量
  15. 对于Transformer 模型----可以从哪些地方进行创新和改进
  16. 二维码怎么生成彩色样式
  17. 2019年秋季校招前端面经
  18. 人工智能入门:第一章 人工智能课程介绍及环境配置
  19. Pixel 手机上基于多曝光序列的 HDR+ 拍摄功能
  20. CLR学习之初识CLR

热门文章

  1. 【ppt幻灯片制作】Focusky教程 | 将图片背景设置成全屏
  2. 微信小程序 — 九宫格
  3. code::blocks自动换行
  4. 云原生应用架构中的文化变革 一:秉承精益制造之魂,启行 DevOps 之路!
  5. iOS 切圆角离屏渲染问题
  6. 英特尔官方解读:Mobileye 针对自动驾驶的 RSS 安全模型究竟有哪些技术要点?
  7. 深度有趣 | 09 Inception-v3图片分类
  8. rabbitmq的启动命令
  9. mysql修改添加数字_mysql数据库插入将所有ID更改为4294967295
  10. 五大提高DAU的运营策略