ios录音Demo

<AudioToolbox/AudioToolbox.h> :这个库是C的接口,偏向于底层,主要用于在线流媒体的播放

<AVFoundation/AVFoundation.h> :提供了音频和回放的底层API,同时也负责管理音频的硬件

//需要遵循一个代理协议<AVAudioRecorderDelegate>

//
//  ViewController.m
//  录音录频
//
//  Created by lanou3g on 15/11/19.
//  Copyright © 2015年 lanou3g. All rights reserved.
//

#import "ViewController.h"
//提供了音频和回放的底层API,同时也负责管理音频硬件
#import <AVFoundation/AVFoundation.h>

//这个库是C的接口,偏向于底层,主要用于在线流媒体的播放
#import <AudioToolbox/AudioToolbox.h>

@interface ViewController () <AVAudioRecorderDelegate>

{
    //用来录音
    AVAudioRecorder *recorder;

//设置定时检测,用来监听当前音量大小,控制话筒图片
    NSTimer *timer;
   
    //设置一个路径,用来保存本地录音的路径
    NSURL *urlPlay;
   
   
   
}

//用来控制录音功能
@property (nonatomic, strong) UIButton *btn;

//用来播放已经录好的音频文件
@property (nonatomic, strong) UIButton *playBtn;

//控制音量的图片
@property (nonatomic, strong) UIImageView *imageView;

//音频播放器
@property (nonatomic, strong) AVAudioPlayer *paly;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
   
    //基本步骤:1.进行录音设置(先配置录音机(是一个字典),设置录音的格式,录音的采样率,录音的先行采样位数,录音的通道数,录音质量,录音路径,初始化录音对象,开启音量检测);2.设置录音按钮的功能(UI设置)3.设置播放按钮并实现播放功能
    //1.进行录音设置
    [self audio];
   
    //实现录音按钮的功能(UI设置)
    //2.加图片
    self.imageView = [[UIImageView alloc] initWithFrame:CGRectMake((self.view.frame.size.width - 100)/2, 100, 100, 100)];
    self.imageView.backgroundColor = [UIColor redColor];
   
    self.btn = [UIButton buttonWithType:UIButtonTypeCustom];
   
    self.btn.frame = CGRectMake(self.imageView.frame.origin.x, 250, 50, 40);    [self.btn setTitle:@"开始" forState:UIControlStateNormal];    [self.btn setBackgroundColor:[UIColor greenColor]];    self.playBtn = [UIButton buttonWithType:UIButtonTypeCustom];    self.playBtn.frame = CGRectMake(self.imageView.frame.origin.x + 50, 250, 50, 40);    [self.playBtn setTitle:@"播放" forState:UIControlStateNormal];    [self.playBtn setBackgroundColor:[UIColor cyanColor]];    [self.playBtn addTarget:self action:@selector(paly:) forControlEvents:UIControlEventTouchUpInside];       //设置按钮的事件,当按钮被按下时    [self.btn addTarget:self action:@selector(btnDown:) forControlEvents:UIControlEventTouchDown];    //当手指抬起时     [self.btn addTarget:self action:@selector(btnUp:) forControlEvents:UIControlEventTouchUpInside];       //当触摸拖动离开控制范围时     [self.btn addTarget:self action:@selector(btnDragUp:) forControlEvents:UIControlEventTouchDragExit];       [self.view addSubview:self.playBtn];    [self.view addSubview:self.btn];    [self.view addSubview:self.imageView];         }//点击播放按钮的实现播放- (void)paly:(UIButton *)sender{    //加载本地录音文件    self.paly = [[AVAudioPlayer alloc] initWithContentsOfURL:urlPlay error:nil];    //准备播放    [self.paly prepareToPlay];    //播放    [self.paly play];   }//录音设置的方法- (void)audio{    //1.先配置Recorder(录音机)    NSMutableDictionary *recorderSetting = [NSMutableDictionary dictionary];

//2.设置录音的格式 / *在2000年被用在MPEG-4中(ISO 14496-3 Audio),所以现在变更为MPEG-4 AAC标准,也就是说,AAC已经成为MPEG4家族的主要成员之一,它是MPEG4第三部分中的音频编码系统。AAC可提供最多48个全音域音频通道。*/

[recorderSetting setValue:[NSNumber numberWithInt:kAudioFormatMPEG4AAC] forKey:AVFormatIDKey];
   
    //3.设置录音采样率 --采样频率是指录音设备在一秒钟内对声音信号的采样次数,采样频率越高声音的还原就越真实越自然。在当今的主流声卡上,采样频率一般共分为22.05KHz、44.1KHz、48KHz三个等级,22.05只能达到FM广播的声音品质,44.1KHz则是理论上的CD音质界限,48KHz则更加精确一些
    [recorderSetting setValue:[NSNumber numberWithFloat:44100] forKey:AVSampleRateKey];

//4.设置录音的通道数
    [recorderSetting setValue:[NSNumber numberWithInt:1] forKey:AVNumberOfChannelsKey];

//5.线性采样位数 8 ,16 ,24 ,32、采样位数可以理解为声卡处理声音的解析度。这个数值越大,解析度就越高,录制和回放的声音就越真实 --一般都是16位的(2的16次方)/*

PCM的基本参数是采样频率和采样位深,采样频率就是每秒采样多少次,位深就是声音通过拾音器转成的电平信号被量化的精细度,同时也代表一次采样会用多少位保存

*/

[recorderSetting setValue:[NSNumber numberWithInt:16] forKey:AVLinearPCMBitDepthKey];
   
    //6,录音质量
    [recorderSetting setValue:[NSNumber numberWithInt:AVAudioQualityHigh] forKey:AVEncoderAudioQualityKey];
   
    //设置录音路径
    NSString *strUrl = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
   
    NSURL *url = [NSURL fileURLWithPath:[NSString stringWithFormat:@"%@/record.aac",strUrl]];
   
    //记录当前路径
    urlPlay = url;
   
    //初始化录音对象
    NSError *error;
    recorder = [[AVAudioRecorder alloc] initWithURL:url settings:recorderSetting error:&error];
   
    //开启音量检测
    recorder.meteringEnabled = YES;
    recorder.delegate = self;

}

- (void)btnDown:(UIButton *)sender
{
   
    [sender setTitle:@"停止" forState:UIControlStateNormal];
    //创建录音文件,准备录音
    if([recorder prepareToRecord]){
   
        //开始
        [recorder record];
       
    }
   
    //设置定时检测,检测我们的图片
    timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(detectionVioce) userInfo:nil repeats:YES];

}
- (void)btnUp:(UIButton *)sender{

//UI状态
    [sender setTitle:@"开始" forState:UIControlStateNormal];
   
    //记录当前时间
    double cTime = recorder.currentTime;
   
    if (cTime > 2) {
       
        NSLog(@"放出去");
    }else{
        //删除我们的记录文件
        [recorder deleteRecording];
   
    }
   
    //停止录音
    [recorder stop];
    //停止计时器
    [timer invalidate];
}

- (void)btnDragUp:(UIButton *)sender{

[sender setTitle:@"开始" forState:UIControlStateNormal];
   
    //删除录制文件
    [recorder deleteRecording];
    //停止录音
    [recorder stop];
    //停止计时器
    [timer invalidate];
   
    NSLog(@"取消发送");
   
   
}

//检测当前声音
- (void)detectionVioce
{
   
    //刷新当前音量数据
    [recorder updateMeters];
    //获取音量的平均值
    double lowPassResults = pow(10, (0.05 * [recorder peakPowerForChannel:0]));//×于当前音量
     //现在取值范围在0~1   (每隔0.7)
    if(0 < lowPassResults < 0.06){
   
   
        [self.imageView setImage:[UIImage imageNamed:@"iconfont-huatong(1)"]];
    }else if( 0.06< lowPassResults <= 0.13){
   
      [self.imageView setImage:[UIImage imageNamed:@"iconfont-huatong(2)"]];
   
    }else if( 0.13<lowPassResults <= 0.20){
       
        [self.imageView setImage:[UIImage imageNamed:@"iconfont-huatong(3)"]];
       
    }else if( 0.20 <lowPassResults <= 0.27){
       
        [self.imageView setImage:[UIImage imageNamed:@"iconfont-huatong(4)"]];
       
    }else if( 0.27 <lowPassResults <= 0.34){
       
        [self.imageView setImage:[UIImage imageNamed:@"iconfont-huatong(5)"]];
       
    }else if( 0.34 <lowPassResults <= 0.41){
       
        [self.imageView setImage:[UIImage imageNamed:@"iconfont-huatong(6)"]];
       
    }else if( 0.41 <lowPassResults <= 0.48){
       
        [self.imageView setImage:[UIImage imageNamed:@"iconfont-huatong(7)"]];
       
    }。。。。。){
       
        [self.imageView setImage:[UIImage imageNamed:@"iconfont-huatong(8)"]];
       
    }else if( 0.55 <lowPassResults <= 0.62){
       
        [self.imageView setImage:[UIImage imageNamed:@"iconfont-huatong(9)"]];
       
    }else if( 0.62 <lowPassResults <= 0.69){
       
        [self.imageView setImage:[UIImage imageNamed:@"iconfont-huatong(10)"]];
       
    }){
       
        [self.imageView setImage:[UIImage imageNamed:@"iconfont-huatong(8)"]];
       
    }else if( 0.55 <lowPassResults <= 0.62){
       
        [self.imageView setImage:[UIImage imageNamed:@"iconfont-huatong(9)"]];
       
    }else if( 0.62 <lowPassResults <= 0.69){
       
        [self.imageView setImage:[UIImage imageNamed:@"iconfont-huatong(10)"]];
       
    }else if( 0.69 <lowPassResults <= 0.76){
       
        [self.imageView setImage:[UIImage imageNamed:@"iconfont-huatong(11)"]];
       
    }else if( 0.76 <lowPassResults <= 0.83){
       
        [self.imageView setImage:[UIImage imageNamed:@"iconfont-huatong(12)"]];
       
    }else if( 0.83 <lowPassResults <= 0.90){
       
        [self.imageView setImage:[UIImage imageNamed:@"iconfont-huatong(13)"]];
       
    }else{
       
       
        [self.imageView setImage:[UIImage imageNamed:@"iconfont-huatong(14)"]];
       
    }
else if( 0.48 <lowPassResults <= 0.55){
       
        [self.imageView setImage:[UIImage imageNamed:@"iconfont-huatong(8)"]];
       
    }else if( 0.55 <lowPassResults <= 0.62){
       
        [self.imageView setImage:[UIImage imageNamed:@"iconfont-huatong(9)"]];
       
    }else if( 0.62 <lowPassResults <= 0.69){
       
        [self.imageView setImage:[UIImage imageNamed:@"iconfont-huatong(10)"]];
       
    }else if( 0.69 <lowPassResults <= 0.76){
       
        [self.imageView setImage:[UIImage imageNamed:@"iconfont-huatong(11)"]];
       
    }else if( 0.76 <lowPassResults <= 0.83){
       
        [self.imageView setImage:[UIImage imageNamed:@"iconfont-huatong(12)"]];
       
    }else if( 0.83 <lowPassResults <= 0.90){
       
        [self.imageView setImage:[UIImage imageNamed:@"iconfont-huatong(13)"]];
       
    }else{
       
       
        [self.imageView setImage:[UIImage imageNamed:@"iconfont-huatong(14)"]];
       
    }
else if( 0.48 <lowPassResults <= 0.55){
       
        [self.imageView setImage:[UIImage imageNamed:@"iconfont-huatong(8)"]];
       
    }else if( 0.55 <lowPassResults <= 0.62){
       
        [self.imageView setImage:[UIImage imageNamed:@"iconfont-huatong(9)"]];
       
    }else if( 0.62 <lowPassResults <= 0.69){
       
        [self.imageView setImage:[UIImage imageNamed:@"iconfont-huatong(10)"]];
       
    }else if( 0.69 <lowPassResults <= 0.76){
       
        [self.imageView setImage:[UIImage imageNamed:@"iconfont-huatong(11)"]];
       
    }else if( 0.76 <lowPassResults <= 0.83){
       
        [self.imageView setImage:[UIImage imageNamed:@"iconfont-huatong(12)"]];
       
    }else if( 0.83 <lowPassResults <= 0.90){
       
        [self.imageView setImage:[UIImage imageNamed:@"iconfont-huatong(13)"]];
       
    }else{
       
       
        [self.imageView setImage:[UIImage imageNamed:@"iconfont-huatong(14)"]];
       
    }
else if( 0.48 <lowPassResults <= 0.55){
       
        [self.imageView setImage:[UIImage imageNamed:@"iconfont-huatong(8)"]];
       
    }else if( 0.55 <lowPassResults <= 0.62){
       
        [self.imageView setImage:[UIImage imageNamed:@"iconfont-huatong(9)"]];
       
    }else if( 0.62 <lowPassResults <= 0.69){
       
        [self.imageView setImage:[UIImage imageNamed:@"iconfont-huatong(10)"]];
       
    }else if( 0.69 <lowPassResults <= 0.76){
       
        [self.imageView setImage:[UIImage imageNamed:@"iconfont-huatong(11)"]];
       
    }else if( 0.76 <lowPassResults <= 0.83){
       
        [self.imageView setImage:[UIImage imageNamed:@"iconfont-huatong(12)"]];
       
    }else if( 0.83 <lowPassResults <= 0.90){
       
        [self.imageView setImage:[UIImage imageNamed:@"iconfont-huatong(13)"]];
       
    }else{
       
       
        [self.imageView setImage:[UIImage imageNamed:@"iconfont-huatong(14)"]];
       
    }
){
       
        [self.imageView setImage:[UIImage imageNamed:@"iconfont-huatong(8)"]];
       
    }else if( 0.55 <lowPassResults <= 0.62){
       
        [self.imageView setImage:[UIImage imageNamed:@"iconfont-huatong(9)"]];
       
    }else if( 0.62 <lowPassResults <= 0.69){
       
        [self.imageView setImage:[UIImage imageNamed:@"iconfont-huatong(10)"]];
       
    }else if( 0.69 <lowPassResults <= 0.76){
       
        [self.imageView setImage:[UIImage imageNamed:@"iconfont-huatong(11)"]];
       
    }else if( 0.76 <lowPassResults <= 0.83){
       
        [self.imageView setImage:[UIImage imageNamed:@"iconfont-huatong(12)"]];
       
    }else if( 0.83 <lowPassResults <= 0.90){
       
        [self.imageView setImage:[UIImage imageNamed:@"iconfont-huatong(13)"]];
       
    }else{
       
       
        [self.imageView setImage:[UIImage imageNamed:@"iconfont-huatong(14)"]];
       
    }0.69){
       
        [self.imageView setImage:[UIImage imageNamed:@"iconfont-huatong(10)"]];
       
    }else if( 0.69 <lowPassResults <= 0.76){
       
        [self.imageView setImage:[UIImage imageNamed:@"iconfont-huatong(11)"]];
       
    }else if( 0.76 <lowPassResults <= 0.83){
       
        [self.imageView setImage:[UIImage imageNamed:@"iconfont-huatong(12)"]];
       
    }else if( 0.83 <lowPassResults <= 0.90){
       
        [self.imageView setImage:[UIImage imageNamed:@"iconfont-huatong(13)"]];
       
    }else{
       
       
        [self.imageView setImage:[UIImage imageNamed:@"iconfont-huatong(14)"]];

时间: 2024-11-03 21:57:55

ios录音Demo的相关文章

Swift实现iOS录音与播放音频功能

作用AVPLayer:可以用来播放在线及本地音视频AVAudioSession:音频会话,主要用来管理音频设置与硬件交互使用时需要导入 #import <AVFoundation/AVFoundation.h> AVAudioSession中配置选项: AVAudioSessionCategory 注意:除了 AVAudioSessionCategoryMultiRoute 外,其他的 Category 都遵循 last in wins 原则,即最后接入的音频设备作为输入或输出的主设备. 1.

汉语字典或者词典的简单的ios小demo

首先感谢  枯龙吟  网易博客提供了[成语词典][新华字典][汉语词典]接口以供使用. 无意中在网上发现了一个关于汉语字典方面的接口 ,于是自己就测试了一下,发现可以使用,非常感谢那位大神!!! 附上demo截图: 主要用到了数据解析的方式,服务器返回的是json数据,直接使用ios提供的类进行解析,这个小demo很简单,直接附上源码:(代码中,我封装了一个方法,传入服务器的地址和请求的数据,回到主线程更新UI) 1 // 2 // ViewController.m 3 // 新华字典 4 //

XMPP协议实现即时通讯底层书写 (二)-- IOS XMPPFramework Demo+分析

我希望,This is a new day! 在看代码之前,我认为你还是应该先整理一下心情,来听我说几句: 首先,我希望你是在早上边看这篇blog,然后一边開始动手操作,假设你仅仅是看blog而不去自己对照项目,作用不是非常大.一日之计在于晨,所以怀着一颗对技术渴望,激动的.亢奋的心情去学习.你才干有所得. 嗯,就拿鄙人当时做项目来说,每天早上起来的第一件事情.就是研究XMPPFramework作者的代码,依照模块来分析和模仿书写.睡觉的时候还在思考,分析.总结... 当然我并非说每一个Dev

iOS录音和播放的那些事儿:几个奇葩的需求

最近在做的项目中,需要在iPhone上接上带麦耳机(苹果叫Headset,不带麦耳机叫Headphone),然后实现同步录音和播放.这个功能实现之后,需要改变录音.播放的输入源. 对于实现同步录音和播放功能,肯定就要使用到底层的接口,用AVAudioRecorder/AVAudioPlayer是无法实现的. 我研究了iOS声音处理的知识之后,发现自己实现太过麻烦,需要用到至少包括AudioQueue/AudioBuffer等等,然后还有各种复杂的C Struct.回调函数.回调处理等等,非常麻烦

IOS 录音(AVAudioRecorder)

#import "HMViewController.h" #import <AVFoundation/AVFoundation.h> @interface HMViewController () - (IBAction)startRecord; - (IBAction)stopRecord; @property (nonatomic, strong) AVAudioRecorder *recorder; @property (nonatomic, strong) CADis

ios录音

#import "ViewController.h" #import <AVFoundation/AVFoundation.h> @interface ViewController () @property(nonatomic,strong)AVAudioRecorder*record; @property(nonatomic,strong)CADisplayLink *updatereflesh; @property(nonatomic,assign)CGFloat ti

iOS录音后播放声音小,AudioSessionInitialize failed,AudioQueueStart failed (-50)

方法1: UInt32 audioRoute = kAudioSessionOverrideAudioRoute_Speaker; AudioSessionSetProperty(kAudioSessionProperty_OverrideAudioRoute, sizeof(audioRoute), &audioRoute); 方法2: [[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback err

iOS天气预报Demo

简单的天气预报demo,调用了百度的天气预报接口,预报城市可通过CoreLocation定位与手动添加. Demo素材与页面设计参考安卓天气软件WayHoo. 截图: Demo地址: https://github.com/WorthyZhang/WZWeather

ios录音、音频播放功能

#import <AVFoundation/AVFoundation.h> { NSInteger   _timeCount;    NSTimer     *_timer; } @property(nonatomic,retain)AVAudioPlayer  *audioPlayer;@property(nonatomic,retain)AVAudioRecorder *recorder;@property(nonatomic,assign)NSInteger      playTag;