综合案例2.0 - 在线MP3音乐播放器
- 一、案例说明
- 二、准备器件
- 三、案例连线
- 四、代码
- 代码说明
- 复制
- 五、测试
一、案例说明
本案例制作一个联网下载声音文件的MP3播放器。
- 案例功能说明:
- 案例使用一块IC035串口屏和两个按键
- 通过按键可以切换音乐,下载播放或删除声音文件
- 案例可以实时显示当前SD卡内的文件数量,可以直观看到文件是否下载完成
- JC035串口屏使用请参考产品资料说明或者参考haas506 2.0开发教程-example-JC035串口屏饮料机
- 案例使用M601开发板,中断引脚选择GPIO44,GPIO20。使用M320请注意IO口选择,M320部分IO口暂不支持中断。
- 代码选择GB2312编码
二、准备器件
本案例需要的硬件
器材 | 数量 |
---|---|
HaaS506开发板 | 1 |
JC025串口屏(480x320) | 1 |
触摸按键 | 1 |
SIM卡 | 1 |
喇叭 | 1 |
SD卡 | 1 |
三、案例连线
四、代码
代码说明
1.代码总共4个文件
2.如果要显示中文代码烧录时选择GB2312编码
复制
mian.py
import net_connect
import mp3
import utime
from driver import UART
from driver import GPIO
#按键使能
key1=GPIO(10,10)
key2=GPIO(10,10)
key1.open("key1")
key2.open("key2")
#菜单目录
data = {'test1.mp3':'http://music.szyywdz.huntercat.cn/haas506.mp3','test2.mp3':'http://music.szyywdz.huntercat.cn/audio/m601test.mp3','test3.mp3':'http://music.szyywdz.huntercat.cn/audio/syserror.mp3'}
num = ['test1.mp3','test2.mp3','test3.mp3']
name = num[0]
#按键
buttons1 = 4
buttons2 = 0
buttons3 = 0
degree = 0
play_stop = True
#切换键
def key1_callback(args):
global degree,play_stop,buttons1,buttons2,buttons3
print('-------- 切换 ---------')
play_stop = True
#设定按键次数
degree += 1
if degree > 2:
degree = 0
if degree == 0:
buttons1 = 4
buttons2 = 0
buttons3 = 0
if degree == 1:
buttons1 = 0
buttons2 = 4
buttons3 = 0
if degree == 2:
buttons1 = 0
buttons2 = 0
buttons3 = 4
#获取文件个数
catalogue = mp3.search()
UART_send("DCV32(120,5,'在线音乐播放器',1);BTN(40,50,400,60,%s,1,5,2,%d);BTN(40,110,400,60,%s,1,5,2,%d);BTN(40,170,400,60,%s,1,5,2,%d);BTN(60,230,360,40,'本地文件个数: %d',1,0,1,0);BTN(40,270,64,32,'下载播放',0,0,2,0);BTN(400,270,64,32,'切换',0,0,5,0);\r\n"%(num[0],buttons1,num[1],buttons2,num[2],buttons3,catalogue))
utime.sleep_ms(100)
key1.disableIrq()
key1.clearIrq()
#播放&删除键
def key2_callback(args):
global degree,play_stop,buttons1,buttons2,buttons3
print('======= 播放 =========')
if play_stop: #播放
print(data[num[degree]],num[degree],'----------------------------------------------')
mp3.download(data[num[degree]],num[degree])
catalogue = mp3.search()
UART_send("DCV32(120,5,'在线音乐播放器',1);BTN(40,50,400,60,%s,1,5,2,%d);BTN(40,110,400,60,%s,1,5,2,%d);BTN(40,170,400,60,%s,1,5,2,%d);BTN(60,230,360,40,'本地文件个数: %d',1,0,1,0);BTN(40,270,64,32,'本地删除',0,0,1,0);BTN(400,270,64,32,'切换',0,0,5,0);\r\n"%(num[0],buttons1,num[1],buttons2,num[2],buttons3,catalogue))
play_stop = False
mp3.play(num[degree])
else: #删除
mp3.delete(num[degree])
play_stop = True
catalogue = mp3.search()
UART_send("DCV32(120,5,'在线音乐播放器',1);BTN(40,50,400,60,%s,1,5,2,%d);BTN(40,110,400,60,%s,1,5,2,%d);BTN(40,170,400,60,%s,1,5,2,%d);BTN(60,230,360,40,'本地文件个数: %d',1,0,1,0);BTN(40,270,64,32,'下载播放',0,0,2,0);BTN(400,270,64,32,'切换',0,0,5,0);\r\n"%(num[0],buttons1,num[1],buttons2,num[2],buttons3,catalogue))
utime.sleep_ms(100)
key2.disableIrq()
key2.clearIrq()
def UART_send(data):
writeBuf0=bytearray(data)
uart0.write(writeBuf0)
if __name__ == '__main__':
uart0=UART()
uart0.open("serial1")
utime.sleep_ms(1000)
UART_send("CLR(0);DIR(3);\r\n")
net_connect.connect_network()
key1.enableIrq(key1_callback)
key2.enableIrq(key2_callback)
catalogue = mp3.search()
UART_send("DCV32(120,5,'在线音乐播放器',1);BTN(40,50,400,60,%s,1,5,2,%d);BTN(40,110,400,60,%s,1,5,2,%d);BTN(40,170,400,60,%s,1,5,2,%d);BTN(60,230,360,40,'本地文件个数: %d',1,0,1,0);BTN(40,270,64,32,'下载播放',0,0,2,0);BTN(400,270,64,32,'切换',0,0,5,0);\r\n"%(num[0],buttons1,num[1],buttons2,num[2],buttons3,catalogue))
while True:
utime.sleep_ms(10)
mp3.py
import uos
import utime
import http
from audio import Audio
uos.mountsd()
def download(url,name):
print('filename in directory:',uos.listdir('/sdcard0'))
htp=http.client()
htp.download(url,'/sdcard0/'+name)
print('filename in directory:',uos.listdir('/sdcard0'))
catalogue = []
def search():
global catalogue
catalogue = uos.listdir('/sdcard0')
return len(catalogue)-1
def delete(name):
print('before removing files:',uos.listdir('/sdcard0'))
uos.remove('/sdcard0/'+name)
print('after removing files:',uos.listdir('/sdcard0'))
def play(name):
print('------------------------Audio play start--------------------')
aud=Audio()
aud.set_pa()
aud.setVolume(5)
utime.sleep(1)
aud.play('/sdcard0/'+name)
# utime.sleep(3)
# aud.stop()
# print('------------------------Audio play done--------------------')
net_connect.py
import network
import utime
g_connect_status = False
def on_4g_cb(args):
global g_connect_status
pdp = args[0]
netwk_sta = args[1]
if netwk_sta == 1:
g_connect_status = True
else:
g_connect_status = False
def connect_network():
global net,on_4g_cb,g_connect_status
net = network.NetWorkClient()
g_register_network = False
if net._stagecode is not None and net._stagecode == 3 and net._subcode == 1:
g_register_network = True
else:
g_register_network = False
if g_register_network:
net.on(1,on_4g_cb)
net.connect(None)
else:
print('!!! connect_network false !!!')
while True:
if g_connect_status:
print('connect_network ok')
break
utime.sleep_ms(20)
board.json
{
"name": "haas506",
"version": "2.0.0",
"io": {
"key1": {
"type": "GPIO",
"port": 44,
"dir": "irq",
"pull": "pullup",
"intMode":"falling"
},
"key2": {
"type": "GPIO",
"port": 20,
"dir": "irq",
"pull": "pullup",
"intMode":"falling"
},
"serial1":{
"type":"UART",
"port":0,
"dataWidth":8,
"baudRate":115200,
"stopBits":1,
"flowControl":"disable",
"parity":"none"
},
"serial2":{
"type":"UART",
"port":1,
"dataWidth":8,
"baudRate":115200,
"stopBits":1,
"flowControl":"disable",
"parity":"none"
},
"serial3":{
"type":"UART",
"port":2,
"dataWidth":8,
"baudRate":115200,
"stopBits":1,
"flowControl":"disable",
"parity":"none"
}
},
"debugLevel": "ERROR",
"repl":"enable",
"replPort":0
}
五、测试
1.上电后屏幕显示如下
- 按开发板key1键可以切换文件
- 按下触摸按键可以下载文件并播放,再次按下删除文件
- 本地文件个数实时刷新
2.log测试