OC-录制本地音频

#import 

/// 录音采样率(Hz)
typedef NS_ENUM(NSInteger, AudioSampleRate) {
    AudioSampleRateLow = 8000,
    AudioSampleRateMedium = 44100,//CD
    AudioSampleRateHigh = 96000
};

/// 线性采样位数-采样精度(bit)
typedef NS_ENUM(NSInteger, AudioLinearPCMBit) {
    AudioLinearPCMBitLow = 8,
    AudioLinearPCMBitMedium = 16,//CD
    AudioLinearPCMBitHigh = 24,
    AudioLinearPCMBitMax = 32
};

@interface LilyRecorder : NSObject
@property (nonatomic, strong) NSString * pcmAudioPath;
@property (nonatomic, strong) NSString * mp3AudioPath;

//实列化
+(LilyRecorder*)sharedAudioRecorder;
//初始化录音器
-(void)createAudioRecorder;

// 开始录音
- (void)startRecording;
// 停止录音
- (void)stopRecording;
@end
#import "LilyRecorder.h"
#import 

@interface LilyRecorder ()
@property(nonatomic,strong) AVAudioRecorder * recorder;
@property(nonatomic,strong) AVAudioSession * session;
@property(nonatomic,strong) NSMutableDictionary * recordSettingDictionary;

@property (nonatomic, assign) BOOL isStopRecording;
@property (nonatomic, strong) dispatch_semaphore_t semaphore;
@end

static LilyRecorder * manager = nil;
@implementation LilyRecorder
+(id)sharedAudioRecorder
{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        manager = [[LilyRecorder alloc] init];
        manager.semaphore = dispatch_semaphore_create(1);
    });
    return manager;
}

#pragma mark - 初始化录音器
-(void)createAudioRecorder
{
    NSURL * url = [NSURL fileURLWithPath:self.pcmAudioPath];
    _recorder = [[AVAudioRecorder alloc] initWithURL:url settings:_recordSettingDictionary error:nil];
    _recorder.delegate = self;
    _recorder.meteringEnabled = YES;
    [_recorder prepareToRecord];
}

#pragma mark - 开始录音
- (void)startRecording
{
    /**
     *  !!!!在录音之前要重新设置音频会话,虽然初始化时设置AVAudioSessionCategoryPlayAndRecord,但是因为
     *  使用了AVPlayer播放音视频,所以会将音频会话隐式转为AVAudioSessionCategoryPlayback,故录音时会出
     *  现录音失败的情况,因此在录音之前要重新设置音频会话为AVAudioSessionCategoryPlayAndRecord。
     */
    [[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayAndRecord error:nil];
    [self.recorder record];
}

#pragma mark - 停止录音
- (void)stopRecording
{
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ //避免结束录音后,尾部数据丢失
        [self.recorder stop];
        self.isStopRecording = YES;
    });
}

#pragma mark - AVAudioRecord Delegate
- (void)audioRecorderDidFinishRecording:(AVAudioRecorder *)recorder successfully:(BOOL)flag {
    DLog(@"finish recording");
}

- (void)audioRecorderEncodeErrorDidOccur:(AVAudioRecorder *)recorder error:(NSError * __nullable)error {
    DLog(@"recorder error");
}


#pragma mark - 懒加载
- (void)setUpRecordingSettings {
    _recordSettingDictionary = [NSMutableDictionary dictionary];
    //录音格式 无法使用
    [_recordSettingDictionary setValue :[NSNumber numberWithInt:kAudioFormatLinearPCM] forKey: AVFormatIDKey];
    //采样率
    [_recordSettingDictionary setValue :[NSNumber numberWithFloat:AudioSampleRateMedium] forKey: AVSampleRateKey];//44100.0
    //通道数
    [_recordSettingDictionary setValue :[NSNumber numberWithInt:2] forKey: AVNumberOfChannelsKey];
    //线性采样位数
    [_recordSettingDictionary setValue :[NSNumber numberWithInt:AudioLinearPCMBitMedium] forKey: AVLinearPCMBitDepthKey];
    //音频质量,采样质量
    [_recordSettingDictionary setValue:[NSNumber numberWithInt:AVAudioQualityHigh] forKey:AVEncoderAudioQualityKey];
}

- (void)setUpAudioSession {
    _session = [AVAudioSession sharedInstance];
    [_session setCategory:AVAudioSessionCategoryPlayAndRecord error:nil];
    [_session setActive:YES error:nil];
    [_session overrideOutputAudioPort:AVAudioSessionPortOverrideSpeaker error:nil];//扬声器播放
}
@end
//初始化录音器
[LilyRecorder sharedAudioRecorder].pcmAudioPath = @"录音文件保存路径";
[[LilyRecorder sharedAudioRecorder] createAudioRecorder];

//开始录音
[[LilyRecorder sharedAudioRecorder] startRecording];

//结束录音
[[LilyRecorder sharedAudioRecorder] stopRecording];

你可能感兴趣的:(OC-录制本地音频)