7688外接USB音效卡,並加上喇叭,如果用自製的喇叭音箱,把板子都藏在音箱裡就更棒了
也可以把喇叭換成耳機的型式
準備MicroSD卡,建立music資料夾,並放入幾首歌曲
1.在/root/app下,建立一個執行檔,檔名叫做stopMp3,這個指令檔是要給python呼叫的外部指令,用來停止目前播放的mp3用的,檔案內容如下:
ps | grep 'playMp3 ' | grep -v grep | awk '{print$1}' | xargs kill -9
ps | grep 'aplay -D' | grep -v grep | awk '{print$1}' | xargs kill -9
ps | grep 'madplay ' | grep -v grep | awk '{print$1}' | xargs kill -9
2.建立完成後要記得將這個檔案變成可執行,指令:chmod 744 stopMp3
1.在/root/app下,建立一個執行檔,檔名叫做playMp3,這個指令檔是要給python呼叫的外部指令,用來停止目前播放的mp3歌曲,並換過新選取的歌曲並播放,檔案內容如下:
ps | grep 'aplay -D' | grep -v grep | awk '{print$1}' | xargs kill -9
ps | grep 'madplay ' | grep -v grep | awk '{print$1}' | xargs kill -9
madplay ${1} -o wave:- | aplay -D plughw:1,0 &
2.建立完成後要記得將這個檔案變成可執行,指令:chmod 744 playMp3
# coding=utf-8
import requests,os
import subprocess,time
from flask import Flask, request, abort
from linebot import (
LineBotApi, WebhookHandler
)
from linebot.exceptions import (
InvalidSignatureError
)
from linebot.models import (
MessageEvent, TextMessage, TextSendMessage,
)
#這邊的port可以修改成你要想run的port
myPort=8090
myPath='/tmp/run/mountd/mmcblk0p1/music/'
myFilesStr=''
mp3_counts=0
for file in os.listdir(myPath):
if file.endswith(".mp3"):
myFilesStr+=(file+'\n')
mp3_counts+=1
app = Flask(__name__)
#請修改以下的程式,加入自己Line的channelAccessToken以及channelSecret
line_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'
@handler.add(MessageEvent, message=TextMessage)
def handle_message(event):
myText=event.message.text;
global myFilesStr
global mp3_counts
if myText=='stop':
subprocess.call(os.getcwd()+'/stopMp3 &' ,shell=True)
line_bot_api.reply_message(event.reply_token,TextSendMessage(text=u'已停止播放歌曲!'))
elif myText.startswith('http://') or myText.startswith('https://'):
r = requests.get(myText, stream=True)
userId=event.source.user_id
line_bot_api.reply_message(event.reply_token,TextSendMessage(text=u'正在下載MP3,請耐心等候,並在下載結束前勿執行其他指令!'))
with open(myPath+str(mp3_counts+1)+'.mp3', 'wb') as f:
try:
for block in r.iter_content(1024):
f.write(block)
f.close()
mp3_counts+=1
myFilesStr+=(str(mp3_counts)+'.mp3\n')
line_bot_api.push_message(userId, TextSendMessage(text=(u'已下載完畢,儲存為'+str(mp3_counts)+'.mp3')))
except KeyboardInterrupt:
pass
elif myText.isdigit()!=True:
line_bot_api.reply_message(event.reply_token,TextSendMessage(text=(myFilesStr+u'輸入歌名數字,即可遠端播放MP3!\n輸入stop可停止播放歌曲!')))
else:
if os.path.exists(myPath+myText+'.mp3'):
line_bot_api.reply_message(event.reply_token,TextSendMessage(text=u'正在播放'+myText+'.mp3'))
subprocess.call(os.getcwd()+'/playMp3 '+myPath+myText+'.mp3 &',shell=True)
else:
line_bot_api.reply_message(event.reply_token,TextSendMessage(text=u'沒有'+myText+u'.mp3這首音樂!'))
if __name__ == "__main__":
app.run(host='0.0.0.0', port= myPort)