如何创建可在特定时间点执行操作的VLC Lua扩展?
VLC Lua扩展实现定时静音方案
针对你需要在视频播放到10秒时静音、20秒时取消静音的需求,结合你遇到的各种等待问题,推荐用事件驱动监听的方式实现,完全避开阻塞、忙等待等问题,且无需依赖vlc.misc接口:
实现代码
-- 配置目标时间(单位:秒) local MUTE_AT = 10 local UNMUTE_AT = 20 -- 标记操作是否已执行,避免重复触发 local has_muted = false local has_unmuted = false local time_event_id = nil function descriptor() return { title = "定时静音"; version = "1.0"; author = ""; capabilities = {}; } end function activate() -- 监听播放时间变化事件,每次时间更新时触发检查 local current_input = vlc.input if current_input then time_event_id = vlc.event.attach(current_input.event().time_changed, check_playback_time) end end function check_playback_time() -- 将当前播放时间转换为秒(vlc.input.time()返回毫秒) local current_sec = vlc.input.time() / 1000 -- 触发静音操作 if current_sec >= MUTE_AT and not has_muted then vlc.audio.set_mute(true) has_muted = true end -- 触发取消静音操作,完成后移除监听 if current_sec >= UNMUTE_AT and not has_unmuted then vlc.audio.set_mute(false) has_unmuted = true if time_event_id then vlc.event.detach(time_event_id) time_event_id = nil end end end function deactivate() -- 扩展停用前清理事件监听 if time_event_id then vlc.event.detach(time_event_id) end end
核心优势
- 完全非阻塞:通过VLC的
time_changed事件触发检查,无需自己实现等待逻辑 - 避免无响应:没有忙等待循环,不会被VLC判定为无响应扩展
- 无需依赖受限接口:全程未使用
vlc.misc相关方法,符合扩展环境限制 - 操作仅执行一次:通过标记变量确保静音和取消静音操作各触发一次,完成后自动移除监听节省资源
内容的提问来源于stack exchange,提问作者Gordon Fogus
相关产品推荐
相关产品推荐

