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(, ), ^{ @try { int read, write; FILE *pcm = fopen([self.pcmFilePath cStringUsingEncoding:], "rb"); //source
fseek(pcm, *, SEEK_CUR); //skip file header
FILE *mp3 = fopen([self.mp3FilePath cStringUsingEncoding:], "wb"); //output const int PCM_SIZE = ;
const int MP3_SIZE = ;
short int pcm_buffer[PCM_SIZE * ];
unsigned char mp3_buffer[MP3_SIZE]; lame_t lame = lame_init();
lame_set_in_samplerate(lame, );
lame_set_VBR(lame, vbr_default);
lame_init_params(lame); do {
read = fread(pcm_buffer, * sizeof(short int), PCM_SIZE, pcm); if (read == ) { 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, , mp3); } while (read != ); 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 即可

最新文章

  1. hibernate(2) —— 主键策略
  2. request 获取服务根目录地址
  3. iOS生成静态库方法
  4. ARM 汇编寻址方式
  5. Redis Sentinel:集群Failover解决方案(转载)
  6. php防止ddos,dns,集群攻击的实现代码
  7. ActionBar官方教程(2)选主题让应用支或不支持ActionBar及支持ActionBar的应用如何隐藏和显示
  8. C#语言中的修饰符
  9. Angular记录(7)
  10. python中的双向链表实现
  11. C#大型电商项目优化(三)——扩展性与支付
  12. 获取url后面的参数的方法
  13. ubuntu下搭建node server的几个坑
  14. UVa 10129 Play on Words(并查集+欧拉路径)
  15. 了不起的Node.js--之一
  16. [LeetCode] 103. Binary Tree Zigzag Level Order Traversal _ Medium tag: BFS
  17. 使用System.Net.Mail发送邮件
  18. 封装scrollView 循环滚动,tableViewCell(连载) mvc
  19. 【比赛】HNOI2018 排列
  20. 在web应用中使用文件

热门文章

  1. 执行了‘“npm install mysql&quot;
  2. 【BZOJ2059】Buying Feed 购买饲料
  3. Tomcat8 启动中提示 org.apache.catalina.webresources.Cache.getResource Unable to add the resource
  4. 项目通过https访问的tomcat相关配置
  5. 【LOJ】#2523. 「HAOI2018」奇怪的背包
  6. 浅谈2-SAT(待续)
  7. HttpServlet Service方法
  8. [ 转载 ] Http详解2
  9. codeforces 596E Wilbur and Strings
  10. ElasticSearch-.net平台下c#操作ElasticSearch详解