image
大多數(shù)代碼源自于 ExtAudioFileConverter长豁,因?yàn)榫W(wǎng)上關(guān)于音頻格式轉(zhuǎn)換的太少了瞳别,而且還特別麻煩,故封裝為
Cocoapods
庫壹蔓,方便使用趟妥,轉(zhuǎn)換MP3
格式需要用到lame
靜態(tài)庫,使用pod
之后不需要單獨(dú)集成lame
靜態(tài)庫佣蓉,直接pod install
即可使用披摄,這是庫的地址:GZZAudioConverter 。
Installation
僅支持 CocoaPods
勇凭,將如下代碼加入 Podfile
文件中疚膊。
pod 'GZZAudioConverter'
Useage
// 例如:轉(zhuǎn)換為 MP3
dispatch_async(dispatch_queue_create(NULL, DISPATCH_QUEUE_SERIAL), ^ {
GZZAudioConverter *converter = [[GZZAudioConverter alloc] init];
converter.inputFile = @"/var/mobile/Containers/Data/Application/xxx/Documents/xxx.wav";
converter.outputFile = @"/var/mobile/Containers/Data/Application/xxx/Documents/xxx.mp3";
converter.outputFileType = kAudioFileMP3Type;
converter.outputFormatID = kAudioFormatMPEGLayer3;
BOOL success = [converter convert];
dispatch_async(dispatch_get_main_queue(), ^ {
if (success) {
NSLog(@"轉(zhuǎn)換成功");
} else {
NSLog(@"轉(zhuǎn)換失敗");
}
});
});
Methods
注意:文件格式和數(shù)據(jù)格式要根據(jù)你要轉(zhuǎn)換的音頻格式來設(shè)置,不同文件格式對(duì)應(yīng)的數(shù)據(jù)格式是不同的套像,這是關(guān)于文件格式和數(shù)據(jù)格式的 說明文檔 酿联。
// 采樣率
converter.outputSampleRate = 44100;
// 聲道數(shù)
converter.outputNumberChannels = 2;
// 位深
converter.outputBitDepth = BitDepth_16;
// 文件格式
converter.outputFileType = kAudioFileWAVEType;
// 數(shù)據(jù)格式
converter.outputFormatID = kAudioFormatLinearPCM;
將來的你,一定會(huì)感激現(xiàn)在拼命的自己夺巩,愿自己與讀者的開發(fā)之路無限美好贞让。