PCM转MP3工具的封装
PCM转MP3工具的封装
说明
1. 对 PCM 转 MP3 进行了简单的封装.
2. 使用 https://github.com/wuqiong/mp3lame-for-iOS 生成支持64位的 lame 库.
源码
https://github.com/YouXianMing/iOS-General-Tools 中的 PCM-to-MP3
// // PcmToMp3Manager.h // RecordMusic // // Created by YouXianMing on 16/7/28. // Copyright © 2016年 YouXianMing. All rights reserved. // // Lame-for-iOS https://github.com/wuqiong/mp3lame-for-iOS // #import <Foundation/Foundation.h> @class PcmToMp3Manager; @protocol PcmToMp3ManagerDelegate <NSObject> @optional /** * Did convert the pcm to mp3. * * @param manager The PcmToMp3Manager object. * @param sucess Sucess or not. * @param errorInfo Error info. */ - (void)didConvertPcmToMp3:(PcmToMp3Manager *)manager sucess:(BOOL)sucess errorInfo:(NSString *)errorInfo; @end /** * In "Build Phases", You can add '-Wno-shorten-64-to-32' to the file 'PcmToMp3Manager.m' to ignore the warning. */ @interface PcmToMp3Manager : NSObject /** * The PcmToMp3Manager's delegate. */ @property (nonatomic, weak) id <PcmToMp3ManagerDelegate> delegate; /** * The pcm file's path. */ @property (nonatomic, strong) NSString *pcmFilePath; /** * The mp3 file's path you specified. */ @property (nonatomic, strong) NSString *mp3FilePath; /** * Before you start convert, you should specified the pcm file's path. */ - (void)startConvert; @end
// // PcmToMp3Manager.m // RecordMusic // // Created by YouXianMing on 16/7/28. // Copyright © 2016年 YouXianMing. All rights reserved. // #import "PcmToMp3Manager.h" #import <lame/lame.h> @implementation PcmToMp3Manager - (void)startConvert { NSParameterAssert(self.pcmFilePath); BOOL isDirectory = NO; BOOL isExist = [[NSFileManager defaultManager] fileExistsAtPath:self.pcmFilePath isDirectory:&isDirectory]; if (isExist && isDirectory == NO) { dispatch_async(dispatch_get_global_queue(0, 0), ^{ @try { int read, write; FILE *pcm = fopen([self.pcmFilePath cStringUsingEncoding:1], "rb"); //source fseek(pcm, 4*1024, SEEK_CUR); //skip file header FILE *mp3 = fopen([self.mp3FilePath cStringUsingEncoding:1], "wb"); //output 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, 44100); 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) { if (self.delegate && [self.delegate respondsToSelector:@selector(didConvertPcmToMp3:sucess:errorInfo:)]) { dispatch_async(dispatch_get_main_queue(), ^{ [self.delegate didConvertPcmToMp3:self sucess:NO errorInfo:exception.description]; }); } } @finally { if (self.delegate && [self.delegate respondsToSelector:@selector(didConvertPcmToMp3:sucess:errorInfo:)]) { dispatch_async(dispatch_get_main_queue(), ^{ [self.delegate didConvertPcmToMp3:self sucess:YES errorInfo:nil]; }); } } }); } else { if (self.delegate && [self.delegate respondsToSelector:@selector(didConvertPcmToMp3:sucess:errorInfo:)]) { dispatch_async(dispatch_get_main_queue(), ^{ [self.delegate didConvertPcmToMp3:self sucess:NO errorInfo:[NSString stringWithFormat:@"'%@' not exist.", self.pcmFilePath]]; }); } } } @end
细节
为了去除 PcmToMp3Manager 的 warning, 在文件 PcmToMp3Manager.m 添加 -Wno-shorten-64-to-32 即可