-2

I have a console program, which needs a way to mute the sound on my computer.

Here's the code that does this, but it was written for the WF. There is an easier-analogue for the console program?

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()));
        }
    }
}

Edit 1: By the way, this code works a bit wrong. With this slider to adjust the volume in the system tray (which adjusts the volume in Windows) does not move. The impression is that my application does not regulate the volume of the system, but something else.

Amazing User
  • 3,473
  • 10
  • 36
  • 75
  • if you cut down the slack this is basically 3 lines of code. Or 5 if you count the using statements. Couldn't get __much__ easier could it? – TaW Apr 20 '14 at 07:59
  • What have you tried to make a console app from this? BTW this code snippet uses legacy API you would normally want to avoid. – Roman R. Apr 20 '14 at 07:59
  • @TaW What? I do not know how to do it. If you know, then show – Amazing User Apr 20 '14 at 08:02

1 Answers1

2

on my computer.

Supposedly you want to mute or change volume on a device rather than for specific application. Or, you could have thought of all devices - you were not specific enough. All in all, the choice of API is an unlucky guess, instead you want Core Audio APIs.

From MSDN:

Audio applications that use the MMDevice API and WASAPI typically use the ISimpleAudioVolume interface to manage stream volume levels on a per-session basis. In rare cases, a specialized audio application might require the use of the IAudioEndpointVolume interface to control the master volume level of an audio endpoint device. A client of IAudioEndpointVolume must take care to avoid the potentially disruptive effects on other audio applications of altering the master volume levels of audio endpoint devices. Typically, the user has exclusive control over the master volume levels through the Windows volume-control program, Sndvol.exe.

This questions is a lead to look for API/interfaces of your interest: Where in the .NET class library is IAudioEndpointVolume located? Also, here is another one for you: Mute/unmute, Change master volume in Windows 7 x64 with C#

Community
  • 1
  • 1
Roman R.
  • 68,205
  • 6
  • 94
  • 158