关于ios下录音

我是采用的AVAudioRecorder这个框架来进行录音

这个录音跟官方网站上的speakHere有些区别,最大的区别是,这个必须要录制完成才能处理文件,而speakhere示例是可以实现边录制边上传的效果。

#import <AVFoundation/AVFoundation.h>

#import <CoreAudio/CoreAudioTypes.h>

引入框架,这是使用录音功能的基本配备


先说明一点,默认AVAudioRecorder录制后的格式是.caf,而大部分的播放器都是不支持这个格式的,下面一段设置是可以让录制格式是wav的格式


NSDictionary *recordSetting = [[NSDictionary alloc] initWithObjectsAndKeys:

                                  [NSNumber numberWithFloat: 44100.0],AVSampleRateKey//采样率

                                  [NSNumber numberWithInt: kAudioFormatLinearPCM],AVFormatIDKey,

                                  [NSNumber numberWithInt:16],AVLinearPCMBitDepthKey,//采样位数 默认 16

                                  [NSNumber numberWithInt: 2], AVNumberOfChannelsKey,//通道的数目

                                  [NSNumber numberWithBool:NO],AVLinearPCMIsBigEndianKey,//大端还是小端 是内存的组织方式

                                  [NSNumber numberWithBool:NO],AVLinearPCMIsFloatKey,nil];//采样信号是整数还是浮点数


NSURL *recordedTmpFile = [NSURL fileURLWithPath:[NSTemporaryDirectory() stringByAppendingPathComponent: [NSString stringWithFormat: @"%.0f.%@", [NSDate timeIntervalSinceReferenceDate] * 1000.0, @"wav"]]];  //文件名的设置

//Setup the recorder to use this file and record to it.

AVAudioRecorder *recorder = [[ AVAudioRecorder alloc] initWithURL:recordedTmpFile settings:recordSetting error:&error];

[recorder prepareToRecord];

[recorder record];


下面代码应该是当前.m文件加载时候就设置

AVAudioSession * audioSession = [AVAudioSession sharedInstance];

[audioSession setCategory:AVAudioSessionCategoryPlayAndRecord error: &error]; //设置音频类别,这里表示当应用启动,停掉后台其他音频

[audioSession setActive:YES error: &error];//设置当前应用音频活跃性





  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值