Cambio de volumen de sonido de código .NET
Pregunta
¿Hay una manera fácil de ajustar el volumen de código administrado de .NET?
Solución
Esta bastante largo artículo muestra cómo: El control de volumen del sonido en C #
Otros consejos
Esto lo hace por mi Windows 7:
Descargar NAudio (http://naudio.codeplex.com/releases/view/79035) y hacer referencia a la DLL en el proyecto. Que añadir el siguiente código:
try
{
//Instantiate an Enumerator to find audio devices
NAudio.CoreAudioApi.MMDeviceEnumerator MMDE = new NAudio.CoreAudioApi.MMDeviceEnumerator();
//Get all the devices, no matter what condition or status
NAudio.CoreAudioApi.MMDeviceCollection DevCol = MMDE.EnumerateAudioEndPoints(NAudio.CoreAudioApi.DataFlow.All, NAudio.CoreAudioApi.DeviceState.All);
//Loop through all devices
foreach (NAudio.CoreAudioApi.MMDevice dev in DevCol)
{
try
{
//Set at maximum volume
dev.AudioEndpointVolume.MasterVolumeLevel = 0;
//Get its audio volume
System.Diagnostics.Debug.Print("Volume of " + dev.FriendlyName + " is " + dev.AudioEndpointVolume.MasterVolumeLevel.ToString());
//Mute it
dev.AudioEndpointVolume.Mute = true;
System.Diagnostics.Debug.Print(dev.FriendlyName + " is muted");
}
catch (Exception ex)
{
//Do something with exception when an audio endpoint could not be muted
System.Diagnostics.Debug.Print(dev.FriendlyName + " could not be muted");
}
}
}
catch (Exception ex)
{
//When something happend that prevent us to iterate through the devices
System.Diagnostics.Debug.Print("Could not enumerate devices due to an excepion: " + ex.Message);
}
Este artículo CodeProject demuestra cómo controlar totalmente el mezclador de Windows configuraciones, incluyendo el volumen principal para el sistema. Parece para envolver la mayor parte de las horribles cosas API Win, por lo que es probablemente la forma más fácil de seguir.
Respuesta simple: Usted tiene que usar interoperabilidad
.Me escribió una biblioteca para hacer todo tipo de cosas de sonido para usted, aunque:
WinnMM.Net: http://winmm.codeplex.com/