11 VisualTFT

您所在的位置:网站首页 gtneo调节音量触屏 11 VisualTFT

11 VisualTFT

2024-07-15 23:33| 来源: 网络整理| 查看: 265

11 音频屏播放 - M系列

本文介绍医用级M系列串口屏如何播放外部SD卡里的视频、音频文件,其中包含以下内容

文件检索:检索SD卡根目录下的文件,如MP3、MP4 列表显示:将检索到的文件,显示出来,通过上下翻页显示 音量控制:调节喇叭的音量大小 循环播放:循环播放遍历出来的歌曲 播放控制:可控制播放的停止、开始,以及指定对应歌曲播放

本例程主要API函数,如下所示

播放音频:play_sound(filename) 停止播放:stop_sound() 音频播放结束回调通知:on_audio_callback (state) 播放视频:play_video(file,left,top,width,height) 停止播放:stop_video() 暂停视频播放:pause_video() 恢复视频播放:resume_video() 视频播放进度回调:on_video_notify(msg,v1, v2) 遍历文件回调函数:on_list_dir(path, filename, type, fsize) 开始遍历:list_dir(sd_dir)

适用范围:M系列

例程下载链接:《M系列-播放SD卡音频频文件》(点击跳转)

路径说明

屏内音视频文件,如果从工程属性加载的音频、视频文件,如下所示

随着工程文件下载到屏内的音视频,Lua脚本引用路径如下所示

play_sound(“3:/sounds/1.mp3”) play_video(“3:/videos/1.mp4”, 0, 0, 800, 480)

通过串口文件传输、4G下载、复制到屏内等等,Lua脚本引用路径如下所示

play_sound(“3:/1.mp3”) play_video(“3:/1.mp4”, 0, 0, 800, 480)

播放SD卡、U盘的音视频,Lua脚本引用路径如下所示

local file_path = ‘’ play_sound(file_path..'/'..'1.mp3') play_video(file_path..'/'..'1.mp4', 0, 0, 800, 480) function on_usb_inserted(dir) file_path = dir end function on_sd_inserted(dir) file_path = dir end

M系列/ 物联型视频播放相关对比,如下所示

M系列 物联型 播放API play_video(file,left,top,width,height) play_video(pathname,repeat) 播放通知 on_video_notify(msg,v1, v2) 控件触控回调函数,对应的视频控件触发 图层关系 默认最顶层,会覆盖视频播放区域的其他控件 同一区域,根据控件层叠关系依次显示 路径盘符 屏内为‘3:/’,U盘/SD卡从回调函数获取 屏内为“A:/’,U盘/SD卡从回调函数获取 遍历方式 大彩API函数 LUA 库函数 11.1 音频播放

本文播放音乐为SD卡根目录下的‘mp3’文件,通过选定对应的音乐播放

画面配置

在画面ID1中,分为以下5大部分,如下所示

歌曲菜单部分:显示音乐名(文本控件ID6~10)、播放状态(图标控件ID1~5)、选择播放(按钮控件ID11~15)。当用户点击按钮控件时候,会播放对应文本控件显示的音乐,并且标亮音乐名称和图标控件 菜单翻页部分:该部分主要由按钮控件ID16(上一页)、按钮控件ID20(下一页)、文本控件ID 17(当前页)以及文本控件ID 20(总页数)。用户点击上一页或下一页会将遍历的音乐名称相应显示出来 音量调节部分:点按钮控件36显示出调节部分的控件,调节部分,主要由进度条控件ID39和滑动条控件ID40来调节音量大小 播放动态效果部分:当音乐播放的时候,动画控件ID31开始播放,播放完毕后,动画控件ID32开始播放 上/下一首部分:该部分由按钮控件实现,按钮控件ID22上一首、按钮控件ID23开始/暂停、按钮控件ID24下一首功能

LUA脚本编辑 遍历歌单:用户插入SD卡后(SD卡根目录有歌曲文件),遍历出所有音频文件并保存在缓冲区里面,当用户翻页或点击播放时,从缓冲区里面显示音乐名或指定音乐播放,LUA脚本如下所示 local music_Tb = {} local music_cnt = 0 local music_curpage = 1 local music_allpage = 1 local paly_music_index = 0 local sd_dir = '' -- 获取扩展名 -- 自定义函数(非回调函数,可以修改) -- @str 需要获取后缀名的字符串 function my_get_extension(str) local file_type = '' local token_s, token_e = string.find(str, "%.", 1) if token_s ~= nil then file_type = string.sub(str, (token_s + 1), string.len(str)) print("file_type -> "..file_type) end return file_type end --变量文件 --@path :文件路径 --@filename:文件名称 --@type :0 文件夹,1 文件 --@fsize :文件大小 function on_list_dir(path, filename, type, fsize) if type == 1 then local cur_file_type = my_get_extension(filename) ...... elseif cur_file_type == 'mp3' then set_enable(sc_music, 23, 1) music_cnt = music_cnt + 1 music_Tb[music_cnt] = path..list_token..filename elseif cur_file_type == 'MP3' then set_enable(sc_music, 23, 1) music_cnt = music_cnt + 1 music_Tb[music_cnt] = path..list_token..filename end end end --显示当前页的歌曲名单 --xth_musicpage : 第几页 function my_show_cur_musicpage(xth_musicpage) if music_cnt > 0 then --计算音乐索引 local music_idnex = (xth_musicpage - 1) * 5 for i = 1, 5 do if (music_idnex + i) 0 then local cur_play_page = math.modf(paly_music_index / 5) if paly_music_index % 5 > 0 then cur_play_page = cur_play_page + 1 end if cur_play_page == xth_musicpage then --当前页是播放页,标亮图标、字体 my_set_music_playstate(paly_music_index) else --当前页非播放页,取消标亮图标、字体 my_set_music_playstate(0) end end end end -- 系统回调函数,插入SD卡自动回调 function on_sd_inserted(dir) print("Lua_DebugMsg -> insert sd disk : "..dir) local cur_screen = get_current_screen() sd_dir = dir list_dir(sd_dir) if cur_screen == sc_music then if music_cnt > 0 then music_allpage = math.modf(music_cnt / 5) if music_cnt % 5 > 0 then music_allpage = music_allpage + 1 end set_text(sc_music, 17, music_curpage) set_text(sc_music, 19, music_allpage) my_show_cur_musicpage(music_curpage) end ...... end end 翻页显示歌单:UI上每一页显示5首歌,通过页码自加键,计算出在“music_Tb”里面的索引,LUA脚本如下所示 function on_control_notify(screen,control,value) if screen == sc_music then --上一页 if control == 16 and value == 0 then local cur_page = 0 if music_cnt > 0 then --计算上一页 cur_page = get_value(sc_music, 17) cur_page = cur_page - 1 if cur_page 0 then --计算上一页 cur_page = get_value(sc_music, 17) all_page = get_value(sc_music, 19) cur_page = cur_page + 1 if cur_page > all_page then cur_page = all_page end --刷新页面,更新歌单 set_value(sc_music, 17, cur_page) my_show_cur_musicpage(cur_page) end ...... end end ...... end 选择对应歌曲播放:标亮选中的歌曲、图标,使“唱片”动态播放,LUA脚本如下所示 --显示播放动画效果 function my_set_music_playgif() stop_animation(sc_music, 31) stop_animation(sc_music, 32) play_animation(sc_music, 31) end --设置当前播放音频的图标、字体标亮 --xth_music:当前音乐索引,此索引为遍历的索引 function my_set_music_playstate(xth_music) local cur_musicname_len = 0 if xth_music > 0 then --计算在歌单列表第几首(1-5) local xth_line = xth_music % 5 if xth_line == 0 then xth_line = 5 end for i = 1, 5 do cur_musicname_len = string.len(get_text(sc_music, i + 5)) if i == xth_line then --标亮播放中的歌曲文字、标亮图标 set_value(sc_music, i, 1) set_fore_color(sc_music, i + 5, play_color) if cur_musicname_len > 16 then set_text_roll(sc_music, i + 5, roll_speed) end else --非播放歌曲取消高亮文字、图标 set_value(sc_music, i, 0) set_fore_color(sc_music, i + 5, noplay_color) set_text_roll(sc_music, i + 5, 0) end end else for i = 1, 5 do set_value(sc_music, i, 0) set_fore_color(sc_music, i + 5, noplay_color) set_text_roll(sc_music, i + 5, 0) end end end function on_control_notify(screen,control,value) ...... if screen == sc_music then ...... elseif (control >= 11 and control 0 then --计算第几首音乐 cur_page = get_value(sc_music, 17) paly_music_index = (cur_page - 1) * 5 + (control - 10) my_set_music_playstate(paly_music_index) if paly_music_index 0 and value 34 and value 67 and value = 22 and control 0 then stop_video() if control ~= 23 then set_value(sc_music, 23, 1) set_value(sc_video, 23, 0) --上一首,播放索引-1 if control == 22 and value == 0 then paly_music_index = paly_music_index - 1 --循环,回到最大索引 if paly_music_index < 1 then paly_music_index = music_cnt end --下一首,播放索引+1 elseif control == 24 and value == 0 then paly_music_index = paly_music_index + 1 --循环,回到最大1 if paly_music_index > music_cnt then paly_music_index = 1 end end --计算当前索引的所在页数 local cur_play_page = math.modf(paly_music_index / 5) if paly_music_index % 5 > 0 then cur_play_page = cur_play_page + 1 end set_value(sc_music, 17, cur_play_page) --刷新显示页数、播放显示状态 my_show_cur_musicpage(cur_play_page) my_set_music_playstate(paly_music_index) --播放歌曲、动态转动唱片 play_sound(music_Tb[paly_music_index]) my_set_music_playgif() else --暂停 if value == 0 then stop_video() stop_sound() stop_animation(sc_music, 31) stop_animation(sc_music, 32) --播放 elseif value == 1 then stop_video() play_sound(music_Tb[paly_music_index]) my_set_music_playgif() end end ...... end ...... end

歌曲循环播放:当声音播放完毕后,自动回调on_audio_callback,则可控制播放下一首,已达到自动播放,循环播放的效果

--音频播放结束通知 function on_audio_callback(state) paly_music_index = paly_music_index + 1 if paly_music_index > music_cnt then paly_music_index = 1 end local cur_play_page = math.modf(paly_music_index / 5) if paly_music_index % 5 > 0 then cur_play_page = cur_play_page + 1 end if cur_play_page == get_value(sc_music, 17) then my_set_music_playstate(paly_music_index) else my_set_music_playstate(0) end my_set_music_playgif() play_sound(music_Tb[paly_music_index]) end 运行预览

编译工程,下载到实体屏中,将mp3文件拷贝到SD卡根目录,插入SD卡,即可显示歌单,选择播放

11.2 视频播放

播放视频通过遍历SD卡目录下的‘mp4’文件,通过选定对应的视频进行播放。

画面配置

在画面ID2中,分为以下4大部分,如下所示

视频菜单部分:显示视频名(文本控件ID6~10)、播放状态(图标控件ID1~5)、选择播放(按钮控件ID11~15)。当用户点击按钮控件时候,会播放对应文本控件显示的视频,并且标亮视频名称和图标控件 菜单翻页部分:该部分主要由按钮控ID16(上一页)、按钮控件ID20(下一页)、文本控件ID 17(当前页)以及文本控件ID 20(总页数)。用户点击上一页或下一页会将遍历的视频名称相应显示出来 音量调节部分:点按钮控件36显示出调节部分的控件,调节部分,主要由进度条控件ID39和滑动条控件ID40来调节音量大小。 播放部分:该部分由按钮控件实现,按钮控件ID22上一首、按钮控件ID20开始/暂停、按钮控件ID24下一首功能,按钮控件ID21是控制上一首、下一首播放进度的显示/隐藏效果

LUA脚本 遍历显示视频表单:用户插入SD卡后(SD卡根目录有mp4文件),遍历出所有mp4文件并保存在缓冲区里面,当用户翻页或点击播放时,从缓冲区里面显示视频名字,LUA脚本如下所示 -- 获取扩展名 -- 自定义函数(非回调函数,可以修改) -- @str 需要获取后缀名的字符串 function my_get_extension(str) local file_type = '' local token_s, token_e = string.find(str, "%.", 1) if token_s ~= nil then file_type = string.sub(str, (token_s + 1), string.len(str)) print("file_type -> "..file_type) end return file_type end --显示当前页的视屏文件名称 --xth_videopage:第几页 function my_show_cur_videopage(xth_videopage) if video_cnt > 0 then --计算该页面的起始视频索引 local video_idnex = (xth_videopage - 1) * 5 for i = 1, 5 do if (video_idnex + i) 0 then local cur_play_page = math.modf(paly_video_index / 5) if paly_video_index % 5 > 0 then cur_play_page = cur_play_page + 1 end if cur_play_page == xth_videopage then my_set_video_playstate(paly_video_index) else my_set_video_playstate(0) end end end end --@path :文件路径 --@filename:文件名称 --@type :0 文件夹,1 文件 --@fsize :文件大小 function on_list_dir(path, filename, type, fsize) if type == 1 then local cur_file_type = my_get_extension(filename) if cur_file_type == 'mp4' then set_enable(sc_video, 23, 1) video_cnt = video_cnt + 1 video_Tb[video_cnt] = path..list_token..filename elseif cur_file_type == 'MP4' then set_enable(sc_video, 23, 1) video_cnt = video_cnt + 1 video_Tb[video_cnt] = path..list_token..filename ...... end end end -- 系统回调函数,插入SD卡自动回调 function on_sd_inserted(dir) local cur_screen = get_current_screen() sd_dir = dir list_dir(sd_dir) ...... elseif cur_screen == sc_video then if video_cnt > 0 then video_allpage = math.modf(video_cnt / 5) if video_cnt % 5 > 0 then video_allpage = video_allpage + 1 end set_text(sc_video, 17, video_curpage) set_text(sc_video, 19, video_allpage) my_show_cur_videopage(video_curpage) end end end 翻页显示视频表单:UI上每一页最多显示5个视频,通过页码自加键,计算出在“video_Tb”里面的索引,LUA脚本如下所示 function on_control_notify(screen,control,value) ...... if screen == sc_video then if control == 16 then local cur_page = 0 if video_cnt > 0 then cur_page = get_value(sc_video, 17) cur_page = cur_page - 1 if cur_page 0 then cur_page = get_value(sc_video, 17) all_page = get_value(sc_video, 19) cur_page = cur_page + 1 if cur_page > all_page then cur_page = all_page end set_value(sc_video, 17, cur_page) my_show_cur_videopage(cur_page) end ...... end end end 选择对应视频播放:标亮选中的视频名字、图标,LUA脚本如下所示 --标亮当前播放视频的图标、文字 function my_set_video_playstate(xth_video) local cur_videoname_len = 0 if xth_video > 0 then local xth_line = xth_video % 5 if xth_line == 0 then xth_line = 5 end for i = 1, 5 do cur_videoname_len = string.len(get_text(sc_video, i + 5)) if i == xth_line then set_value(sc_video, i, 1) set_fore_color(sc_video, i + 5, play_color) if cur_videoname_len > 12 then set_text_roll(sc_video, i + 5, roll_speed) end else set_value(sc_video, i, 0) set_fore_color(sc_video, i + 5, noplay_color) set_text_roll(sc_video, i + 5, 0) end end else for i = 1, 5 do set_value(sc_video, i, 0) set_fore_color(sc_video, i + 5, noplay_color) set_text_roll(sc_video, i + 5, 0) end end end function on_control_notify(screen,control,value) ....... if screen == sc_video then ...... elseif (control >= 11 and control 0 then cur_page = get_value(sc_video, 17) paly_video_index = (cur_page - 1) * 5 + (control - 10) my_set_video_playstate(paly_video_index) if paly_video_index = 22 and control 0 then stop_sound() if control ~= 23 then set_value(sc_video, 23, 1) --停止音频播放、初始化状态 set_value(sc_music, 23, 0) stop_animation(sc_music, 31) stop_animation(sc_music, 32) --上一首 if control == 22 and value == 0 then paly_video_index = paly_video_index - 1 if paly_video_index < 1 then paly_video_index = video_cnt end --下一首 elseif control == 24 and value == 0 then paly_video_index = paly_video_index + 1 if paly_video_index > video_cnt then paly_video_index = 1 end end --计算页码、标亮图标、视频文字等 local cur_play_page = math.modf(paly_video_index / 5) if paly_video_index % 5 > 0 then cur_play_page = cur_play_page + 1 end set_value(sc_video, 17, cur_play_page) my_show_cur_videopage(cur_play_page) my_set_video_playstate(paly_video_index) play_video(video_Tb[paly_video_index], video_x, video_y, video_w, video_h) --显示视频播放进度 my_setvisable_video_progress(1) else if value == 0 then stop_sound() pause_video() elseif value == 1 then stop_sound() set_value(sc_music, 23, 0) stop_animation(sc_music, 31) stop_animation(sc_music, 32) resume_video() --显示视频播放进度 my_setvisable_video_progress(1) end end end ...... end end 音量调节:按下音量按钮,显示音乐部分的控件,通过拖动滑动条控件,联动文本控件音量值、图标控件音量等级的控制,代码如下所示 --显示音量部分的控件 function my_set_volume_visiable(state) for i = 37, 41 do set_visiable(sc_music, i, state) set_visiable(sc_video, i, state) end if state == 1 then start_timer(volume_timer, 2*1000, 1, 1) end end --设置音量大小、音量图标等级、音量值 function my_set_volume_value(value) --设置音量图标 if value == 0 then set_value(sc_music, 35, 0) set_value(sc_music, 38, 0) set_value(sc_video, 35, 0) set_value(sc_video, 38, 0) elseif value > 0 and value 34 and value 67 and value video_cnt then paly_video_index = 1 end local cur_play_page = math.modf(paly_video_index / 5) if paly_video_index % 5 > 0 then cur_play_page = cur_play_page + 1 end if cur_play_page == get_value(sc_video, 17) then my_set_video_playstate(paly_video_index) else my_set_video_playstate(0) end play_video(video_Tb[paly_video_index], video_x, video_y, video_w, video_h) my_setvisable_video_progress(1) end end 运行预览

编译工程,下载到实体屏中,将mp4文件拷贝到SD卡根目录,插入SD卡,即可显示歌单,选择播放

Copyright ©Dacai all right reserved,powered by Gitbook该文件修订时间: 2023-03-30 11:43:18


【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3