ios CAF音频转换为MP3
Url:http://blog.csdn.net/ysy441088327/article/details/7392842#reply
为了达到 iPhone 与 Android 实现音频互通. 那么Mp3格式的音频文件再好不过了.
至于能够转换成Amr 是最好,10秒 的 一个Amr文件 只有5K左右的大小. 非常适合移动设备的数据传输
这里主要用到lame,一款非常棒的Mp3音频编码器.
那么在转换之前呢? 就需要先录制好音频文件,使用 AVAudioRecorder 进行音频录制之前,进行如下参数设置:
- (void)initialRecord
{
//录音权限设置,IOS7必须设置,得到AVAudioSession单例对象
AVAudioSession *audioSession = [AVAudioSession sharedInstance];
//设置类别,此处只支持支持录音
[audioSession setCategory:AVAudioSessionCategoryRecord error:nil];
//启动音频会话管理,此时会阻断后台音乐的播放
[audioSession setActive:YES error:nil];
//录音参数设置设置
NSMutableDictionary *recordSetting = [[NSMutableDictionary alloc]init];
//设置录音格式 AVFormatIDKey==kAudioFormatLinearPCM
// [recordSetting setValue:[NSNumber numberWithInt:kAudioFormatMPEG4AAC] forKey:AVFormatIDKey];
//caf的录制格式
[recordSetting setValue:[NSNumber numberWithInt:kAudioFormatLinearPCM] forKey:AVFormatIDKey];
//设置录音采样率(Hz) 如:AVSampleRateKey==8000/44100/96000(影响音频的质量)//acc的采样频率
// [recordSetting setValue:[NSNumber numberWithFloat:44100] forKey:AVSampleRateKey];
[recordSetting setValue:[NSNumber numberWithFloat:11025.0] forKey:AVSampleRateKey];
//录音通道数 1 或 2 在录制caf文件时,需要使用双通道,否则在转换为MP3格式时,声音不对
[recordSetting setValue:[NSNumber numberWithInt:2] forKey:AVNumberOfChannelsKey];
//线性采样位数 8、16、24、32
[recordSetting setValue:[NSNumber numberWithInt:16] forKey:AVLinearPCMBitDepthKey];
//录音的质量
// [recordSetting setValue:[NSNumber numberWithInt:AVAudioQualityHigh] forKey:AVEncoderAudioQualityKey];
[recordSetting setValue:[NSNumber numberWithInt:AVAudioQualityMin] forKey:AVEncoderAudioQualityKey];
//录音文件保存的URL
CFUUIDRef cfuuid = CFUUIDCreate(kCFAllocatorDefault);
NSString *cfuuidString = (NSString*)CFBridgingRelease(CFUUIDCreateString(kCFAllocatorDefault, cfuuid));
NSString * filename = [NSString stringWithFormat:@"%@.caf",cfuuidString];
NSString *audioRecordFilePath = AUDIORECORDFILEPATH(filename);
lastaudioRecordFilePath = audioRecordFilePath;
//判断目录是否存在不存在则创建
NSString *audioRecordDirectories = [audioRecordFilePath stringByDeletingLastPathComponent];
NSFileManager *fileManager=[NSFileManager defaultManager];
if (![fileManager fileExistsAtPath:audioRecordDirectories]) {
[fileManager createDirectoryAtPath:audioRecordDirectories withIntermediateDirectories:YES attributes:nil error:nil];
}
NSURL *url = [NSURL fileURLWithPath:audioRecordFilePath];
NSError *error=nil;
//初始化AVAudioRecorder
_recorder = [[AVAudioRecorder alloc]initWithURL:url settings:recordSetting error:&error];
if (error != nil) {
//NSLog(@"初始化录音Error: %@",error);
}else{
if ([_recorder prepareToRecord]) {
//录音最长时间
[_recorder recordForDuration:self.recorderDuration-1];
_recorder.delegate=self;
[_recorder record];
//开启音量检测
_recorder.meteringEnabled = YES;
//开启定时器,音量监测
_timer=[NSTimer scheduledTimerWithTimeInterval:0.05 target:self selector:@selector(volumeMeters:) userInfo:nil repeats:YES];
}
}
}
通过上面的参数所录制的音频文件体积非常大,不过不要担心,这只是第一步,只要成功转换成Mp3以后,可以保证文件体积每秒在4K左右.^^
另外一点,除非你时间多,那么没必要去尝试设置其他种类的参数再用来转换,因为作者我就试了不少,反正只有上面的参数才能保证,音质的完整和流畅.
下面介绍 lame 静态库 使用流程 主要有两个核心文件,使用很简单:
需要加入 lame.h
#include "lame.h"
//转编码为 mp3
- (void)audio_PCMtoMP3:(NSString *)cafFilePath andMP3FilePath:(NSString *)mp3FilePath
{
NSFileManager* fileManager=[NSFileManager defaultManager];
if([fileManager removeItemAtPath:mp3FilePath error:nil]) {
NSLog(@"删除");
} @try {
int read, write; FILE *pcm = fopen([cafFilePath cStringUsingEncoding:1], "rb"); //source 被转换的音频文件位置 if(pcm == NULL) {
NSLog(@"file not found");
} else {
fseek(pcm, 4*1024, SEEK_CUR); //skip file header
FILE *mp3 = fopen([mp3FilePath 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 = 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生成成功");
}
}
lamp下载区 密码: 3gy2
ios CAF音频转换为MP3的更多相关文章
- swift 录制多个音频 并将音频转换为mp3 并合成多个mp3文件为一个文件
我的需求是可以录制多个文件,最后生成的文件格式为mp3形式,查了下各种资料,因为swift无法直接将音频录制为mp3格式,所以最后我采取的解决方案为先将每个单独的文件转为mp3,最后逐一合并形成一个m ...
- lame,把ios录音转换为mp3格式
在ios设备中进行录音,录音文件的格式为caf.但这种格式在很多设备中没法播放.为了适应终端的播放功能,特将caf转换为mp3格式文件来使用. 在录制caf文件时,需要使用双通道,否则在转换为MP3格 ...
- PHP 将amr音频文件转换为mp3格式
说下整体思路 1.服务器安装ffmpeg 2.使用ffmpeg -i 指令来转换amr为mp3格式(这个到时候写在PHP代码中,使用exec函数执行即可) 3.在网页端使用HTML5的audio标签来 ...
- 【iOS 录音转码MP3及转码BASE64上传】
iOS 录音转码MP3及转码BASE64上传 一,开始录音 NSLog(@"开始录音"); [self startRecord]; - (void)startRecord { // ...
- iOS 9音频应用播放音频之iOS 9音频播放进度
iOS 9音频应用播放音频之iOS 9音频播放进度 iOS 9音频应用开发播放进度 音频文件在播放后经过了多久以及还有多久才可以播放完毕,想必是用户所关注的问题.为了解决这一问题,在很多的音乐播放器中 ...
- 在阿里云的CentOS 6.5 上面安装 timidity++ 和 ffmpeg(含libmp3lame) 实现命令行将midi转换为mp3
首先使用yum安装需要的组件 yum install -y automake autoconf libtool gcc gcc-c++ yasm yasm-devel 然后从sourceforge下载 ...
- iOS 9音频应用播放音频之音量设置与声道设置
iOS 9音频应用播放音频之音量设置与声道设置 iOS 9音频应用音量设置 音量又称响度.音强,是指人耳对所听到的声音大小强弱的主观感受,其客观评价尺度是声音的振幅大小.在iOS 9音频应用的应用中, ...
- iOS 9音频应用播放音频之第一个ios9音频实例2
iOS 9音频应用播放音频之第一个ios9音频实例2 ios9音频应用关联 iOS9音频应用中对于在主视图上添加的视图或控件,在使用它们时必须要与插座变量进行关联.ios9插座变量其实就是为主视图中的 ...
- iOS 9音频应用播放音频之第一个ios9音频实例
iOS 9音频应用播放音频之第一个ios9音频实例 第一个ios9音频实例 为了让开发者可以对上面的内容有更加深入的了解,本节将实现播放音频的第一个实例.在此实例中会涉及到项目的创建.界面设计.关联以 ...
随机推荐
- Ruby学习笔记(二)
1.block 代码块 do...end 或 {} 构成一个代码块,就像常见的 .each后面跟的代码块. my_nums = [1,2,3] my_double_nums = my_nums.col ...
- Imatest 崩溃
在使用Imatest时候,发现选取chart图的区域时候.Imatest停止工作,例如以下图.百度半天没有找到类似的问题,事实上这个问题在之前的公司也碰到过一回,卸载重N次,无效.如今又碰到了,问之前 ...
- Spring Data MongoDB example with Spring MVC 3.2
Spring Data MongoDB example with Spring MVC 3.2 Here is another example web application built with S ...
- HDU 1025 Constructing Roads In JGShining's Kingdom (DP)
Problem Description JGShining's kingdom consists of 2n(n is no more than 500,000) small cities which ...
- 第八条——覆盖equals方法时需遵守的通用约定
1)自反性 对于任何非null的引用值x,x.equals(x)必须返回true.---这一点基本上不会有啥问题 2)对称性 对于任何非null的引用值x和y,当且仅当x.equals(y)为true ...
- java中关于public class
在编写类的时候可以使用两种方式定义类: public class定义类: class定义类: 1,如果一个类声明的时候使用了public class进行了声明,则类名称必须与文件名称完 ...
- isAssignableFrom与instanceof的区别
1.isAssignableFrom针对的是class对象: 2.instanceof是实例. isAssignableFrom是用来判断一个类Class1和另一个类Class2是否相同或是另一个类的 ...
- 知识点摸清 - - position属性值之relative与absolute
两者共同特点是: 改变文档流 激活元素left.top.right.bottom.z-index属性 让元素”浮起来“,z-index>0 不同的是: 1.position:relative 会 ...
- 原生JS+tween.js模仿微博发布效果
转载请注明出处:http://www.cnblogs.com/zhangmingze/p/4816865.html 1.先看效果吧,有效果才有动力: 2.html结构: <!DOCTYPE ht ...
- weex-toolkit打包
需要2.15.1及以上的npm支持 运行终端,查看版本 npm --version 如果版本低于2.15.1,需要更新 sudo npm install -g npm 安装weex-toolkit n ...