最新案例教程点击下方链接跳转,CSDN已停止更新
点
击
跳
转
简介
案例通过url下载网络资源至开发板播放。
- 使用http协议下载网络音乐资源,案例只提供测试url,需要自己添加其他url。
- python层接口,可以自行更改。储存位置可以选择本地或者SD卡。
- 参数格式url_play(url,src,name)
- url:文件链接
- src:存储位置 - 1本地 / 其他SD卡
- name:文件名称
准备器材
本案例需要的硬件
器材 | 数量 |
---|---|
HaaS506开发板 | 1 |
喇叭 | 1 |
SIM卡 | 1 |
SD卡 | 1 |
硬件接口
代码
复制代码到VS code
- main.py
import http
from audio import Audio
import network
import utime as time
import uos
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('网络注册失败')
while True:
if g_connect_status:
print('网络连接成功')
break
time.sleep_ms(20)
def play(name):
global address
print('------------------------Audio play start--------------------')
aud=Audio()
aud.set_pa()
aud.setVolume(5)
time.sleep(2)
aud.play(address+name)
# aud.stop()
def url_play(url,src,name):
global address
if src == 1: #存入开发板模块
address = '/data/pyamp'
else: #默认存入SD卡
ret = uos.mountsd() #启用SD卡
if ret:
address = '/sdcard0'
else:
address = '/data/pyamp'
print('!!!!!! no SD card !!!!!!')
print('===The file is saved locally===')
#删除文件
print('before removing files:',uos.listdir(address))
try:
uos.remove(address+name)
except:
print('no'+name)
print('after removing files:',uos.listdir(address))
#下载文件
htp=http.client()
print('filename in directory of user before downloading:',uos.listdir(address))
htp.download(url,address+name)
print('filename in directory of user after downloading:',uos.listdir(address))
#播放文件
play(name)
if __name__ == '__main__':
connect_network() #连接网络
#播放音频
mp3url = 'http://music.szyywdz.huntercat.cn/haas506.mp3' #案例链接
url_play(mp3url,0,'/down.mp3') #参数格式url_play(url,src,name)(文件链接,存储位置:1本地 / 其他SD卡,文件名称)
while True:
time.sleep(1)
测试log
程序运行流程
- 网络连接成功
- 读取选择文件夹中的文件
- 删除重复的同名文件
- 下载目标文件
- 读取文件夹文件
- 播放声音文件
POWERONREASON:0x0000,parse:NORMAL_REBOOT.
mpthread_init stack:0x80b04584 0x80b04584
[ 11.727]<E>ACTIVATION_REPORT2 activation_report2:286 report http data:POST /device/add HTTP/1.1
Host:nps.huntercat.cn
User-Agent: AliOS-Things
Content-Length:157
Accept: */*
Content-Type:application/json
Connection: Keep-Alive
{"activationStr":"V=3.0.0&P=Enginelf&A=HaaS506_App&B=HaaS506&C=M601&N=Cellular&X=HaaS506-M320&S=Cellular&O=FreeRTOS&T=DTU&M=866907051837598&Y=Alibaba-Cloud"}
网络连接成功
before removing files: ['System Volume Information', 'down.mp3', 'down1.mp3', 'down2.mp3']
after removing files: ['System Volume Information', 'down1.mp3', 'down2.mp3']
filename in directory of user before downloading: ['System Volume Information', 'down1.mp3', 'down2.mp3']
[ 12.728]<E>http_wrapper [httpclient_retrieve_content 519] no more(content-length)
filename in directory of user after downloading: ['System Volume Information', 'down.mp3', 'down1.mp3', 'down2.mp3']
------------------------Audio play start--------------------