Matecho - 充电
type: channel
link: https://matecho-dev.zapic.moe/tag/%E5%85%85%E7%94%B5/
items: 为 Windows 10 笔记本电脑添加充电提示音

为 Windows 10 笔记本电脑添加充电提示音
type: item
date: 2021-09-09T04:37:00+00:00
description: Android和iOS阵营都有给设备充电的提示音, 但是Windows没有, 感觉少了点仪式感. 所以就想着给Windows也加一个充电提示音.@2021/09/12 更新重新组织了一下代码, 修复了将充满识别成断开电源的问题.\#0 获取充电状态第一反应是通过Windows日志获取电源状态更改事件, 然后用计划任务去播放声音.结果实际测试下来这种方法延迟很高(约5秒), 体验不大行, 所以直接使用VBScript去实现查询.我也不知道为什么要这样实现, 四处搜刮得到的代码, 能用就行.pngSet WMI = GetObject("winmgmts:\\.\root\WMI") Dim LastState Function GetCharge Set Query = WMI.ExecQuery("Select PowerOnline From BatteryStatus") For Each Status In Query If Status.PowerOnline <> LastState Then GetCharge = Status.PowerOnline Exit Function End If Next GetCharge = LastState End Function LastState = GetCharge While True Current = GetCharge If Current <> LastState Then If Current Then PlaySound("charge") Else PlaySound("discharge") End If LastState = Current End If WScript.Sleep 500 Wend\#1 播放声音然后cv一个播放声音的代码:Set Fs = CreateObject("Scripting.FileSystemObject") Set Player = CreateObject("WMPlayer.OCX") Function PlaySound(name) Path = "./"+ name +".mp3" If (Not Fs.FileExists(Path)) Then Exit Function End If Player.URL = Path Player.Controls.play End Function\#2 找点音效素材从几个不知名的国外网站上面凑齐了一套MIUI的充电音效:连接充电器:草了, 2021年了, 你的浏览器怎么还不支持HTML5断开充电器:草了, 2021年了, 你的浏览器怎么还不支持HTML5\#3 拼接一下代码Set Fs = CreateObject("Scripting.FileSystemObject") Set Player = CreateObject("WMPlayer.OCX") Set WMI = GetObject("winmgmts:\\.\root\WMI") Dim LastState Function PlaySound(name) Path = "./"+ name +".mp3" If (Not Fs.FileExists(Path)) Then Exit Function End If Player.URL = Path Player.Controls.play End Function Function GetCharge Set Query = WMI.ExecQuery("Select PowerOnline From BatteryStatus") For Each Status In Query If Status.PowerOnline <> LastState Then GetCharge = Status.PowerOnline Exit Function End If Next GetCharge = LastState End Function LastState = GetCharge While True Current = GetCharge If Current <> LastState Then If Current Then PlaySound("charge") Else PlaySound("discharge") End If LastState = Current End If WScript.Sleep 500 Wend\#4 开机启动计划任务就行, 就像这样:<?xml version="1.0" encoding="UTF-16"?> 2021-09-09T11:35:11.9499057 Zapic Play no…