PowerShell调节电脑音量 5


你可能会质疑,用鼠标或者键盘就能非常方便地调整音量,为什么需要PowerShell?所以有的时候场景真的很重要,就像美团杀入打车软件行列一样。

我们家就一个场景:熊孩子2岁多,晚上喜欢用爱奇艺看动画片,常常错过睡觉时间,所以我要在电脑中设置一个定时Job:

晚上固定时间运行,检测是否有爱奇艺客户端运行;如果有就关闭,然后把音乐调到最大,重复播放一句话3次,比如:“请注意,荔XXX小朋友,你睡觉的时间到了,明天早晨还要早点起床去上学”,然后播放倒计时“5,4,3,2,1,关机”。播放结束后关机。

经过一周的运行发现,效果非常明显,从此小朋友就认为关机是客观不可控的行为,和他老爸没关系了。:)

可以贴代码了:

function Set-AudioVolum 
{
    param($Volum,[switch]$Mute)

    Add-Type -TypeDefinition @'
    using System.Runtime.InteropServices;
    [Guid("5CDF2C82-841E-4546-9722-0CF74078229A"), InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
    interface IAudioEndpointVolume
    {
        // f(), g(), ... are unused COM method slots. Define these if you care
        int f(); int g(); int h(); int i();
        int SetMasterVolumeLevelScalar(float fLevel, System.Guid pguidEventContext);
        int j();
        int GetMasterVolumeLevelScalar(out float pfLevel);
        int k(); int l(); int m(); int n();
        int SetMute([MarshalAs(UnmanagedType.Bool)] bool bMute, System.Guid pguidEventContext);
        int GetMute(out bool pbMute);
    }
    [Guid("D666063F-1587-4E43-81F1-B948E807363F"), InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
    interface IMMDevice
    {
        int Activate(ref System.Guid id, int clsCtx, int activationParams, out IAudioEndpointVolume aev);
    }
    [Guid("A95664D2-9614-4F35-A746-DE8DB63617E6"), InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
    interface IMMDeviceEnumerator
    {
        int f(); // Unused
        int GetDefaultAudioEndpoint(int dataFlow, int role, out IMMDevice endpoint);
    }
    [ComImport, Guid("BCDE0395-E52F-467C-8E3D-C4579291692E")] class MMDeviceEnumeratorComObject { }
    public class Audio
    {
        static IAudioEndpointVolume Vol()
        {
            var enumerator = new MMDeviceEnumeratorComObject() as IMMDeviceEnumerator;
            IMMDevice dev = null;
            Marshal.ThrowExceptionForHR(enumerator.GetDefaultAudioEndpoint(/*eRender*/ 0, /*eMultimedia*/ 1, out dev));
            IAudioEndpointVolume epv = null;
            var epvid = typeof(IAudioEndpointVolume).GUID;
            Marshal.ThrowExceptionForHR(dev.Activate(ref epvid, /*CLSCTX_ALL*/ 23, 0, out epv));
            return epv;
        }
        public static float Volume
        {
            get { float v = -1; Marshal.ThrowExceptionForHR(Vol().GetMasterVolumeLevelScalar(out v)); return v; }
            set { Marshal.ThrowExceptionForHR(Vol().SetMasterVolumeLevelScalar(value, System.Guid.Empty)); }
        }
        public static bool Mute
        {
            get { bool mute; Marshal.ThrowExceptionForHR(Vol().GetMute(out mute)); return mute; }
            set { Marshal.ThrowExceptionForHR(Vol().SetMute(value, System.Guid.Empty)); }
        }
    }
'@
::Volume=$Volum
    if($Mute){
::Mute=$true
    }

}



$qiyi=Get-Process QyClient -ErrorAction SilentlyContinue
if($qiyi -eq $null){ return}
$qiyi.Kill()

# 添加 System.speech.dll 引用
Add-Type -AssemblyName System.speech
# 创建 SpeechSynthesizer 对象
$syn=New-Object System.Speech.Synthesis.SpeechSynthesizer
$syn.Rate=-1
$syn.Volume=100
Set-AudioVolum -Volum 1
1..3 |foreach {
$syn.Speak("请注意,荔XX小朋友,你睡觉的时间到了,明天早晨还要早点起床去上学。")

}

$syn.Speak("现在倒计时关电脑:五,四,三,二,一,关机")
Set-AudioVolum -Volum 0.2
Stop-Computer -Force
本文链接: https://www.pstips.net/set-audiovolum.html
请尊重原作者和编辑的辛勤劳动,欢迎转载,并注明出处!

关于 Mooser Lee

我是一个Powershell的爱好者,创建了PowerShell中文博客,热衷于Powershell技术的搜集和分享。本站部分内容来源于互联网,不足之处敬请谅解,并欢迎您批评指正。

回复 Mooser Lee 取消回复

您的电子邮箱地址不会被公开。 必填项已用*标注

5 条评论 “PowerShell调节电脑音量