文件下載整個功能完成了,那么對應(yīng)的文件上傳也跑不了。So~ Look here~
業(yè)務(wù)需求是錄制音頻然后上傳到七牛并且Android可以讀。
與安卓溝通了一下統(tǒng)一了mp3格式,大小質(zhì)量都不錯。由于AVAudioRecorder錄音的格式為.caf或者.wav而且很大需要進行轉(zhuǎn)換壓縮為MP3格式。這里需要用到三方庫 lame。
使用lame轉(zhuǎn)換后音頻的質(zhì)量和
_recorder = [[AVAudioRecorder alloc] initWithURL:_recordFilePath settings:setting error:NULL];
里的 setting 息息相關(guān)。 所以整理了兩個配置。
我把這兩種的配置寫在了工具類所以直接貼代碼了~要用的話直接CV大法就可以。
- 獲取轉(zhuǎn)換文件所在文件夾
+ (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轉(zhuǎn)換MP3配置
+ (NSDictionary *)getAudioSettingWithPCM {
NSMutableDictionary *dic = [NSMutableDictionary dictionary];
//設(shè)置錄音格式
[dic setObject:@(kAudioFormatLinearPCM) forKey:AVFormatIDKey];
//設(shè)置錄音采樣率,8000是電話采樣率,對于一般錄音已經(jīng)夠了
[dic setObject:@(44100.0) forKey:AVSampleRateKey];
//設(shè)置通道,這里采用雙聲道
[dic setObject:@(1) forKey:AVNumberOfChannelsKey];
//每個采樣點位數(shù),分為8、16、24、32
[dic setObject:@(16) forKey:AVLinearPCMBitDepthKey];
//是否使用浮點數(shù)采樣
[dic setObject:@(YES) forKey:AVLinearPCMIsFloatKey];
//....其他設(shè)置等
return dic;
}
- CAF轉(zhuǎn)換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轉(zhuǎn)換MP3的lame方法
+ (NSString *)audioPCMtoMP3:(NSString *)wavPath {
NSString *cafFilePath = wavPath;
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 被轉(zhuǎn)換的音頻文件位置
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轉(zhuǎn)換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 被轉(zhuǎn)換的音頻文件位置
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);//設(shè)置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;
}
}
兩種質(zhì)量大小不錯都可以使用????
歡迎光臨我的個人博客