Audio Playback API: Plays music files from SD/U-disks, and works with player controls to create a complete music player (play/pause/previous/next/progress bar/volume).
1. API Summary Table
| Functions | Description |
|---|---|
play_sound(filename) | Play audio, absolute path (e.g. "1:/music/xx.mp3") |
stop_sound() | Stop playing |
pause_sound() | Pause |
resume_sound() | Resume playing |
2. Player Control Address Table
| Address | Purpose |
|---|---|
| LW1000 | Flip left/right |
| LW1001 | Previous/Next |
| LW1002 | Play/Pause (Bit status indicator: 0-Pause/1-Continue) |
| LW1003 | Current Page |
| LW1004 | Total Pages |
| LW1005 | Whether there are songs in the SD root directory (Bit status) |
| LW1100~1200 | Song title text list |
| LW1240 | Current playback time |
| LW1250 | Total time |
| LW1006 | Playback progress bar |
| LW0140 | Volume |
3. Traverse SD card songs
musicNameListTb = {} -- 歌名列表缓存
curPlayIndex = 1
function on_sd_inserted(dir)
list_dir(dir) -- 列 SD 卡根目录
end
function on_list_dir(path, filename, type, fsize)
if type == 1 then
local ext = string.sub(filename, -4)
if ext == ".mp3" or ext == ".wav" then
table.insert(musicNameListTb, filename)
set_uint16(VT_LW, 0x1005, 1) -- 有歌标志
end
end
end4. Playback control logic
function on_update(slave, vtype, addr)
if vtype == VT_LW then
if addr == 0x1002 then
local v = get_uint16(VT_LW, 0x1002)
if v == 1 then
play_sound("1:/music/"..musicNameListTb[curPlayIndex])
else
pause_sound()
end
elseif addr == 0x1001 then
-- 上一首/下一首:切换 curPlayIndex 后重新播放
local v = get_uint16(VT_LW, 0x1001)
if v == 1 and curPlayIndex > 1 then
curPlayIndex = curPlayIndex - 1
elseif v == 2 and curPlayIndex < #musicNameListTb then
curPlayIndex = curPlayIndex + 1
end
play_sound("1:/music/"..musicNameListTb[curPlayIndex])
end
end
end
function on_run(screen)
updatMisPlayState() -- 检测播放状态,播完自动下一首
endThree pitfalls:① The path must include a device prefix(M series 1:=SD/2:=U-disk/3:=Internal Flash), where "1:/music/a.mp3" is correct and "1:music/a.mp3" is incorrect;② After playback, it will not automatically switch to the next song, and you need to poll the playback status in on_run (similar mechanism to on_video_notify);③ The audio format is determined according to the firmware support list, and unsupported formats will fail silently - first try with a known playable file.
Compiled from Guangzhou Dacai Technology VisualHMI development documentation (hmi-doc.gz-dc.com) LUA tutorial "Audio Playback", copyright owned by Dacai Technology.
Leave a Reply