Solved

Mute Sound in C# without using DirectX

Posted on 2003-10-27
2
3,255 Views
Last Modified: 2012-08-13
Hi!

I need to code a program which is able to mute the computers audio.
This program should run on xp / nt... i don't exactly know if NT pcs need directX but i know for sure that its not installed...

Is there a way?
0
Comment
Question by:eliaZ
  • 2
2 Comments
 
LVL 22

Accepted Solution

by:
_TAD_ earned 250 total points
ID: 9627653



Here is the WINAPI for the dll you can use if you don't want to go through the hassle of using DIRECTX.

[DllImport("winmm.dll")]
Private static extern long auxSetVolume(long uDeviceID, long dwVolume);

uDeviceID:
The device ID of the auxiliary audio device to set the volume of. Valid values range from zero to the number of auxiliary audio devices minus one.

dwVolume:
The new volume setting of the device. If the device supports separate left and right channel volumes, the low-order word contains the left channel volume and the high-order word contains the right channel volume. If the device supports only one overall volume, the low-order word is the volume. Valid volumes range from 0 to 0xFFFF.



When referencing this as a LONG value, I am pretty sure that they mean this is an Int32 value (which is a standard int primitive type in .NET, where as Int16 is the standard size of the Integer primitive type in most other platforms).  Also something to note is how the volume is controlled.  you may have to do some Hex conversion to get it right.

//sets both the left and right speakers to half volume
retval=auxSetVolume(0,0x80008000);

//sets both the left and right speakers to full volume
retval=auxSetVolume(0,0xFFFFFFFF);

//Mutes both the left and right speakers
retval=auxSetVolume(0,0);

0
 
LVL 22

Assisted Solution

by:_TAD_
_TAD_ earned 250 total points
ID: 9627678


The downside to using WINAPI is that it actually affects the OS.  If you want to be nice, you will first get the sound settings and store them in a variable somwhere.  Mute the sound, and then when your program shuts down set the sound level to the place where it was before.

But that's just my opinion.
0

Featured Post

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.

856 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question