/// <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 SetSystemVolume(int volumeLevel) { try { int curVolume; int mixerCtrl; //create a new volume control VolumeStructures.MixerStruct volumeCtrl = new VolumeStructures.MixerStruct(); //open the mixer control WinXPSystem.mixerOpen(out mixerCtrl, 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 GetSystemMixer(mixerCtrl, VolumeConstants.MIXERLINE_COMPONENTTYPE_DST_SPEAKERS, controlType, out volumeCtrl, out curVolume); //Check for volume level. If the volume level //is greater than the maximum level then set the volume to //the maximum level. If it's less than the minimum level //then set it to the minimun level if (volumeLevel > volumeCtrl.lMaximum) { volumeLevel = volumeCtrl.lMaximum; } else if (volumeLevel < volumeCtrl.lMinimum) { volumeLevel = volumeCtrl.lMinimum; } //set the volume SetSystemMixer(mixerCtrl, volumeCtrl, volumeLevel); //now re-get the mixer control GetSystemMixer(mixerCtrl, VolumeConstants.MIXERLINE_COMPONENTTYPE_DST_SPEAKERS, controlType, out volumeCtrl, out curVolume); //make sure the volume level is equal to the current volume if (volumeLevel != curVolume) { throw new Exception("Cannot Set Volume"); } //close the mixer control as we are finished with it WinXPSystem.mixerClose(mixerCtrl); } catch (Exception ex) { ChefmateLogger.Instance.LogError("SetSystemVolume", ex.Message); } }
/// <summary> /// method for retrieving the current volume from the system /// </summary> /// <returns>int value</returns> public static int GetSystemVolume() { //method level variables int curVolume; int mixerCtrl; //create a new volume control VolumeStructures.MixerStruct mixer = new VolumeStructures.MixerStruct(); //open the mixer WinXPSystem.mixerOpen(out mixerCtrl, 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 GetSystemMixer(mixerCtrl, VolumeConstants.MIXERLINE_COMPONENTTYPE_DST_SPEAKERS, type, out mixer, out curVolume); //close the mixer control since we are now done with it WinXPSystem.mixerClose(mixerCtrl); //return the current volume to the calling method return(curVolume); }