为 Windows 10 笔记本电脑添加充电提示音
Zapic
2021-09-09 2

Android和iOS阵营都有给设备充电的提示音, 但是Windows没有, 感觉少了点仪式感.
所以就想着给Windows也加一个充电提示音.

@2021/09/12 更新
重新组织了一下代码, 修复了将充满识别成断开电源的问题.

\#0 获取充电状态

第一反应是通过Windows日志获取电源状态更改事件, 然后用计划任务去播放声音.
结果实际测试下来这种方法延迟很高(约5秒), 体验不大行, 所以直接使用VBScript去实现查询.

我也不知道为什么要这样实现, 四处搜刮得到的代码, 能用就行.png

Set 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的充电音效:

连接充电器:

断开充电器:

\#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"?>
<Task version="1.2" xmlns="http://schemas.microsoft.com/windows/2004/02/mit/task">
  <RegistrationInfo>
    <Date>2021-09-09T11:35:11.9499057</Date>
    <Author>Zapic</Author>
    <Description>Play notify sound when charger connected or discconected.</Description>
    <URI>\ChargeNotify</URI>
  </RegistrationInfo>
  <Triggers>
    <BootTrigger>
      <Enabled>true</Enabled>
    </BootTrigger>
  </Triggers>
  <Principals>
    <Principal id="Author">
      <UserId>S-1-5-18</UserId>
      <RunLevel>LeastPrivilege</RunLevel>
    </Principal>
  </Principals>
  <Settings>
    <MultipleInstancesPolicy>IgnoreNew</MultipleInstancesPolicy>
    <DisallowStartIfOnBatteries>false</DisallowStartIfOnBatteries>
    <StopIfGoingOnBatteries>false</StopIfGoingOnBatteries>
    <AllowHardTerminate>true</AllowHardTerminate>
    <StartWhenAvailable>true</StartWhenAvailable>
    <RunOnlyIfNetworkAvailable>false</RunOnlyIfNetworkAvailable>
    <IdleSettings>
      <StopOnIdleEnd>true</StopOnIdleEnd>
      <RestartOnIdle>false</RestartOnIdle>
    </IdleSettings>
    <AllowStartOnDemand>true</AllowStartOnDemand>
    <Enabled>true</Enabled>
    <Hidden>false</Hidden>
    <RunOnlyIfIdle>false</RunOnlyIfIdle>
    <WakeToRun>false</WakeToRun>
    <ExecutionTimeLimit>PT0S</ExecutionTimeLimit>
    <Priority>7</Priority>
  </Settings>
  <Actions Context="Author">
    <Exec>
      <Command>C:\Windows\ChargeNotify\daemon.vbs</Command>
      <WorkingDirectory>C:\Windows\ChargeNotify\</WorkingDirectory>
    </Exec>
  </Actions>
</Task>

\#5 试一下


所有结果都在这个压缩包里了:
ChargeNotify.zip (36.6KiB)
把整个文件夹放到C:\Windows\下, 然后把文件夹里的ChargeNotify.xml作为计划任务导入即可.

评论 2
langyo #175

好活儿

2021-09-09 14:00
回复
45gfg9
45gfg9
#176

好活儿

2021-09-09 22:22
回复
评论已关闭
发表评论
评论 取消回复
Copyright © 2024 Zapic's Blog