IOS-录音

 1 //
 2 //  ViewController.m
 3 //  IOS_0322_录音
 4 //
 5 //  Created by ma c on 16/3/22.
 6 //  Copyright © 2016年 博文科技. All rights reserved.
 7 //
 8
 9 #import "ViewController.h"
10 #import <AVFoundation/AVFoundation.h>
11
12 @interface ViewController ()
13
14 @property (nonatomic, strong) AVAudioRecorder *recorder;
15 @property (nonatomic, strong) CADisplayLink *timer;
16 @property (nonatomic, assign) CGFloat silentDuration;
17
18
19 - (IBAction)startRecord;
20 - (IBAction)stopRecord;
21
22 @end
23
24 @implementation ViewController
25
26 - (void)viewDidLoad {
27     [super viewDidLoad];
28     // Do any additional setup after loading the view, typically from a nib.
29 }
30
31 - (void)didReceiveMemoryWarning {
32     [super didReceiveMemoryWarning];
33     // Dispose of any resources that can be recreated.
34 }
35
36 - (void)addTimer
37 {
38     self.timer = [CADisplayLink displayLinkWithTarget:self selector:@selector(update)];
39     [self.timer addToRunLoop:[NSRunLoop mainRunLoop] forMode:NSRunLoopCommonModes];
40 }
41
42 - (void)removeTimer
43 {
44     [self.timer invalidate];
45     self.timer = nil;
46 }
47
48 - (void)update
49 {
50     //跟新测试值
51     [self.recorder updateMeters];
52     float power = [self.recorder averagePowerForChannel:0];
53     if (power < -20) { //静音
54         self.silentDuration += self.timer.duration;
55
56         if (self.silentDuration > 2) {
57             [self.recorder stop];
58         }
59     } else{ //说话
60         self.silentDuration = 0;
61     }
62 }
63 - (IBAction)startRecord {
64     NSString *doc = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)lastObject];
65     NSString *path = [doc stringByAppendingString:@"test.caf"];
66     NSURL *url = [NSURL fileURLWithPath:path];
67
68     AVAudioRecorder *recorder = [[AVAudioRecorder alloc] initWithURL:url settings:nil error:nil];
69     //缓冲
70     [recorder prepareToRecord];
71     //录音
72     [recorder record];
73
74     //开启分贝测量功能
75     recorder.meteringEnabled = YES;
76     [recorder averagePowerForChannel:0];
77
78     self.recorder = recorder;
79 }
80
81 - (IBAction)stopRecord {
82     [self.recorder stop];
83 }
84 @end
时间: 2024-12-28 16:18:15

IOS-录音的相关文章

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

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

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录音Demo

<AudioToolbox/AudioToolbox.h> :这个库是C的接口,偏向于底层,主要用于在线流媒体的播放 <AVFoundation/AVFoundation.h> :提供了音频和回放的底层API,同时也负责管理音频的硬件 //需要遵循一个代理协议<AVAudioRecorderDelegate> ////  ViewController.m//  录音录频////  Created by lanou3g on 15/11/19.//  Copyright

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

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

ios录音、音频播放功能

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

iOS录音实践

在AVFoundation框架中AVAudioRecorder类专门处理录音操作,支持多种音频格式.下面是常用的属性和方法: 属性 说明 @property(readonly, getter=isRecording) BOOL recording; 是否正在录音,只读 @property(readonly) NSURL *url 录音文件地址,只读 @property(readonly) NSDictionary *settings 录音文件设置,只读 @property(readonly) N

iOS 学习笔记 十三 (2015.04.15)采用第三方库,实现ios录音转为amr

1.第三方开源库地址 https://github.com/guange2015/ios-amr 2.参考博客地址 http://blog.csdn.net/windsoul85/article/details/8035088 http://macleo.iteye.com/blog/1708747

ios AudioQueueStart returns -50 录音失败问题

使用iOS录音时,如果在应用的别处有语音的操作,可能会出现上述问题: 导致录音机打开失败!无法录音,并且 AudioQueueStart() 返回的错误码 -50 解决方式 在调用 AudioQueueStart()方法之前,调用如下代码 AVAudioSession * session = [AVAudioSession sharedInstance]; if (!session) printf("ERROR INITIALIZING AUDIO SESSION! \n"); els