iOS實(shí)現(xiàn)錄音轉(zhuǎn)碼MP3及轉(zhuǎn)碼BASE64上傳示例
iOS 錄音轉(zhuǎn)碼MP3及轉(zhuǎn)碼BASE64上傳
一,開始錄音
NSLog(@"開始錄音"); [self startRecord]; - (void)startRecord { //刪除上次生成的文件,保留最新文件 NSFileManager *fileManager = [NSFileManager defaultManager]; if ([NSTemporaryDirectory() stringByAppendingString:@"myselfRecord.mp3"]) { [fileManager removeItemAtPath:[NSTemporaryDirectory() stringByAppendingString:@"myselfRecord.mp3"] error:nil]; } if ([NSTemporaryDirectory() stringByAppendingString:@"selfRecord.wav"]) { [fileManager removeItemAtPath:[NSTemporaryDirectory() stringByAppendingString:@"selfRecord.wav"] error:nil]; } //開始錄音 //錄音設(shè)置 NSMutableDictionary *recordSetting = [[NSMutableDictionary alloc] init]; //設(shè)置錄音格式 AVFormatIDKey==kAudioFormatLinearPCM [recordSetting setValue:[NSNumber numberWithInt:kAudioFormatLinearPCM] forKey:AVFormatIDKey]; //設(shè)置錄音采樣率(Hz) 如:AVSampleRateKey==8000/44100/96000(影響音頻的質(zhì)量), 采樣率必須要設(shè)為11025才能使轉(zhuǎn)化成mp3格式后不會(huì)失真 [recordSetting setValue:[NSNumber numberWithFloat:11025.0] forKey:AVSampleRateKey]; //錄音通道數(shù) 1 或 2 ,要轉(zhuǎn)換成mp3格式必須為雙通道 [recordSetting setValue:[NSNumber numberWithInt:2] forKey:AVNumberOfChannelsKey]; //線性采樣位數(shù) 8、16、24、32 [recordSetting setValue:[NSNumber numberWithInt:16] forKey:AVLinearPCMBitDepthKey]; //錄音的質(zhì)量 [recordSetting setValue:[NSNumber numberWithInt:AVAudioQualityHigh] forKey:AVEncoderAudioQualityKey]; //存儲(chǔ)錄音文件 recordUrl = [NSURL URLWithString:[NSTemporaryDirectory() stringByAppendingString:@"selfRecord.wav"]]; //初始化 audioRecorder = [[AVAudioRecorder alloc] initWithURL:recordUrl settings:recordSetting error:nil]; //開啟音量檢測 audioRecorder.meteringEnabled = YES; audioSession = [AVAudioSession sharedInstance];//得到AVAudioSession單例對象 if (![audioRecorder isRecording]) { [audioSession setCategory:AVAudioSessionCategoryPlayAndRecord error:nil];//設(shè)置類別,表示該應(yīng)用同時(shí)支持播放和錄音 [audioSession setActive:YES error:nil];//啟動(dòng)音頻會(huì)話管理,此時(shí)會(huì)阻斷后臺(tái)音樂的播放. [audioRecorder prepareToRecord]; [audioRecorder peakPowerForChannel:0.0]; [audioRecorder record]; } }
二,停止錄音
[self endRecord]; - (void)endRecord { [audioRecorder stop]; //錄音停止 [audioSession setActive:NO error:nil]; //一定要在錄音停止以后再關(guān)閉音頻會(huì)話管理(否則會(huì)報(bào)錯(cuò)),此時(shí)會(huì)延續(xù)后臺(tái)音樂播放 }
三,轉(zhuǎn)碼成MP3
- (void)transformCAFToMP3 { mp3FilePath = [NSURL URLWithString:[NSTemporaryDirectory() stringByAppendingString:@"myselfRecord.mp3"]]; @try { int read, write; FILE *pcm = fopen([[recordUrl absoluteString] cStringUsingEncoding:1], "rb"); //source 被轉(zhuǎn)換的音頻文件位置 fseek(pcm, 4*1024, SEEK_CUR); //skip file header FILE *mp3 = fopen([[mp3FilePath absoluteString] 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(); lame_set_in_samplerate(lame, 11025.0); lame_set_VBR(lame, vbr_default); lame_init_params(lame); do { read = (int)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) { NSLog(@"%@",[exception description]); } @finally { NSLog(@"MP3生成成功"); base64Str = [self mp3ToBASE64]; } }
四,上傳需要轉(zhuǎn)碼BASE64
- (NSString *)mp3ToBASE64{ NSData *mp3Data = [NSData dataWithContentsOfFile:[NSTemporaryDirectory() stringByAppendingString:@"myselfRecord.mp3"]]; NSString *_encodedImageStr = [mp3Data base64Encoding]; NSLog(@"===Encoded image:\n%@", _encodedImageStr); return _encodedImageStr; }
備注:其中可以直接生成的.caf .wav 有壓縮的MP3需要轉(zhuǎn)格式,不能直接錄音生成
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- iOS多媒體音頻(下)-錄音及其播放的實(shí)例
- IOS開發(fā)實(shí)現(xiàn)錄音功能
- 詳解iOS App中調(diào)用AVAudioPlayer播放音頻文件的用法
- 講解iOS開發(fā)中對音效和音樂播放的簡單實(shí)現(xiàn)
- iOS App中實(shí)現(xiàn)播放音效和音樂功能的簡單示例
- 淺析iOS中視頻播放的幾種方案
- 實(shí)例解析iOS中音樂播放器應(yīng)用開發(fā)的基本要點(diǎn)
- iOS開發(fā)中音頻工具類的封裝以及音樂播放器的細(xì)節(jié)控制
- 詳解iOS應(yīng)用中播放本地視頻以及選取本地音頻的組件用法
- iOS實(shí)時(shí)錄音和播放功能
相關(guān)文章
iOS自定義UIButton點(diǎn)擊動(dòng)畫特效
這篇文章主要為大家詳細(xì)介紹了iOS自定義UIButton點(diǎn)擊動(dòng)畫特效,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-04-04iOS通過Runtime實(shí)現(xiàn)友盟統(tǒng)計(jì)的實(shí)例代碼
本篇文章主要介紹了iOS通過Runtime實(shí)現(xiàn)友盟統(tǒng)計(jì)的實(shí)例代碼,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-06-06iOS實(shí)現(xiàn)去除html標(biāo)簽的方法匯總
相信大家在做網(wǎng)站的時(shí)候,經(jīng)常會(huì)遇到去除html標(biāo)簽的問題,下面這篇文章主要給大家總結(jié)介紹了關(guān)于iOS如何實(shí)現(xiàn)去除html標(biāo)簽的一些方法,文中通過示例代碼介紹的非常詳細(xì),需要的朋友可以參考借鑒,下面來一起看看吧。2017-10-10ajax 三種實(shí)現(xiàn)方法實(shí)例代碼
這篇文章主要介紹了ajax 三種實(shí)現(xiàn)方法實(shí)例代碼的相關(guān)資料,需要的朋友可以參考下2016-09-09iOS經(jīng)驗(yàn)之初始化方法中不該設(shè)置self.view的屬性淺析
這篇文章主要給大家介紹了關(guān)于iOS經(jīng)驗(yàn)之初始化方法中不該設(shè)置self.view的屬性的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起看看吧2018-09-09iOS實(shí)現(xiàn)九宮格自動(dòng)生成視圖
這篇文章主要為大家詳細(xì)介紹了iOS實(shí)現(xiàn)九宮格自動(dòng)生成視圖的方法,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-03-03iOS實(shí)現(xiàn)后臺(tái)長時(shí)間運(yùn)行
這篇文章主要為大家詳細(xì)介紹了iOS實(shí)現(xiàn)后臺(tái)長時(shí)間運(yùn)行,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-10-10swift 單例的實(shí)現(xiàn)方法及實(shí)例
這篇文章主要介紹了swift 單例的實(shí)現(xiàn)方法及實(shí)例的相關(guān)資料,需要的朋友可以參考下2017-07-07