首先微信有兩種錄制語音方式
1.wx.startRecord? 得到的文件是silk格式的,從版本1.6.0開始荐吵,微信就不再維護(hù)這個(gè)接口了
2.wx.getRecorderManager? 得到的文件是mp3格式的骑冗,建議使用這個(gè)方式來錄音
const manager = wx.getRecorderManager();
startRecord: function(e){
? ? const options = {
? ? ? durations: 10000,
? ? ? sampleRate: 16000,
? ? ? numberOfChannels: 1,
? ? ? encodeBitRate: 96000,
? ? ? format: 'mp3',
? ? ? frameSize: 50
? ? }
? ? manager.start(options);
? ? var that = this;
? ? manager.onStart(() => {
? ? ? console.log('record start')
? ? })
? },
endRecord: function(e){
? ? var that = this;
? ? var filePath = '';
? ? manager.stop();
? ? manager.onStop((res) => {
? ? ? filePath = res.tempFilePath
? ? ? wx.uploadFile({
????????...//上傳到后臺(tái)處理
? ? ? ? success: function (res) {
? ? ? ? ? ? that.setData({
? ? ? ? ? ? ? text: res.result
? ? ? ? ? ? })
? ? ? ? },
? ? ? ? fail: function (res) {
? ? ? ? }
? ? ? })
? ? })
? }
這樣我們就成功拿到錄音拉赊瞬,然后語音識(shí)別我用的是百度的API,JAVA SDK贼涩;
看看都會(huì)的拉巧涧,就是有一個(gè)問題,百度語音在識(shí)別的時(shí)候遥倦,只支持PCM WAV AMR這三個(gè)格式谤绳,但是我們拿到的是MP3的格式,就需要轉(zhuǎn)換一下格式
解決方案 鏈接袒哥,使用這位老鐵的方式有一個(gè)問題就是轉(zhuǎn)換格式后的語音根本識(shí)別不了缩筛,然后我就自己設(shè)置了一下參數(shù)
//AudioFormat baseFormat = in.getFormat();
//targetFormat = new AudioFormat(AudioFormat.Encoding.PCM_SIGNED, baseFormat.getSampleRate(), 16, baseFormat.getChannels(), baseFormat.getChannels() * 2, baseFormat.getSampleRate(), false);
targetFormat = new AudioFormat(AudioFormat.Encoding.PCM_SIGNED, 16000, 16,1, 50, 16000, false);
就好了,就是注意下這些參數(shù)堡称,在錄音的時(shí)候需要配置瞎抛,轉(zhuǎn)換的時(shí)候也有,還有識(shí)別的時(shí)候也有却紧。桐臊。