利用7688上的LineBot實作千里傳音功能
USB的音效卡加上OTG的線,插入7688的USB HOST的接孔
USB音效卡,喇叭線插入耳機插孔中
若有7688的grove擴展板,上面也有耳機插孔可用
1、安裝驅動程式
2、執行以下指令,如果有看到如下的USB Audio Device的東西,表示音效卡已安裝完成
3、試試看是否能正常播放mp3音樂,音效卡播放mp3的指令
以上指令以播放stream.mp3這個檔案為例,若要播放其他mp3請自行修改,並將mp3的檔案傳上7688試播。
# coding=utf-8import requestsimport subprocess,timefrom flask import Flask, request, abortfrom linebot import ( LineBotApi, WebhookHandler)from linebot.exceptions import ( InvalidSignatureError)from linebot.models import ( MessageEvent, TextMessage, TextSendMessage,)#這邊的port可以修改成你要想run的portmyPort=8090app = Flask(__name__)#請修改以下的程式,加入自己Line的channelAccessToken以及channelSecretline_bot_api = LineBotApi('這裡改成自己Line的channelAccessToken')handler = WebhookHandler('這裡改成自己Line的channelSecret')@app.route("/", methods=['POST'])def index(): # get X-Line-Signature header value signature = request.headers['X-Line-Signature'] # get request body as text body = request.get_data(as_text=True) app.logger.info("Request body: " + body) # handle webhook body try: handler.handle(body, signature) except InvalidSignatureError: abort(400) return 'OK'#當收到Line傳來的文字訊息後,將該文字訊息合併進要下載google姐聲音的網址內成為stream_url這個字串#python連結進該網址將stream的聲音儲存成stream.mp3#然後python呼叫外部指令madplay stream.mp3 -o wave:- | aplay -D plughw:1,0播放聲音#最後再將傳來的文字回傳給Line,表示已經收到,並且已播放聲音完畢@handler.add(MessageEvent, message=TextMessage)def handle_message(event): stream_url = 'https://google-translate-proxy.herokuapp.com/api/tts?query='+event.message.text+'&language=zh-tw' r = requests.get(stream_url, stream=True) with open('stream.mp3', 'wb') as f: try: for block in r.iter_content(1024): f.write(block) f.close() subprocess.call('madplay stream.mp3 -o wave:- | aplay -D plughw:1,0',shell=True) except KeyboardInterrupt: pass line_bot_api.reply_message(event.reply_token,TextSendMessage(text=event.message.text))if __name__ == "__main__": app.run(host='0.0.0.0', port= myPort)