zoukankan      html  css  js  c++  java
  • 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
  • 相关阅读:
    MySQL性能优化(二):优化数据库的设计
    MySQL性能优化(一):优化方式
    PTA 07-图4 哈利·波特的考试 (25分)
    PTA 06-图3 六度空间 (30分)
    PTA 06-图2 Saving James Bond
    PTA 06-图1 列出连通集 (25分)
    PTA 05-树9 Huffman Codes (30分)
    PTA 05-树8 File Transfer (25分)
    PTA 05-树7 堆中的路径 (25分)
    PTA 04-树6 Complete Binary Search Tree (30分)
  • 原文地址:https://www.cnblogs.com/oc-bowen/p/5308014.html
Copyright © 2011-2022 走看看