VisualHMI audio playback: play_sound series and automatic continuous playback of MP3

freeFree Technical Resource

This content is free to read, suitable for basic learning and search traffic.

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

FunctionsDescription
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

AddressPurpose
LW1000Flip left/right
LW1001Previous/Next
LW1002Play/Pause (Bit status indicator: 0-Pause/1-Continue)
LW1003Current Page
LW1004Total Pages
LW1005Whether there are songs in the SD root directory (Bit status)
LW1100~1200Song title text list
LW1240Current playback time
LW1250Total time
LW1006Playback progress bar
LW0140Volume

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
end

4. 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()     -- 检测播放状态,播完自动下一首
end
Three 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.

Put this resource to use in a real project?

Go to the Tool Center for message parsing, CRC verification and device debugging, or submit your requirements for selection and integration advice.

Engineer Membership

Turn this article into actionable debugging resources

After activation, you can use advanced message parsing, resource pack downloads, code examples, engineering cases and priority technical support, suitable for real project delivery.

Unlimited Advanced Tools
Resource & Code Packs
Complete Engineering Case Library
Priority Technical Support

Leave a Reply

Your email address will not be published. Required fields are marked *.