iOS AVAudioRecorder錄音,轉(zhuǎn)MP3,播放

功能說明

對錄制聲音的UI以及功能進(jìn)行簡單的封裝,提供快速實(shí)現(xiàn)錄音的幫助類.

  1. 支持監(jiān)測錄音時(shí)的聲音大小.
  2. 支持特定情況下的wav格式轉(zhuǎn)MP3.
  3. 支持簡單的本地音頻資源文件播放.

注意事項(xiàng)

  1. 第三方庫lame的真機(jī)編譯需要關(guān)閉bitcode
  2. 需要在info.plist中間添加麥克風(fēng)權(quán)限NSMicrophoneUsageDescription
  3. 需要加入系統(tǒng)庫AVFoundation.framework

錄音的UI制作

圖片.png

實(shí)現(xiàn)原理

主體思路

  1. 當(dāng)按下時(shí)開始計(jì)時(shí)和錄音,到達(dá)最大設(shè)置時(shí)間停止錄音
  2. 向上移動(dòng)一段距離代表取消錄音,用按下時(shí)的Y坐標(biāo)減去當(dāng)前的Y坐標(biāo)作為判斷依據(jù)
  3. 抬起時(shí)如果取消則刪除錄音本地文件,相反則回調(diào)成功的block

聲音大小效果監(jiān)測設(shè)計(jì)

顯示效果不是幾張不同的圖片,全是view然后設(shè)置了背景色,根據(jù)聲音監(jiān)聽的不同值來設(shè)置view的隱藏和顯示達(dá)到此效果.

實(shí)現(xiàn)對聲音大小的監(jiān)聽
AVAudioRecorder有一個(gè)meteringEnabled的參數(shù),設(shè)置為YES后即可用一個(gè)循環(huán)來監(jiān)聽聲音的大小.

wav轉(zhuǎn)MP3

使用lame這個(gè)庫,需要注意的是轉(zhuǎn)換的部分參數(shù)得和你錄音的一些參數(shù)一致,否則轉(zhuǎn)換出來的就是霹靂巴拉的聲音

錄音的字典配置

 //獲取錄音參數(shù)配置
- (NSDictionary *)getConfig{
    
    NSDictionary *result = nil;
    
    NSMutableDictionary *recordSetting = [[NSMutableDictionary alloc]init];
    //設(shè)置錄音格式  AVFormatIDKey==kAudioFormatLinearPCM
    [recordSetting setValue:[NSNumber numberWithInt:kAudioFormatLinearPCM] forKey:AVFormatIDKey];
    
    //設(shè)置錄音采樣率(Hz) 如:AVSampleRateKey==8000/44100/96000(影響音頻的質(zhì)量)
    [recordSetting setValue:[NSNumber numberWithFloat:44100] forKey:AVSampleRateKey];
    
    //線性采樣位數(shù)  8、16、24、32
    [recordSetting setValue:[NSNumber numberWithInt:16] forKey:AVLinearPCMBitDepthKey];
        
    //錄音通道數(shù)  1 或 2
    [recordSetting setValue:[NSNumber numberWithInt:2] forKey:AVNumberOfChannelsKey];
   
    //錄音的質(zhì)量
    [recordSetting setValue:[NSNumber numberWithInt:AVAudioQualityMedium] forKey:AVEncoderAudioQualityKey];
    
    result = [NSDictionary dictionaryWithDictionary:recordSetting];
    return result;
}

轉(zhuǎn)換代碼

回調(diào)block

typedef void (^BTRecordConvertBlock)(NSString*errorInfo);

轉(zhuǎn)換方法

wavFilePath wav文件路徑

savePath mp3存儲(chǔ)路徑

+(void)convertWavToMp3:(NSString*)wavFilePath withSavePath:(NSString*)savePath withBlock:(BTRecordConvertBlock)block
{
    @try {
        int read, write;
        
        FILE *pcm = fopen([wavFilePath cStringUsingEncoding:1], "rb");  //source 被轉(zhuǎn)換的音頻文件位置
        fseek(pcm, 4*1024,SEEK_CUR);                                   //skip file header
        FILE *mp3 = fopen([savePath cStringUsingEncoding:1], "wb");  //output 輸出生成的Mp3文件位置
        
        const int PCM_SIZE = 8192;
        const int MP3_SIZE = 8192;
        short int pcm_buffer[PCM_SIZE*2];
        unsigned char mp3_buffer[MP3_SIZE];
        
        lame_t lame = lame_init();
        //這里的44100值和錄音的字典中的AVSampleRateKey一樣
        lame_set_in_samplerate(lame, 44100);
        lame_set_VBR(lame, vbr_default);
        lame_init_params(lame);
        
        do {
            read = fread(pcm_buffer, 2*sizeof(short int), PCM_SIZE, pcm);
            if (read == 0)
                write = lame_encode_flush(lame, mp3_buffer, MP3_SIZE);
            else
                write = lame_encode_buffer_interleaved(lame, pcm_buffer, read, mp3_buffer, MP3_SIZE);
            fwrite(mp3_buffer, write, 1, mp3);
        } while (read != 0);
        
        lame_close(lame);
        fclose(mp3);
        fclose(pcm);
    }
    @catch (NSException *exception) {
        dispatch_async(dispatch_get_main_queue(), ^{
            //更新UI操作
            block(exception.reason);
        });
        
    }
    @finally {
        dispatch_async(dispatch_get_main_queue(), ^{
            //更新UI操作
            NSLog(@"MP3生成成功: %@",savePath);
            block(nil);
        });
        
    }
    
}

播放相關(guān)

這里要注意的是AVAudioPlayer類的對象如果只是局部變量,那么要保證這個(gè)對象在方法執(zhí)行完成后不被釋放,因?yàn)楫?dāng)你的聲音有10s而方法只用了很短的時(shí)間就執(zhí)行完了后,對象就被釋放了,所以也就沒有聲音了.

NSError * error=nil;
AVAudioSession *audioSession = [AVAudioSession sharedInstance];
[audioSession setCategory:AVAudioSessionCategoryPlayback error:&error];
NSURL * urlResult=[NSURL fileURLWithPath:url];
self.player = [[AVAudioPlayer alloc] initWithContentsOfURL:urlResult error:&error];
self.player.delegate=self;
self.player.volume=1;
if ([self.player prepareToPlay]) {
    [self.player play];
}    

展示效果

Untitled.gif

Demo地址:https://github.com/StoneMover/alldemo/tree/master/voice

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容