公司項(xiàng)目中涉及到語音錄制的功能汉形,在錄音格式方面遇到一些小問題現(xiàn)在拿出來與大家分享一下。
眾所周知倍阐,iOS 音頻錄制是不支持AMR格式的概疆。但 Android 好像是默認(rèn)是AMR格式的。兩邊格式不同必然有一方做出妥協(xié)的收捣。這里只簡單介紹一下iOS 格式轉(zhuǎn)碼的方法届案。
1、音頻錄制簡介
在AVFoundation
框架中AVAudioRecorder
類專門處理錄音操作罢艾,它支持多種音頻格式楣颠。這里以AVAudioRecorder
錄制音頻,AVAudioPlayer
播放音頻咐蚯。 在創(chuàng)建錄音時(shí)除了指定路徑外還必須指定錄音設(shè)置信息童漩,因?yàn)殇浺魰r(shí)必須知道錄音文件的格式、采樣率春锋、通道數(shù)矫膨、每個(gè)采樣點(diǎn)的位數(shù)等信息,但是也并不是所有的信息都必須設(shè)置,通常只需要幾個(gè)常用設(shè)置侧馅。關(guān)于錄音設(shè)置詳見幫助文檔中的“AV Foundation Audio Settings Constants”危尿。
2、相關(guān)庫導(dǎo)入
查了好久 轉(zhuǎn)碼好使的就是opencore-amr 編譯的靜態(tài)庫這里已經(jīng)為大家整理好了馁痴。
點(diǎn)擊這里下載
打開是醬紫滴谊娇!導(dǎo)入項(xiàng)目中就OK了。
新項(xiàng)目有BUGB拊巍<没丁!
開發(fā)遇到BUG很正常小渊,請?jiān)敿?xì)閱讀報(bào)錯(cuò)信息法褥。我只遇到這一個(gè)錯(cuò)先給大家展示出來。
選中你的Targets 選中Build Setting 點(diǎn)擊搜索框搜索BitCode 改成No 就OK了酬屉。
另外還需要導(dǎo)入兩個(gè)庫
#import <AVFoundation/AVFoundation.h>
#import <AudioToolbox/AudioToolbox.h>
引入頭文件
#import "VoiceConverter.h"
3半等、音頻錄制
- 開始錄音
//根據(jù)當(dāng)前時(shí)間生成文件名
self.recordFileName = [ViewController GetCurrentTimeString];
//獲取路徑
NSString *documentsDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSInteger timeStamp = [[NSDate date] timeIntervalSince1970] * 1000;
self.recordFilePath = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.wav", @(timeStamp)]];
NSDictionary *recordSetting = [[NSDictionary alloc] initWithObjectsAndKeys:
[NSNumber numberWithFloat: 8000.0],AVSampleRateKey, //采樣率
[NSNumber numberWithInt: kAudioFormatLinearPCM],AVFormatIDKey,
[NSNumber numberWithInt:16],AVLinearPCMBitDepthKey,//采樣位數(shù) 默認(rèn) 16
[NSNumber numberWithInt: 1], AVNumberOfChannelsKey,//通道的數(shù)目
// [NSNumber numberWithBool:NO],AVLinearPCMIsFloatKey,//采樣信號是整數(shù)還是浮點(diǎn)數(shù)
// [NSNumber numberWithInt: AVAudioQualityMedium],AVEncoderAudioQualityKey,//音頻編碼質(zhì)量
nil];
//初始化錄音
self.recorder = [[AVAudioRecorder alloc]initWithURL:[NSURL fileURLWithPath:self.recordFilePath]
settings:recordSetting
error:nil];
//準(zhǔn)備錄音
if ([self.recorder prepareToRecord]){
[[AVAudioSession sharedInstance] setCategory: AVAudioSessionCategoryPlayAndRecord error:nil];
[[AVAudioSession sharedInstance] setActive:YES error:nil];
//開始錄音
if ([self.recorder record]){
//UI操作
}
}else{
NSLog(@"音頻格式出錯(cuò),Recorder---%@",self.recorder);
}
- 停止錄音
if (self.recorder.isRecording){
//停止錄音
[self.recorder stop];
}
- 生成當(dāng)前時(shí)間字符串
+(NSString*)GetCurrentTimeString{
NSDateFormatter *dateformat = [[NSDateFormatter alloc]init];
[dateformat setDateFormat:@"yyyyMMddHHmmss"];
return [dateformat stringFromDate:[NSDate date]];
}
- 生成文件路徑
+(NSString*)GetPathByFileName:(NSString *)_fileName ofType:(NSString *)_type{
NSString *directory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)objectAtIndex:0];
NSString* fileDirectory = [[[directory stringByAppendingPathComponent:_fileName]
stringByAppendingPathExtension:_type]
stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
return fileDirectory;
}
4、格式轉(zhuǎn)換
- WAV轉(zhuǎn)AMR
NSString *amrPath = [ViewController GetPathByFileName:self.recordFileName ofType:@"amr"];
if ([VoiceConverter ConvertAmrToWav:self.recordFilePath wavSavePath:amrPath]) {
//UI操作 播放音頻
}else{
NSLog(@"wav轉(zhuǎn)amr失敗");
}
- AMR轉(zhuǎn)WAV
NSString *convertedPath = [ViewController GetPathByFileName:[self.recordFileName stringByAppendingString:@"_AmrToWav"] ofType:@"wav"];
if ([VoiceConverter ConvertAmrToWav:amrPath wavSavePath:convertedPath]){
//UI操作 播放音頻
}else{
NSLog(@"amr轉(zhuǎn)wav 轉(zhuǎn)換失敗");
}
5呐萨、總結(jié)
由于時(shí)間關(guān)系暫時(shí)寫這么多 酱鸭,后面上傳會給大家補(bǔ)上。(未完待續(xù)垛吗。凹髓。。怯屉。)