最近折腾了swift的语音录制识别和转码,这块还是比较坑的,由于语音识别的准确度实测大概也就80%左右,所以还是需要上传录音文件啊。
首先是用讯飞语音SDK实现语音录制和识别(语音听写),第一个坑是讯飞SDK只录制了PCM格式的文件,这个文件是原始格式,默认比较大,另外播放器支持也不好,因此需要先把它转成mp3,本来考虑使用系统的AudioConverter转aac格式,不过aac好像不能在浏览器上播放。
转成mp3需要lame库支持,注意国内网搜到的lame.a库不支持64位,所以现在不能用了。
还好已经有人做了这个事情,直接提供了最新编译脚本和编译好的framework库,地址是https://github.com/wuqiong/mp3lame-for-iOS
我直接用了上面编译的framework,没有自己去编译,直接把lame.framework拖到工程里。
然后需要用oc写个封装类,我不确定这个封装类能不能用swift写,毕竟里面用了很多c的语法,还是用oc桥接一层比较保险。oc封装类如下:
- #import <Foundation/Foundation.h>
- #import "AudioWrapper.h"
- #import "lame/lame.h"
- @implementation AudioWrapper
- + (void)audioPCMtoMP3 :(NSString *)audioFileSavePath :(NSString *)mp3FilePath
- {
- @try {
- int read,write;
- FILE *pcm = fopen([audioFileSavePath 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,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_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 converted: %@",mp3FilePath);
- }
- }
- @end
然后在桥接文件XXX-Bridging-Header.h中加入
#import "AudioWrapper.h"
我觉得在主线程调用容易出问题,就新开了个线程调用,实测没有问题。