iOS 录音

13 篇文章 0 订阅

1、导入头文件

#import <AVFoundation/AVFoundation.h>

2、声明 路径和录音对象

@property(nonatomic,strong) NSURL * url;

@property(nonatomic,strong) AVAudioRecorder* recorder;

3、//设置路径

-(NSURL *)url{

    if (_url == nil) {

        NSString *tmpDir = NSTemporaryDirectory();

        NSString *urlPath = [tmpDir stringByAppendingString:@"record.caf"];

        _url = [NSURL fileURLWithPath:urlPath];

    }

    NSLog(@"url");

    return _url;

}

4、//开始录音

-(void)startRcd{

    NSError *error = nil;

    //激活AVAudioSession

    AVAudioSession *session = [AVAudioSession sharedInstance];

    [session setCategory:AVAudioSessionCategoryPlayAndRecord error:&error];

    if (session != nil) {

        [session setActive:YES error:nil];

    }else {

        NSLog(@"session error: %@",error);

    }

    //设置AVAudioRecorder类的setting参数

    NSDictionary *recorderSettings = [[NSDictionary alloc] initWithObjectsAndKeys:

                                      [NSNumber numberWithFloat:16000.0],AVSampleRateKey,

                                      [NSNumber numberWithInt:kAudioFormatAppleIMA4],AVFormatIDKey,

                                      [NSNumber numberWithInt:1],AVNumberOfChannelsKey,

                                      [NSNumber numberWithInt:AVAudioQualityMax], AVEncoderAudioQualityKey,

                                      nil];    

    //实例化AVAudioRecorder对象

    self.recorder = [[AVAudioRecorder alloc] initWithURL:self.url settings:recorderSettings error:&error];

    if (error) {

        NSLog(@"recorder error: %@", error);

    }

    //准备录音

    [self.recorder prepareToRecord];

    //开始录音

    [self.recorder record];

    NSLog(@"start");

}

//结束录音

-(void)stopRcd{

    [self.recorder stop];

    //释放self.recorder,否则无法更新录音文件,即不能进行二次录音

    self.recorder = nil;

    NSLog(@"end");

}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值