windows 从powershell更改音频级别?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21355891/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Change audio level from powershell?
提问by bitshift
How can I use powershell to set the speaker volume? Ive dug around on here and elsewhere online can cant really find an answer.
如何使用powershell设置扬声器音量?我已经在这里和其他地方在线挖掘无法真正找到答案。
I think I will have to write something in C# that wraps a Win32 API and THEN call it from my powershell script. The Win32 API's would be one of these
我想我必须用 C# 编写一些东西来包装 Win32 API,然后从我的 powershell 脚本中调用它。Win32 API 就是其中之一
[DllImport("winmm.dll")]
public static extern int waveOutGetVolume(IntPtr hwo, out uint dwVolume);
[DllImport("winmm.dll")]
public static extern int waveOutSetVolume(IntPtr hwo, uint dwVolume);
回答by Vimes
SendKeys stopped working for me in Windows 10 (it literally types digits where my caret is). I found this blog postwith a very convenient way of doing it.
SendKeys 在 Windows 10 中停止对我来说有效(它实际上在我的插入符号所在的位置键入数字)。我发现这篇博客文章使用了一种非常方便的方法。
First, run this to get access to the audio API:
首先,运行它以访问音频 API:
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)); }
}
}
'@
Then control the volume like this:
然后像这样控制音量:
[audio]::Volume = 0.2 # 0.2 = 20%, etc.
And mute/unmute like this:
并像这样静音/取消静音:
[audio]::Mute = $true # Set to $false to un-mute
回答by logicaldiagram
I created a cmdlet for manipulating audio devices.
我创建了一个用于操作音频设备的 cmdlet。
http://www.automatedops.com/projects/windowsaudiodevice-powershell-cmdlet/
http://www.automatedops.com/projects/windowsaudiodevice-powershell-cmdlet/
It even includes a live peak value display.
它甚至包括实时峰值显示。
回答by Knuckle-Dragger
We can Mute, Volume Down, Volume Up speaker levels with these commands. A simple 1..50
loop (each counter = 2% volume) can be added to make a function that accepts input and adjusts volume without any need for C#.
我们可以使用这些命令静音、调低音量、调高扬声器音量。1..50
可以添加一个简单的循环(每个计数器 = 2% 音量)来制作一个接受输入并调整音量的函数,而无需任何 C#。
Volume Mute
静音
$obj = new-object -com wscript.shell
$obj.SendKeys([char]173)
Volume Down Button
降低音量按钮
$obj = new-object -com wscript.shell
$obj.SendKeys([char]174)
Volume Up Button
音量调高按钮
$obj = new-object -com wscript.shell
$obj.SendKeys([char]175)
Find some relevant info here.
在这里找到一些相关信息。
How can I mute/unmute my sound from PowerShell
EDIT: Here is a reusable function, tested and working on W7x64 w/ Powershell v2.
编辑:这是一个可重用的功能,经过测试并在 W7x64 w/Powershell v2 上工作。
Function Set-Speaker($Volume){$wshShell = new-object -com wscript.shell;1..50 | % {$wshShell.SendKeys([char]174)};1..$Volume | % {$wshShell.SendKeys([char]175)}}
#
Example usage. Remember each tick is 2%
示例用法。记住每个刻度是 2%
#Sets volume to 60%
Set-Speaker -Volume 30
#Sets volume to 80%
Set-Speaker -Volume 40
#Sets volume to 100%
Set-Speaker -Volume 50
and this function will Toggle-Mute
这个功能将切换静音
Function Toggle-Mute(){$wshShell = new-object -com wscript.shell;$wshShell.SendKeys([char]173)}
#
回答by Keith Hill
Check out this PC Volume Control script on TechNet. It claims to do what you're asking for - well at least on Windows XP. Here's another approach that uses a tool called NirCmd.
在 TechNet 上查看此PC 音量控制脚本。它声称可以满足您的要求 - 至少在 Windows XP 上如此。这是另一种使用名为 NirCmd 的工具的方法。