繁体   English   中英

从 powershell 更改音频音量的平衡级别?

[英]Change balance level for audio volume from powershell?

虽然我已经阅读了几种使用 Powershell 更改音量的解决方案:

如何从 PowerShell 静音/取消静音

从 powershell 更改音频级别?

我找不到任何改变平衡的东西。 就像是:

#Sets left channel volume to 20%
Set-Speaker -LeftChannel 20

#Sets right channel volume to 80%
Set-Speaker -RightChannel 80

需要明确的是,我正在谈论修改这个:

在此处输入图像描述

我需要它来设置一个启动脚本来保持我当前(或最后)的音量,但将左声道始终保持在 20%

谢谢

如此所述,您需要调用SetChannelVolumeLevel IAudioEndpointVolume SetChannelVolumeLevelScalar方法。 interop 方法包含在这个 Gist中,但它缺少一个方便的包装方法。

要开始使用,您需要使用Add-Type在 PowerShell 中包含 C# 代码:

Add-Type -TypeDefinition @'
-paste C# code here-
'@

将此方法添加到 class AudioManager

        /// <summary>
        /// Sets the channel volume to a specific level
        /// </summary>
        /// <param name="channelNumber">Channel number, which can be 0..(numberOfChannels-1)</param>
        /// <param name="newLevel">Value between 0 and 100 indicating the desired scalar value of the volume</param>
        public static void SetChannelVolumeLevel(uint channelNumber, float newLevel)
        {
            IAudioEndpointVolume masterVol = null;
            try
            {
                masterVol = GetMasterVolumeObject();
                if (masterVol == null)
                    return;

                masterVol.SetChannelVolumeLevelScalar(channelNumber, newLevel/100, Guid.Empty);
            }
            finally
            {
                if (masterVol != null)
                    Marshal.ReleaseComObject(masterVol);
            }
        }      

我已经在private static IAudioEndpointVolume GetMasterVolumeObject()行之前插入了它,但是将它放在 class 中的什么位置并不重要。

现在你可以像这样从 PowerShell 调用它:

[VideoPlayerController.AudioManager]::SetChannelVolumeLevelScalar(0, 60)
[VideoPlayerController.AudioManager]::SetChannelVolumeLevelScalar(1, 40)

在我的系统上,它会同时移动左右音量,但我可能会遇到锁定平衡的问题。 这里描述了一个注册表调整,但对我不起作用。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM