参考文章:https://www.jianshu.com/p/c1bdab0ddf59
caf文件可以在苹果手机上正常播放,但是不能在安卓手机上播放,所以我们需要将caf转为mp3。
在iOS中使用lame静态库进行压缩。
lame静态库
lame是一个开源的MP3音频压缩软件,lame是一个递归缩写,来自Lame Ain't an MP3 Encoder(LAME不是MP3编码器),它自1998年以来由一个开源社区开发,目前是工人有损品质MP3中压缩效果最好的编码器。
使用步骤
第一步:下载lame的最新版本并解压
下载完成后,如下图3所示,双节解压该文件,如图所示
![](https://img.haomeiwen.com/i6942160/ede67597c2ef651c.png)
解压后,如下图
![](https://img.haomeiwen.com/i6942160/e5a93fd4dfff5dab.png)
这些文件均不可直接使用,需要打包成静态库来使用,但是这里和我们之前打包静态库不太一样,这里有很多spec文件,没法直接打包,这个时候我们需要通过一个脚本文件来打包。
第二步: 生成静态库
2.1 下载build的脚本
2.2 在桌面新建一个lamebuild的文件夹
2.3 将下载并解压的lame库拖拽到lamebuild文件夹下
2.4 将下载的lame-ios-build脚本也拖拽到lamebuild文件夹下
文件结构如下图5所示:
![](https://img.haomeiwen.com/i1672235/cac25f233c4ec5e9.png)
2.5 在终端执行
allisondeMacBook-Pro:~ allison$ cd /Users/allison/Desktop/lamebuild
allisondeMacBook-Pro:lamebuild allison$ ./build.sh
编译中...
building arm64...
configure: WARNING: if you wanted to set the --build type, don't use --host.
If a cross compiler is detected then cross compile mode will be used
......
Making install in vc_solution
make[2]: Nothing to be done for `install-exec-am'.
make[2]: Nothing to be done for `install-data-am'.
make[2]: Nothing to be done for `install-exec-am'.
make[2]: Nothing to be done for `install-data-am'.
building fat binaries...
编译完成后,需要查看是否编译成功,打开lamebuild文件下,发现下面多了一个fat-lame,查看里面的lib文件夹,发现有一个大包出来的静态库,如图6
![](https://img.haomeiwen.com/i1672235/42231e6ae3cafff3.png)
此时,还可查看该库支持的架构
allisondeMacBook-Pro:lamebuild allison$ cd /Users/allison/Desktop/lamebuild/fat-lame/lib
allisondeMacBook-Pro:lib allison$ lipo -info libmp3lame.a
Architectures in the fat file: libmp3lame.a are: i386 armv7 armv7s x86_64 arm6
第三步: 导入静态库工程,开始使用。
3.1 将lame.h和libmp3lame.a文件,均拖入项目中
第四步:创建caf转mp3工具类
LameTool.h
#import <Foundation/Foundation.h>
@interface LameTool : NSObject
//cafFilePath为caf文件本地地址,mp3FilePath为要保存mp3文件的本地地址
+ (BOOL)audioPCMtoMP3:(NSString *)cafFilePath mp3FilePath:(NSString *)mp3FilePath;
@end
LameTool.m
#import "LameTool.h"
#import "lame.h"
@implementation LameTool
+ (BOOL)audioPCMtoMP3:(NSString *)cafFilePath mp3FilePath:(NSString *)mp3FilePath{
if ([[NSFileManager defaultManager] fileExistsAtPath:mp3FilePath]) {
if([[NSFileManager defaultManager] removeItemAtPath:mp3FilePath error:nil]){
NSLog(@"删除原MP3文件");
}
}
BOOL isSuccess = YES;
@try {
int read, write;
FILE *pcm = fopen([cafFilePath cStringUsingEncoding:NSUTF8StringEncoding], "rb"); //source 被转换的音频文件位置
fseek(pcm, 4*1024, SEEK_CUR); //skip file header
FILE *mp3 = fopen([mp3FilePath cStringUsingEncoding:NSUTF8StringEncoding], "wb+"); //output 输出生成的Mp3文件位置
if (pcm != NULL && mp3 != NULL) {
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, 8000.0);
lame_set_VBR(lame, vbr_default);
lame_set_num_channels(lame,2);//默认为2双通道
lame_set_brate(lame,8);
lame_set_mode(lame,3);
lame_set_quality(lame,5); /* 2=high 5 = medium 7=low 音质*/
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);
}
if (mp3 != NULL) {
fwrite(mp3_buffer, write, 1, mp3);
}
} while (read != 0);
lame_close(lame);
fclose(mp3);
fclose(pcm);
}else{
if (pcm) {
fclose(pcm);
}
if (mp3) {
fclose(mp3);
}
isSuccess = NO;
}
}@catch (NSException *exception) {
NSLog(@"%@",[exception description]);
isSuccess = NO;
}@finally {
}
return isSuccess;
}
网友评论