文件下载整个功能完成了,那么对应的文件上传也跑不了。So~ Look here~


业务需求是录制音频然后上传到七牛并且Android可以读。

与安卓沟通了一下统一了mp3格式,大小质量都不错。由于AVAudioRecorder录音的格式为.caf或者.wav而且很大需要进行转换压缩为MP3格式。这里需要用到三方库 lame

使用lame转换后音频的质量和

 _recorder = [[AVAudioRecorder alloc] initWithURL:_recordFilePath settings:setting error:NULL];

里的 setting 息息相关。 所以整理了两个配置。

我把这两种的配置写在了工具类所以直接贴代码了~要用的话直接CV大法就可以。

lame三方库的资源


  • 获取转换文件所在文件夹
+ (NSString *)getRecPathUrl{
NSString *str = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject]; NSString *recordDir = [str stringByAppendingPathComponent:@"RecordCourse"]; return recordDir;
}
  • 获取时间戳用于文件的命名
+ (NSString *)getTimestamp{
NSDate *nowDate = [NSDate date];
double timestamp = (double)[nowDate timeIntervalSince1970]*1000;
long nowTimestamp = [[NSNumber numberWithDouble:timestamp] longValue];
NSString *timestampStr = [NSString stringWithFormat:@"%ld",nowTimestamp];
return timestampStr;
}
  • PCM转换MP3配置
+ (NSDictionary *)getAudioSettingWithPCM {
NSMutableDictionary *dic = [NSMutableDictionary dictionary];
//设置录音格式
[dic setObject:@(kAudioFormatLinearPCM) forKey:AVFormatIDKey];
//设置录音采样率,8000是电话采样率,对于一般录音已经够了
[dic setObject:@(44100.0) forKey:AVSampleRateKey];
//设置通道,这里采用双声道
[dic setObject:@(1) forKey:AVNumberOfChannelsKey];
//每个采样点位数,分为8、16、24、32
[dic setObject:@(16) forKey:AVLinearPCMBitDepthKey];
//是否使用浮点数采样
[dic setObject:@(YES) forKey:AVLinearPCMIsFloatKey];
//....其他设置等
return dic;
}
  • CAF转换MP3配置
+ (NSDictionary *)getAudioSettingWithCAF {
NSDictionary *setting = [NSDictionary dictionaryWithObjectsAndKeys:
[NSNumber numberWithInt:AVAudioQualityMin],
AVEncoderAudioQualityKey,
[NSNumber numberWithInt:16],
AVEncoderBitRateKey,
[NSNumber numberWithInt:2],
AVNumberOfChannelsKey,
[NSNumber numberWithFloat:44100.0],
AVSampleRateKey,
nil]; return setting;
}
  • PCM转换MP3的lame方法
+ (NSString *)audioPCMtoMP3:(NSString *)wavPath {
NSString *cafFilePath = wavPath; 大专栏  录音文件lame转换MP3相关配置NSString *mp3FilePath = [NSString stringWithFormat:@"%@.mp3",[NSString stringWithFormat:@"%@%@",[cafFilePath substringToIndex:cafFilePath.length - 4],[self getTimestamp]]]; NSFileManager* fileManager = [NSFileManager defaultManager];
if([fileManager removeItemAtPath:mp3FilePath error:nil]){
NSLog(@"删除原MP3文件");
}
@try {
int read, write;
FILE *pcm = fopen([cafFilePath cStringUsingEncoding:1], "rb"); //source 被转换的音频文件位置
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, 22050.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 {
return mp3FilePath;
}
}
  • CAF转换MP3的lame方法
+ (NSString *)audioCAFtoMP3:(NSString *)wavPath {

    NSString *cafFilePath = wavPath;

    NSString *mp3FilePath = [NSString stringWithFormat:@"%@.mp3",[NSString stringWithFormat:@"%@%@",[cafFilePath substringToIndex:cafFilePath.length - 4],[self getTimestamp]]];

    @try {
int read, write; FILE *pcm = fopen([cafFilePath cStringUsingEncoding:1], "rb"); //source 被转换的音频文件位置
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_num_channels(lame,1);//设置1为单通道,默认为2双通道
lame_set_in_samplerate(lame, 44100.0);
lame_set_VBR(lame, vbr_default); lame_set_brate(lame,8); lame_set_mode(lame,3); lame_set_quality(lame,2); 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 {
return mp3FilePath;
}
}

两种质量大小不错都可以使用

录音文件lame转换MP3相关配置的更多相关文章

  1. 微信录音文件上传到服务器以及amr转化成MP3格式,linux上转换简单方法

    微信公众号音频接口开发 根据业务需求,我们可能需要将微信录音保存到服务器,而通过微信上传语音接口上传到微信服务器的语音文件的有效期只有3天,所以需要将文件下载到我们自己的服务器. 上传语音接口 wx. ...

  2. 微信录音文件上传到服务器以及amr转化成MP3格式

    微信公众号音频接口开发 根据业务需求,我们可能需要将微信录音保存到服务器,而通过微信上传语音接口上传到微信服务器的语音文件的有效期只有3天,所以需要将文件下载到我们自己的服务器. 上传语音接口 wx. ...

  3. spring mvc 图片上传,图片压缩、跨域解决、 按天生成文件夹 ,删除,限制为图片代码等相关配置

    spring mvc 图片上传,跨域解决 按天生成文件夹 ,删除,限制为图片代码,等相关配置 fs.root=data/ #fs.root=/home/dev/fs/ #fs.root=D:/fs/ ...

  4. MP3 Lame 转换 参数 设置(转)

    我们在对音频格式的转换中,打交道最多的就是MP3了.如果你能彻底玩转MP3,那么对你的音频创作和对其他音频格式的掌握会有很大的帮助.下面我们给大家介绍MP3制作软件:LAME 要制作出高音质的MP3靠 ...

  5. ios开发之Info.plist文件相关配置

    前言:在iOS开发中有些情况下需要对Info.plist文件进行配置,以下介绍几种相关配置.以后遇到需要配置的再更新... 开发环境:swift3.0.1,Xcode8.1 一,项目中需要使用第三方字 ...

  6. SpringCloud系列六:Feign接口转换调用服务(Feign 基本使用、Feign 相关配置)

    1.概念:Feign 接口服务 2.具体内容 现在为止所进行的所有的 Rest 服务调用实际上都会出现一个非常尴尬的局面,例如:以如下代码为例: Dept dept = this.restTempla ...

  7. apache的.htaccess文件作用和相关配置

    首先.htaccess什么? .htaccess是一个纯文本文件,它里面存放着Apache服务器配置相关的指令. 当我们使用apache部署一个网站代码准备部署到网上的时候,我们手中的apache的h ...

  8. generator.xml文件与相关配置插件

    一,generator.xml配置信息 1 <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE ...

  9. 【spring boot logback】日志使用自定义的logback-spring.xml文件后,application.properties中关于日志的相关配置还会起作用么

    本篇 将针对[日志使用自定义的logback-spring.xml文件后,application.properties中关于日志的相关配置还会起作用么]这一个主题进行探索. 这个测试项目是根据[spr ...

随机推荐

  1. jquery选择器之获取父级元素、同级元素、子元素

    一.获取父级元素 1. parent([expr]): 获取指定元素的所有父级元素 二.获取同级元素: 1.next([expr]): 获取指定元素的下一个同级元素 2.nextAll([expr]) ...

  2. Maven打包时报Failed to execute goal org.apache.maven.plugins:maven-war-plugin:解决方案

    问题现象: 用Maven打包时,报Failed to execute goal org.apache.maven.plugins:maven-war-plugin:2.2:war错误. 原因分析: 打 ...

  3. python解一元一次方程

    将未知数看成是虚数 将常数看成是实数 最终求解. import re class Item: def __init__(self,imag=0,real=0): self.imag = imag se ...

  4. php time()时间戳作为文件名产生文件同名的bug

    /*time()函数生成的文件名可能是相同的,因为如果php运行的过程如果足够快,time()函数调用的足够频繁,那么有可能time()生成的时间戳会相同,因为时间戳是以秒为单位,所以如果足够频繁有可 ...

  5. zabbix监控Linux服务器丢包率

    http://www.ttlsa.com/zabbix/zabbix-simple-checks/  这个文章看了,还没有实践 1.先创建监控项,键值如下 icmppingloss[<121.1 ...

  6. Regex: positive lookahead 先行断言____ 后行断言(lookbehind)

    先行断言: /a(?=b)/  ,positive lookahead,a的后方必须是b才行 /a(?!b)/   ,negative lookahead,a的后方必须不是b才能匹配 如下图示:  来 ...

  7. 以puppeteer抓取微指数,puppeteer基本示例,docker部署headless

    还是直接上代码 https://github.com/cclient/weizhishu-puppeteer 根据关键字获取微博指数 早期版本以常规的构造cookie,token,sign的访问api ...

  8. 系统学习Javaweb8----JavaScript4(结束)

    学习内容: 1.DOM对象 1.2DOM对象--元素对象常见属性 2.JS事件 2.1JS事件--入门案例 2.2JS事件--驱动机制 2.3常见JS事件--点击事件 2.4常见JS事件--点击事件 ...

  9. VirtualBox虚拟机Ubuntu设置共享文件夹,并自动挂载

    一.环境 Win10系统,VirtualBox-5.1.22-115126+Ubuntu16.04(64位)虚拟机   二.目的 在Ubuntu中能够共享Win10中的某个文件夹,而且能够自动挂载   ...

  10. LG_2967_[USACO09DEC]视频游戏的麻烦Video Game Troubles

    题目描述 Farmer John's cows love their video games! FJ noticed that after playing these games that his c ...