录音

//

//  ViewController.m

//  录音

//

//  Created by 谢泽锋 on 2017/3/6.

//  Copyright © 2017年 xiezefeng. All rights reserved.

//

#define RecordFile [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject] stringByAppendingPathComponent:@"tempRecord.data"]

 

#import "ViewController.h"

#import <AVFoundation/AVFoundation.h>

@interface ViewController ()<AVAudioRecorderDelegate>

@property(strong, nonatomic)AVPlayerItem * palyItem;

@property(strong, nonatomic)AVPlayer * player;

 

/***  录音对象  */

@property (nonatomic, strong) AVAudioRecorder *recorder;

 

@end

 

@implementation ViewController

 

- (void)viewDidLoad {

    [super viewDidLoad];

}

- (IBAction)start:(id)sender {

    [self startRecorder];

}

/**

 *  设置录音一些属性

 */

- (NSDictionary *)recordingSettings

{

    NSMutableDictionary *recordSetting =[NSMutableDictionary dictionaryWithCapacity:10];

    // 音频格式

    recordSetting[AVFormatIDKey] = @(kAudioFormatMPEG4AAC);

    // 录音采样率(Hz) 如:AVSampleRateKey==8000/44100/96000(影响音频的质量)

    recordSetting[AVSampleRateKey] = @(44100);

    // 音频通道数 1 或 2

    recordSetting[AVNumberOfChannelsKey] = @(1);

    // 线性音频的位深度  8、16、24、32

    recordSetting[AVLinearPCMBitDepthKey] = @(16);

    //录音的质量

    recordSetting[AVEncoderAudioQualityKey] = [NSNumber numberWithInt:AVAudioQualityHigh];

    

    return recordSetting;

}

 

/**

 *  开始录音

 *

 *  @param basePath 设置录音基础路径(后面要加上具体录音的名字)

 */

-(void)startRecorder{

    

    //处理权限问题

    AVAudioSession *audioSession = [AVAudioSession sharedInstance];

    NSError *err = nil;

    //设置AVAudioSession

    [audioSession setCategory:AVAudioSessionCategoryPlayAndRecord error:&err];

    [audioSession setActive:YES error:nil];

    if(err) {

        NSLog(@"获取权限失败");

        return;

    }

    

    

    //初始化生成录音对象

    err = nil;

    NSURL *recordedFile = [NSURL fileURLWithPath:[RecordFile stringByAppendingString:@"kkk.aac"]];

    NSDictionary *dic = [self recordingSettings];

    _recorder = [[AVAudioRecorder alloc] initWithURL:recordedFile settings:dic error:&err];

    if(_recorder == nil) {

        NSLog(@"生成录音对象失败");

        return;

    }

    

    

    //准备和开始录音

    [_recorder prepareToRecord];

    //启用录音测量

    _recorder.meteringEnabled = YES;

    //开始录音

    [_recorder record];

    [_recorder recordForDuration:0];

 

    

}

 

- (IBAction)stop:(UIButton*)sender {

    

    //处理权限问题

    AVAudioSession *audioSession = [AVAudioSession sharedInstance];

    NSError *err = nil;

    //设置AVAudioSession

    [audioSession setCategory:AVAudioSessionCategoryPlayback error:&err];

    [audioSession setActive:YES error:nil];

 

    NSURL * audioUrl  = [NSURL fileURLWithPath:[RecordFile stringByAppendingString:@"kkk.aac"]];

    _palyItem = [[AVPlayerItem alloc]initWithURL:audioUrl];

    _player = [[AVPlayer alloc]initWithPlayerItem:_palyItem];

    AVPlayerLayer *playerLayer=[AVPlayerLayer playerLayerWithPlayer:_player];

    [sender.layer addSublayer:playerLayer];

    [_player play];

}

 

@end

 

posted @ 2017-03-08 17:14  谢小锋  阅读(217)  评论(0编辑  收藏  举报