Controlling your system volume takes a lot of advanced coding, because you have to hook into the operating system in order to gain access to the controls needed, thats the reason for all the Win32 API's. The easiest way to do this is by using a mixer control, which is what we will be doing in this tutorial. This tutorial may be rather long, as this is a complete class library, with class files for:
- All the Win32 API Calls
- All the structures
- All the constants needed
- Class for doing the actual work
I will start by showing the other 3 classes first, so you can see first hand the items needed for taking control of the system's volume controls.
The first class we will see is the Win32 API class. In this class is where we will be storing all the Win32 calls needed for controlling the sound. Since each section is it's own class, we will need to reference certain Namespaces for each class. The Namespaces we need for the Win32 class are:
using System; using System.Collections.Generic; using System.Text; using System.Runtime.InteropServices; //custom Namespaces using VolumeControl.Library.Constants; using VolumeControl.Library.Structs;
You will notice there are 2 custom Namespaces, those are our other class files we will be getting to shortly. Since our classes reference custom Namespaces, you should have guessed that all the class are a member of the VolumeControl.Library Namespace, but each also have an appendage to that, depending on which class it is. The Namespaces we create are:
- VolumeControl.Library.Structs
- VolumeControl.Library.Win32
- VolumeControl.Library.Constants
The Namespace for the Win32 class is, you guessed it, VolumeControl.Library.Win32, I am going to make an assumption here, since you continued to read along in this tutorial I'm assuming you know about Win32 API calls, and how to look up their function if you needed to, so I am not going to go into great detail about what these API calls do. If you want to look them up, a good place to start is PInvoke.Net, they have pretty much every Win32 API in programming.
In this class we have Win32 API's for opening a mixer control, closing a mixer control, getting the details of the current mixer control, getting the ID of the mixer control, and more. Here are the Win32 API's we will be needing:
- mixerClose
- mixerGetControlDetailsA
- mixerGetDevCapsA
- mixerGetID
- mixerGetLineControlsA
- mixerGetLineInfoA
- mixerGetNumDevs
- mixerMessage
- mixerOpen
- mixerSetControlDetails
So now lets declare our Win32 API's:
[DllImport("winmm.dll", CharSet=CharSet.Ansi)]
public static extern int mixerClose (int hmx);
[DllImport("winmm.dll", CharSet=CharSet.Ansi)]
public static extern int mixerGetControlDetailsA(int hmxobj, ref VolumeStructs.MixerDetails pmxcd, int fdwDetails);
[DllImport("winmm.dll", CharSet=CharSet.Ansi)]
public static extern int mixerGetDevCapsA(int uMxId, VolumeStructs.MixerCaps pmxcaps, int cbmxcaps);
[DllImport("winmm.dll", CharSet=CharSet.Ansi)]
public static extern int mixerGetID(int hmxobj, int pumxID, int fdwId);
[DllImport("winmm.dll", CharSet=CharSet.Ansi)]
public static extern int mixerGetLineControlsA(int hmxobj, ref VolumeStructs.LineControls pmxlc, int fdwControls);
[DllImport("winmm.dll", CharSet=CharSet.Ansi)]
public static extern int mixerGetLineInfoA(int hmxobj, ref VolumeStructs.MixerLine pmxl, int fdwInfo);
[DllImport("winmm.dll", CharSet=CharSet.Ansi)]
public static extern int mixerGetNumDevs();
[DllImport("winmm.dll", CharSet=CharSet.Ansi)]
public static extern int mixerMessage(int hmx, int uMsg, int dwParam1, int dwParam2);
[DllImport("winmm.dll", CharSet=CharSet.Ansi)]
public static extern int mixerOpen(out int phmx, int uMxId, int dwCallback, int dwInstance, int fdwOpen);
[DllImport("winmm.dll", CharSet=CharSet.Ansi)]
public static extern int mixerSetControlDetails(int hmxobj, ref VolumeStructs.MixerDetails pmxcd, int fdwDetails);
Now we will look at the structs we are going to be using for this class library. A struct is normally used to encapsulate small pieces of related data, such as we are doing in this library. First, as with any class, we need our Namespace references, here are the Namespaces you need to use in your structs class
using System; using System.Runtime.InteropServices; //custom namespaces using VolumeControl.Library.Constants;
As you can see, we will be using the constants class in our structs class. Now for the structs we will be using. First is a struct to hold the data about the caps of the mixer
/// <summary>
/// struct for holding data for the mixer caps
/// </summary>
public struct MixerCaps
{
public int wMid;
public int wPid;
public int vDriverVersion;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = VolumeConstants.MAXPNAMELEN)] public string szPname;
public int fdwSupport;
public int cDestinations;
}
Next is the struct to hold the data for the mixer control itself
/// <summary>
/// struct to hold data for the mixer control
/// </summary>
public struct Mixer
{
public int cbStruct;
public int dwControlID;
public int dwControlType;
public int fdwControl;
public int cMultipleItems;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = VolumeConstants.MIXER_SHORT_NAME_CHARS)] public string szShortName;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = VolumeConstants.MIXER_LONG_NAME_CHARS)] public string szName;
public int lMinimum;
public int lMaximum;
[MarshalAs(UnmanagedType.U4, SizeConst = 10)] public int reserved;
}
struct for holding data for the details of the mixer control
/// <summary>
/// struct for holding data about the details of the mixer control
/// </summary>
public struct MixerDetails
{
public int cbStruct;
public int dwControlID;
public int cChannels;
public int item;
public int cbDetails;
public IntPtr paDetails;
}
Now a struct for an unsigned mixer control
/// <summary>
/// struct to hold data for an unsigned mixer control details
/// </summary>
public struct UnsignedMixerDetails
{
public int dwValue;
}
A struct for holding data about the mixer line being used
/// <summary>
/// struct to hold data for the mixer line
/// </summary>
public struct MixerLine
{
public int cbStruct;
public int dwDestination;
public int dwSource;
public int dwLineID;
public int fdwLine;
public int dwUser;
public int dwComponentType;
public int cChannels;
public int cConnections;
public int cControls;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = VolumeConstants.MIXER_SHORT_NAME_CHARS)] public string szShortName;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = VolumeConstants.MIXER_LONG_NAME_CHARS)] public string szName;
public int dwType;
public int dwDeviceID;
public int wMid;
public int wPid;
public int vDriverVersion;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = VolumeConstants.MAXPNAMELEN)] public string szPname;
}
A struct for holding the data for the mixer line controls being used
/// <summary>
/// struct for holding data for the mixer line controls
/// </summary>
public struct LineControls
{
public int cbStruct;
public int dwLineID;
public int dwControl;
public int cControls;
public int cbmxctrl;
public IntPtr pamxctrl;
}
Ok, we have our Win32 API's and structs out of the way, now lets look at the constants needed for using a mixer control to take control of the volume control. Lets take a look at the constants for accomplishing this task:
/// <summary>
/// Class to hold all the constants needed for controlling the system sound
/// </summary>
public static class VolumeConstants
{
public const int MMSYSERR_NOERROR = 0;
public const int MAXPNAMELEN = 32;
public const int MIXER_LONG_NAME_CHARS = 64;
public const int MIXER_SHORT_NAME_CHARS = 16;
public const int MIXER_GETLINEINFOF_COMPONENTTYPE = 0x3;
public const int MIXER_GETCONTROLDETAILSF_VALUE = 0x0;
public const int MIXER_GETLINECONTROLSF_ONEBYTYPE = 0x2;
public const int MIXER_SETCONTROLDETAILSF_VALUE = 0x0;
public const int MIXERLINE_COMPONENTTYPE_DST_FIRST = 0x0;
public const int MIXERLINE_COMPONENTTYPE_SRC_FIRST = 0x1000;
public const int MIXERCONTROL_CT_CLASS_FADER = 0x50000000;
public const int MIXERCONTROL_CT_UNITS_UNSIGNED = 0x30000;
public const int MIXERCONTROL_CONTROLTYPE_FADER = (MIXERCONTROL_CT_CLASS_FADER | MIXERCONTROL_CT_UNITS_UNSIGNED);
public const int MIXERCONTROL_CONTROLTYPE_VOLUME = (MIXERCONTROL_CONTROLTYPE_FADER + 1);
public const int MIXERLINE_COMPONENTTYPE_DST_SPEAKERS = (MIXERLINE_COMPONENTTYPE_DST_FIRST + 4);
public const int MIXERLINE_COMPONENTTYPE_SRC_MICROPHONE = (MIXERLINE_COMPONENTTYPE_SRC_FIRST + 3);
public const int MIXERLINE_COMPONENTTYPE_SRC_LINE = (MIXERLINE_COMPONENTTYPE_SRC_FIRST + 2);
}
NOTE: You will notice that the first 3 class are all declared as static, this is done so we don't have to create an instance of the class to use the items it contains. As with structs, there is a raging debate over using static classes, as far as I'm concerned the jury is still out on this.
Now on to the class that actually does all the work. In this class we have 4 methods, 1 for getting the mixer control, 1 for setting the mixer control, 1 for getting the current volume level, and finally 1 for setting the volume level. First the method for getting the mixer control. In this method we will be creating a new mixer control, we will retrieve it and use the method for setting its properties to set all the information we need for the mixer.
In this class you will be using many methods of the Marshal Class, located in the System.Runtime.InteropServices Namespace. We will be allocating memory blocks for our mixer control using the AllocCoTaskMem Method, we will be grabbing the size of our unmanaged type using the Marshal.SizeOf Method and more.
This is one of the reasons I said at the start of this tutorial that it is an advanced tutorial, and requires a firm grasp of the language, and the objects used, to understand what is going on. Before we can write any code that is going to work, we need to make sure we have references to the proper Namespaces, here are the Namespaces you will need to reference:
using System; using System.Collections.Generic; using System.Text; using System.Runtime.InteropServices; using System.Windows.Forms; //custom Namespaces using VolumeControl.Library.Constants; using VolumeControl.Library.Structs; using VolumeControl.Library.Win32;
Now for the first method in our class, the GetMixer method:
/// <summary>
/// method to retrieve a mixer control
/// </summary>
/// <param name="i"></param>
/// <param name="type"></param>
/// <param name="ctrlType"></param>
/// <param name="mixerControl"></param>
/// <param name="currVolume"></param>
/// <returns></returns>
private static bool GetMixer(int i, int type, int ctrlType, out VolumeStructs.Mixer mixerControl, out int currVolume)
{
//create our method level variables
int details;
bool bReturn;
currVolume = -1;
//create our struct objects
VolumeStructs.LineControls lineControls = new VolumeStructs.LineControls();
VolumeStructs.MixerLine line = new VolumeStructs.MixerLine();
VolumeStructs.MixerDetails mcDetails = new VolumeStructs.MixerDetails();
VolumeStructs.UnsignedMixerDetails detailsUnsigned = new VolumeStructs.UnsignedMixerDetails();
//create a new mixer control
mixerControl = new VolumeStructs.Mixer();
//set the properties of out mixerline object
line.cbStruct = Marshal.SizeOf(line);
line.dwComponentType = type;
//get the line info and assign it to our details variable
details = PCWin32.mixerGetLineInfoA(i, ref line, VolumeConstants.MIXER_GETLINEINFOF_COMPONENTTYPE);
//make sure we didnt receive any errors
if (VolumeConstants.MMSYSERR_NOERROR == details)
{
int mcSize = 152;
//get the size of the unmanaged type
int control = Marshal.SizeOf(typeof(VolumeStructs.Mixer));
//allocate a block of memory
lineControls.pamxctrl = Marshal.AllocCoTaskMem(mcSize);
//get the size of the line controls
lineControls.cbStruct = Marshal.SizeOf(lineControls);
//set properties for our mixer control
lineControls.dwLineID = line.dwLineID;
lineControls.dwControl = ctrlType;
lineControls.cControls = 1;
lineControls.cbmxctrl = mcSize;
// Allocate a buffer for the control
mixerControl.cbStruct = mcSize;
// Get the control
details = PCWin32.mixerGetLineControlsA(i, ref lineControls, VolumeConstants.MIXER_GETLINECONTROLSF_ONEBYTYPE);
//once again check to see if we received any errors
if (VolumeConstants.MMSYSERR_NOERROR == details)
{
bReturn = true;
//Copy the control into the destination structure
mixerControl = (VolumeStructs.Mixer)Marshal.PtrToStructure(lineControls.pamxctrl, typeof(VolumeStructs.Mixer));
}
else
{
bReturn = false;
}
int mcDetailsSize = Marshal.SizeOf(typeof(VolumeStructs.MixerDetails));
int mcDetailsUnsigned = Marshal.SizeOf(typeof(VolumeStructs.UnsignedMixerDetails));
mcDetails.cbStruct = mcDetailsSize;
mcDetails.dwControlID = mixerControl.dwControlID;
mcDetails.paDetails = Marshal.AllocCoTaskMem(mcDetailsUnsigned);
mcDetails.cChannels = 1;
mcDetails.item = 0;
mcDetails.cbDetails = mcDetailsUnsigned;
details = PCWin32.mixerGetControlDetailsA(i, ref mcDetails, VolumeConstants.MIXER_GETCONTROLDETAILSF_VALUE);
detailsUnsigned = (VolumeStructs.UnsignedMixerDetails)Marshal.PtrToStructure(mcDetails.paDetails, typeof(VolumeStructs.UnsignedMixerDetails));
currVolume = detailsUnsigned.dwValue;
return bReturn;
}
bReturn = false;
return bReturn;
}
Next we will be looking at the SetMixer method. This is the method that does all the work for changing the volume level. It is called from the SetVolume method. We will be passing this method the mixer control we're using, the level we want the volume set at:
/// <summary>
/// method for setting the value for a volume control
/// </summary>
/// <param name="i"></param>
/// <param name="mixerControl"></param>
/// <param name="volumeLevel"></param>
/// <returns>true/false</returns>
private static bool SetMixer(int i, VolumeStructs.Mixer mixerControl, int volumeLevel)
{
//method level variables
bool bReturn;
int details;
//create our struct object for controlling the system sound
VolumeStructs.MixerDetails mixerDetails = new VolumeStructs.MixerDetails();
VolumeStructs.UnsignedMixerDetails volume = new VolumeStructs.UnsignedMixerDetails();
//set out mixer control properties
mixerDetails.item = 0;
//set the id of the mixer control
mixerDetails.dwControlID = mixerControl.dwControlID;
//return the size of the mixer details struct
mixerDetails.cbStruct = Marshal.SizeOf(mixerDetails);
//return the volume
mixerDetails.cbDetails = Marshal.SizeOf(volume);
//Allocate a buffer for the mixer control value buffer
mixerDetails.cChannels = 1;
volume.dwValue = volumeLevel;
//Copy the data into the mixer control value buffer
mixerDetails.paDetails = Marshal.AllocCoTaskMem(Marshal.SizeOf(typeof(VolumeStructs.UnsignedMixerDetails)));
Marshal.StructureToPtr(volume, mixerDetails.paDetails, false);
//Set the control value
details = PCWin32.mixerSetControlDetails(i, ref mixerDetails, VolumeConstants.MIXER_SETCONTROLDETAILSF_VALUE);
//Check to see if any errors were returned
if (VolumeConstants.MMSYSERR_NOERROR == details)
{
bReturn = true;
}
else
{
bReturn = false;
}
return bReturn;
}
Now before you can set the volume you need to know what the current volume is, that is the job of our GetVolume method. Here we will call our GetMixer method which will allow us to grab the current volume level. Remember to always close your mixer object when you are done using it to free up those resources, and to prevent any memory leaks.
NOTE: .Net languages are managed languages and do a pretty good job managing resources and memory usage, but here we are dealing with an unmanaged type so we need to ensure we are closing items when we dont need them anymore.
Now for the GetVolume method:
/// <summary>
/// method for retrieving the current volume from the system
/// </summary>
/// <returns>int value</returns>
public static int GetVolume()
{
//method level variables
int currVolume;
int mixerControl;
//create a new volume control
VolumeStructs.Mixer mixer = new VolumeStructs.Mixer();
//open the mixer
PCWin32.mixerOpen(out mixerControl, 0, 0, 0, 0);
//set the type to volume control type
int type = VolumeConstants.MIXERCONTROL_CONTROLTYPE_VOLUME;
//get the mixer control and get the current volume level
GetMixer(mixerControl, VolumeConstants.MIXERLINE_COMPONENTTYPE_DST_SPEAKERS, type, out mixer, out currVolume);
//close the mixer control since we are now done with it
PCWin32.mixerClose(mixerControl);
//return the current volume to the calling method
return currVolume;
}
The final method we have is the method for setting the volume to a specified level. Here we will create a new mixer control (as we do in every method in this class, because we make sure to close it as soon as we're done with it). We will then open the mixer, then we will check the value that is being passed for the volume level. If a value greater than the maximum level we will set the level at the maximum value, same with the minimum level, if the value provided is lower than the minimum value we will set the volume level at the minimum level. Once we are done we, once again, close the mixer to free up those resources:
/// <summary>
/// method for setting the volume to a specific level
/// </summary>
/// <param name="volumeLevel">volume level we wish to set volume to</param>
public static void SetVolume(int volumeLevel)
{
try
{
//method level variables
int currVolume;
int mixerControl;
//create a new volume control
VolumeStructs.Mixer volumeControl = new VolumeStructs.Mixer();
//open the mixer control
PCWin32.mixerOpen(out mixerControl, 0, 0, 0, 0);
//set the type to volume control type
int controlType = VolumeConstants.MIXERCONTROL_CONTROLTYPE_VOLUME;
//get the current mixer control and get the current volume
GetMixer(mixerControl, VolumeConstants.MIXERLINE_COMPONENTTYPE_DST_SPEAKERS, controlType, out volumeControl, out currVolume);
//now check the volume level. If the volume level
//is greater than the max then set the volume to
//the max level. If it's less than the minimum level
//then set it to the minimun level
if (volumeLevel > volumeControl.lMaximum)
{
volumeLevel = volumeControl.lMaximum;
}
else if (volumeLevel < volumeControl.lMinimum)
{
volumeLevel = volumeControl.lMinimum;
}
//set the volume
SetMixer(mixerControl, volumeControl, volumeLevel);
//now re-get the mixer control
GetMixer(mixerControl, VolumeConstants.MIXERLINE_COMPONENTTYPE_DST_SPEAKERS, controlType, out volumeControl, out currVolume);
//make sure the volume level is equal to the current volume
if (volumeLevel != currVolume)
{
throw new Exception("Cannot Set Volume");
}
//close the mixer control as we are finished with it
PCWin32.mixerClose(mixerControl);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
Now that we have created this class library you're probably wondering how you actually use it. Well here is an example of how you would use this in, say the click event of a button on your form. When we click the button we will first display the current volume level, we will then set a new volume level, then we will re-display the current volume level:
private void button1_Click(object sender, System.EventArgs e)
{
//first we will display the current volume
Label1.Text = VolumeControl.GetVolume.ToString();
//now we will change the volume level
VolumeControl.SetVolume(50);
//now we will display the new volume level
Label1.Text = VolumeControl.GetVolume().ToString();
}
There you have it! A way to create a Windows mixer control and control the volume of the computer the application is running on. I truly hope you found this tutorial useful and informative, I know I learned a lot when creating this class library. I am including the solution for this class library, all the license information and headers must remain in tact because it is convered by the GNU - General Public License, but you are free to modify and distribute as you wish. Thank you for reading.
PC_VolumeControl.zip (33.98K)
Number of downloads: 12139
Happy Coding!




MultiQuote






|