ios 录制音频

//导入上面2个框架

#import <AVFoundation/AVFoundation.h>

#import <CoreFoundation/CoreFoundation.h>

//声明下面4个属性

@property (nonatomic ,assign)BOOL recording;   //判断是否可以录制

@property (nonatomic ,strong)NSString *fileName; //音频文件存储时的文件名

@property (nonatomic ,strong)AVAudioRecorder *recorder;  //声明一个录制器

@property (nonatomic ,strong)AVAudioPlayer *player;   //声明一个音频播放器


- (void)startRecording{

if (self.recording) {

        return;

    }

    self.recording =YES;

//    设置录音格式  AVFormatIDKey==kAudioFormatLinearPCM

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

//    录音通道数  1 或 2   AVNumberOfChannelsKey

//    线性采样位数  8、16、24、32   AVLinearPCMBitDepthKey

//     录音的质量   AVEncoderAudioQualityKey == AVAudioQualityHigh

//    AVLinearPCMIsBigEndianKey 大端还是小端,是内存的组织方式

//    AVLinearPCMIsFloatKey  采样信号是整数还是浮点数

    NSDictionary *audioSettingDict =[NSDictionary dictionaryWithObjectsAndKeys:

                            [NSNumber numberWithFloat:8000],AVSampleRateKey,

                            [NSNumber numberWithInt:kAudioFormatLinearPCM],

                                AVFormatIDKey,

                            [NSNumber numberWithInt:1],AVNumberOfChannelsKey,

                            [NSNumber numberWithInt:16],AVLinearPCMBitDepthKey,

                            [NSNumber numberWithBool:NO],AVLinearPCMIsBigEndianKey,

                            [NSNumber numberWithBool:NO],AVLinearPCMIsFloatKey,

                            [NSNumber numberWithInt:AVAudioQualityHigh],AVEncoderAudioQualityKey,

                            nil];

    NSError *error =nil;

    AVAudioSession * audioSession = [AVAudioSession sharedInstance];

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

    

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

 

    

    NSDate *now =[NSDate date];

    //实例化一个NSDateFormatter对象

    NSDateFormatter *dateFormater = [[NSDateFormatter alloc] init];

    //设定时间格式,这里可以设置成自己需要的格式

    [dateFormater setDateFormat:@"yyyy-MM-dd HH-mm-ss"];

    

    NSString *fileName = [NSString stringWithFormat:@"rec_%@.wav",[dateFormater stringFromDate:now]];

    self.fileName =fileName;

    NSString *filePath =[NSString documentPathWith:fileName];

    NSURL *fileUrl =[NSURL URLWithString:filePath];

    

//   初始化录制的类

    self.recorder =[[AVAudioRecorder alloc]initWithURL:fileUrl settings:audioSettingDict error:&error];

    

    [self.recorder prepareToRecord];

    [self.recorder peakPowerForChannel:0];

    [self.recorder setMeteringEnabled:YES];

    [self.recorder record];

}

  

转载于:https://www.cnblogs.com/zuolovedu/p/3876716.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值