微信小程式語音識別開發過程筆記

2022-09-09 03:54:10 字數 2779 閱讀 4533

在小程式中識別使用者語音輸入的命令

錄音儲存-->上傳錄音檔案到伺服器後台-->讀取伺服器返回結果

關鍵**:

//按鍵按下事件

startrec:function(res)

else

// 顯示識別成功

wx.showtoast()

},// 上傳識別

fail:function())

}});

wx.playvoice(

})},

fail: function (res)

})settimeout(function () , 10000)

},

安裝指南:

依賴元件:gcc、ffmpeg

git clone 

cd silk*/silk

make

#若果有waring不用擔心,看看目錄下有沒有生成二級制檔案decoder

# 使用上個目錄的sh指令碼,把1.silk轉偉1.w**,這兩個檔案在同乙個目錄

cd ..

sh converter.sh 1.slk w**

# 看看有沒有生成1.w**

62 $cur_dir/silk/decoder "$1" "$1.pcm" > /dev/null 2>&1

63 if [ ! -f "$1.pcm" ]; then

64 ffmpeg -y -i "$1" "$.$2" > /dev/null 2>&1 &

65 ffmpeg_pid=$!

66 while kill -0 "$ffmpeg_pid"; do sleep 1; done > /dev/null 2>&1

67 [ -f "$.$2" ]&&echo -e "$[ok]$ convert $1 to $.$2 success, $but not a silk v3 encoded file.$"& &exit

68 echo -e "$[warning]$ convert $1 false, maybe not a silk v3 encoded file."&&exit

69 fi

70 #ffmpeg -y -f s16le -ar 12000 -ac 2 -i "$1.pcm" "$.$2" > /dev/null 2>&1

71 ffmpeg -y -f s16le -ar 12000 -ac 2 -i "$1.pcm" -f w** -ar 16000 -ac 1 "$.$2" > /dev/null 2>&1

72 ffmpeg_pid=$!

73 while kill -0 "$ffmpeg_pid"; do sleep 1; done > /dev/null 2>&1

74 rm "$1.pcm"

75 [ ! -f "$.$2" ]&&echo -e "$[warning]$ convert $1 false, maybe ffmpeg no format handler for $2."&&exit

76 echo -e "$[ok]$ convert $1 to $.$2 finish."

77 exit

def voice_detec():

if request.method == 'post':

# print('post:',request.form)

# 接受小程式上傳的檔案

voice_file = request.files['voice']

if voice_file:

# 儲存到磁碟

voice_file.s**e('2.silk')

# 轉碼,這裡需要注意檔案放的位置,將在下面給出檔案位置

msg=os.system('sudo sh silk-v3-decoder/converter.sh ../2.silk w**')

print(msg)

f=open('2.w**','rb')

# 呼叫api識別

# 返回識別結果

return json.dumps(result)

return ''

整個伺服器專案的目錄結構:

webproject

├── 2.silk # 伺服器上傳的檔案包存2.silk

├── 2.w** #轉碼結果

語音識別api配置:這個直接看官方文件吧,很清楚。

不要企圖使用電腦客戶端的快取的.silk去轉換,和真機環境不是乙個型別

使用python2的朋友可能會面臨字串轉碼的問題,頭痛

silk-v3-decoder 編譯過程報錯或者警告問題

微信小程式 開發過程記錄

在page的js檔案中,可以使用selectcomponent來獲取自定義元件,但注意如果該元件處於隱藏狀態得到的將是null 在父元件中追加 bind custom event name function name 在子元件中激發事件 this.triggerevent custom event ...

微信小程式開發過程的簡易教程

微信小程式開發的第一天 0.太麻煩了,先看一遍官方手冊吧 1.微信開發環境的安裝 1.安裝開發者工具 2.安裝nodejs的環境 2.程式的目錄和結構 當使用快速開始的模板的時候,會自動的生成幾個檔案 1.json字尾的json配置檔案 2.wxml html 3.wxss css 4.js js檔...

微信退款開發過程

商戶號 mch id 隨機字串 nonce str 商戶訂單號 out trade no 商戶退單號 out refund no 退款總金額 refund fee 訂單總金額 total fee 操作員帳號 op user id 商戶平台中對應的key值 key 注意事項 1.訂單總金額和退款總金額...