AVAudioRecorder 录音

.h 文件


@interface ViewController : UIViewController <AVAudioRecorderDelegate>


{

    // 用来录音

    AVAudioRecorder *recoder;

    // 设置定时检测用来监听当前音量大小控制话筒图片.

    NSTimer *timer;

    // 音频的 存储的 url 路径

    NSURL *urlPlay;

}


// 用来控制录音功能

@property (nonatomicstrongUIButton *recordButton;


// 音频播放器

@property (nonatomicstrongAVAudioPlayer *audioPlay;


@end



#####################################################################


.m 文件


@implementation ViewController


- (void)viewDidLoad {

    [super viewDidLoad];

    

    // 进行录音设置

    [self xLAudio];


    self.recordButton = [UIButton buttonWithType:UIButtonTypeCustom];

    _recordButton.backgroundColor = [UIColor purpleColor];

    self.recordButton.frame = CGRectMake(self.imageView.frame.origin.x2505040);

    [self.recordButton setTitle:@"start" forState:UIControlStateNormal];

    

    // 当按钮被按下

    [self.recordButton addTarget:self action:@selector(recordButtonDown:) forControlEvents:UIControlEventTouchDown];

    

    // 当手指抬起时

    [self.recordButton addTarget:self action:@selector(recordButtonUp:) forControlEvents:UIControlEventTouchUpInside];

    

    // 当触摸拖动离开控制范围时

    [self.recordButton addTarget:self action:@selector(recordButtonDragUp:) forControlEvents:UIControlEventTouchDragExit];

    

    [self.view addSubview:self.recordButton];

    

    // 设置音频会话

    [self xLSetAudioSession];

}


// 设置音频会话

- (void)xLSetAudioSession {

    

    AVAudioSession *audioSession = [AVAudioSession sharedInstance];

    // 设置为播放和录音状态以便可以在录制完之后播放录音

    [audioSession setCategory:AVAudioSessionCategoryPlayAndRecord error:nil];

    [audioSession setActive:YES error:nil];

}


// 录音设置

- (void)xLAudio {

    // 先配置Recorder

    NSMutableDictionary *recordSetting = [NSMutableDictionary dictionary];

    

    // 设置录音格式

    [recordSetting setValue:[NSNumber numberWithInt:kAudioFormatMPEG4AACforKey:AVFormatIDKey];

    // 设置录音采样率

    [recordSetting setValue:[NSNumber numberWithFloat:44100forKey:AVSampleRateKey];

    // 录音的通道数

    [recordSetting setValue:[NSNumber numberWithInt:1forKey:AVNumberOfChannelsKey];

    // 线性采样位数8, 16, 24, 32

    [recordSetting setValue:[NSNumber numberWithInt:16forKey:AVLinearPCMBitDepthKey];

    // 录音质量

    [recordSetting setValue:[NSNumber numberWithInt:AVAudioQualityHighforKey:AVEncoderAudioQualityKey];

    

    NSString *strUrl = [self xlGetSavePathWithFileSuffix:@"/record.aac"];

    NSURL *url = [NSURL fileURLWithPath:[NSString stringWithFormat:@"%@", strUrl]];

    urlPlay = url;

    

    NSError *error = nil;

    recoder = [[AVAudioRecorder allocinitWithURL:url settings:recordSetting error:&error];

    

    // 开启音量检测

    recoder.meteringEnabled = YES;

    recoder.delegate = self;

}


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

    [sender setTitle:@"stop" forState:UIControlStateNormal];


    // 创建录音文件准备录音

    if ([recoder prepareToRecord]) {

        // 开始

        [recoder record];

    }

    

    // 设置定时检测

    timer = [NSTimer scheduledTimerWithTimeInterval:0 target:self selector:@selector(detectionVoice) userInfo:nil repeats:YES];

}


// 检测当前声音

- (void)detectionVoice {

    [recoder updateMeters]; // 刷新当前音量数据

    

    double lowPassResults = pow(10, (0.05 * [recoder peakPowerForChannel:0]));

    // 取值范围现在是 0 ~ 1

    if (0 < lowPassResults < 0.06) {

        

    }

}


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

    [sender setTitle:@"start" forState:UIControlStateNormal];

    double cTime = recoder.currentTime;

    if (cTime > 2) {

             

        NSError *error = nil;

        self.audioPlay = [[AVAudioPlayer allocinitWithContentsOfURL:urlPlay error:&error];

        if (!error) {

            _audioPlay.currentTime = 0;

            [_audioPlay play];

        }

    

        self.audioPlay = nil;

        

        NSLog(@"放出去...");

    } else {

        // 删除记录文件

        [recoder deleteRecording];

    }

    [recoder stop];

    [timer invalidate];

}


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

    [sender setTitle:@"start" forState:UIControlStateNormal];

    

    // 删除录制文件

    [recoder deleteRecording];

    

    [recoder stop];

    [timer invalidate];

    

    NSLog(@"取消发送");

}


// 获取文件名:由时间 + 后缀组成

- (NSString *)xlGetSavePathWithFileSuffix:(NSString *)suffix {

    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectoryNSUserDomainMaskYES);

    NSString *documentPath = [paths objectAtIndex:0];

    

    NSDate *date = [NSDate date];

    // 获取当前时间

    NSDateFormatter *dateFormat = [[NSDateFormatter allocinit];

    [dateFormat setDateFormat:@"yyyyMMddHHmmss"];

    NSString *curretDateAndTime = [dateFormat stringFromDate:date];

    

    // 命名文件

    NSString *fileName = [NSString stringWithFormat:@"%@.%@", curretDateAndTime,suffix];

    // 指定文件存储路径

    NSString *filePath = [documentPath stringByAppendingPathComponent:fileName];

    

    return filePath;

}


- (void)didReceiveMemoryWarning {

    [super didReceiveMemoryWarning];

    if ([self isViewLoaded] && !self.view.window) {

        self.view = nil;

    }

}


@end


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值