iOS錄音開發(fā)

一,概述
  1. AVAudioRecorder錄音
  • url:保存錄音文件的沙盒路徑
  • setting:錄音格式設置

AVSampleRateKey: String 采樣率 8000, 44100等
AVNumberOfChannelsKey: String 聲道數(shù) 1為單聲道, 2為雙聲道(立體聲)
AVLinearPCMBitDepthKey: String 位寬 數(shù)據(jù)一般為: 8, 16, 24, 32
AVEncoderAudioQualityKey: String 錄音質量,在AVAudioQuality枚舉中,值有min low medium high max四個
AVLinearPCMIsBigEndianKey: String 大小端編碼:1為大端, 0為小端.
AVFormatIDKey: String 錄音數(shù)據(jù)格式 可以參考CoreAudio 里面相關的值

  1. AVAudioSession音頻會話
  • Audio是iOS、tvOS和watchOS中的托管服務。系統(tǒng)通過使用音頻會話管理應用程序、應用程序間和設備級別的音頻行為。
    AVAudioSession
  • category:Category iOS下目前有七種,每種Category都對應是否支持下面四種能力

Interrupts non-mixable apps audio:是否打斷不支持混音播放的APP
Silenced by the Silent switch:是否會響應手機靜音鍵開關
Supports audio input:是否支持音頻錄制
Supports audio output:是否支持音頻播放

category

參考資料 官方文檔

二,基本實踐應用

1.引入AVFoundation框架;

#import <AVFoundation/AVFoundation.h>
@property (nonatomic, strong) AVAudioRecorder *audioRecorder;//音頻錄音機
@property (nonatomic,copy) NSString *cafPathStr;//錄音存放目錄

2.錄音對象AVAudioRecorder及初始化

/**
 *  獲得錄音機對象
 *
 *  @return 錄音機對象
 */
-(AVAudioRecorder *)audioRecorder{
    if (!_audioRecorder) {
        //創(chuàng)建錄音文件保存路徑
        NSString *path = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject];
        self.cafPathStr = [path stringByAppendingPathComponent:@“myRecord.caf”];
        NSURL *url = [NSURL URLWithString: self.cafPathStr];
        //創(chuàng)建錄音格式設置,詳見下文
        NSDictionary *setting = [self getAudioSetting];
        //創(chuàng)建錄音機
        NSError *error=nil;
        _audioRecorder=[[AVAudioRecorder alloc]initWithURL:url settings:setting error:&error];
        //設置委托代理
        _audioRecorder.delegate=self;
        //如果要監(jiān)控聲波則必須設置為YES
        _audioRecorder.meteringEnabled=YES;
        if (error) {
            NSLog(@"創(chuàng)建錄音機對象時發(fā)生錯誤,錯誤信息:%@",error.localizedDescription);
            return nil;
        }
    }
    return _audioRecorder;
}

3.錄音格式設置

/**
 *  取得錄音文件設置
 *  @return 錄音設置
 */
-(NSDictionary *)getAudioSetting{
    //LinearPCM 是iOS的一種無損編碼格式,但是體積較為龐大
    //錄音設置
    NSMutableDictionary *recordSettings = [[NSMutableDictionary alloc] init];
    //錄音格式 無法使用
    [recordSettings setValue :[NSNumber numberWithInt:kAudioFormatLinearPCM] forKey: AVFormatIDKey];
    //采樣率
    [recordSettings setValue :[NSNumber numberWithFloat:11025.0] forKey: AVSampleRateKey];//44100.0
    //通道數(shù)
    [recordSettings setValue :[NSNumber numberWithInt:2] forKey: AVNumberOfChannelsKey];
    //線性采樣位數(shù)
    //[recordSettings setValue :[NSNumber numberWithInt:16] forKey: AVLinearPCMBitDepthKey];
    //音頻質量,采樣質量
    [recordSettings setValue:[NSNumber numberWithInt:AVAudioQualityMin] forKey:AVEncoderAudioQualityKey];
    return recordSettings;
}

4.錄音開始

#pragma mark -- 錄音開始
- (void)startRecordNotice{
    if ([self.audioRecorder isRecording]) {
        [self.audioRecorder stop];
    }
    // 刪掉錄音文件
    [self.audioRecorder deleteRecording];
    //創(chuàng)建音頻會話對象
    AVAudioSession *audioSession=[AVAudioSession sharedInstance];
   //設置category
    [audioSession setCategory:AVAudioSessionCategoryPlayAndRecord error:nil];
    if (![self.audioRecorder isRecording]){
        // 首次使用應用時如果調用record方法會詢問用戶是否允許使用麥克風
        [self.audioRecorder record];
    }
}

5.錄音暫停和停止

//暫停錄音
- (void)stopRecordNotice{
    [self.audioRecorder pause];
}

//結束錄音
- (void)stopRecordNotice{
    [self.audioRecorder stop];
}
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
【社區(qū)內容提示】社區(qū)部分內容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發(fā)布,文章內容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

友情鏈接更多精彩內容