【问题标题】:How to programmatically disable the sound?如何以编程方式禁用声音?
【发布时间】:2014-04-20 07:54:23
【问题描述】:

我有一个控制台程序,它需要一种方法来使 我的计算机上的声音静音。

这是执行此操作的代码,但它是为 WF 编写的。控制台程序有更简单的模拟吗?

using System;
using System.Windows.Forms;
using System.Runtime.InteropServices;

namespace VolumeOff
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        [DllImport("winmm.dll", SetLastError = true, CharSet = CharSet.Auto)]
        public static extern uint waveOutGetVolume(uint hwo, ref uint dwVolume);

        [DllImport("winmm.dll", SetLastError = true, CallingConvention = 
                     CallingConvention.Winapi)]
        public static extern int waveOutSetVolume(uint uDeviceID, uint dwVolume);

        private void button1_Click(object sender, EventArgs e)
        {
                uint volume = 0;           
                uint hWO = 0;
                waveOutGetVolume(hWO, ref volume);       
                textBox1.Text = volume.ToString();
        }

        private void button2_Click(object sender, EventArgs e)
        {           
                uint hWO = 0;              
                waveOutSetVolume(hWO, Convert.ToUInt32(textBox1.Text.ToString()));
        }
    }
}

编辑 1: 顺便说一句,这段代码有点错误。使用此滑块调整系统托盘中的音量(在 Windows 中调整音量)不会移动。印象是我的应用不调节系统的音量,而是别的什么。

【问题讨论】:

  • 如果你减少松弛,这基本上是 3 行代码。如果计算 using 语句,则为 5。不能更容易吗?
  • 你有什么尝试以此来制作控制台应用程序?顺便说一句,此代码 sn-p 使用您通常希望避免的旧 API。
  • @TaW 什么?我不知道怎么做。如果你知道,然后显示

标签: c# audio


【解决方案1】:

在我的电脑上。

假设您想在设备上静音或更改音量,而不是针对特定应用程序。或者,您可以考虑 所有 设备 - 您不够具体。总而言之,API 的选择是一个不幸的猜测,而不是您想要 Core Audio API。

From MSDN:

使用 MMDevice API 和 WASAPI 的音频应用程序通常使用 ISimpleAudioVolume 接口来管理每个会话的流音量级别。 在极少数情况下,专门的音频应用程序可能需要使用 IAudioEndpointVolume 接口来控制音频端点设备的主音量级别。 IAudioEndpointVolume 的客户端必须注意避免更改音频端点设备的主音量级别对其他音频应用程序的潜在破坏性影响。通常,用户可以通过 Windows 音量控制程序 Sndvol.exe 对主音量级别进行独占控制。

这个问题是寻找您感兴趣的 API/接口的线索:Where in the .NET class library is IAudioEndpointVolume located? 另外,这里还有一个适合您的问题:Mute/unmute, Change master volume in Windows 7 x64 with C#

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-03-14
    • 2014-02-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-02-19
    相关资源
    最近更新 更多