Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
824 views
in Technique[技术] by (71.8m points)

.net - C# get master volume level/precent

I got this code to mute/unmute the master volume

private const int APPCOMMAND_VOLUME_MUTE = 0x80000;
private const int WM_APPCOMMAND = 0x319;

[DllImport("user32.dll")]
public static extern IntPtr SendMessageW(IntPtr hWnd, int Msg, IntPtr wParam, IntPtr lParam);

SendMessageW(this.Handle, WM_APPCOMMAND, this.Handle, (IntPtr)APPCOMMAND_VOLUME_MUTE);

I would to know how can I get the master volume level/precent because I want to know if the sound is already muted or not.

Edit: or else I would like to split the mute/unmute sound so I will have two functions - one for mute and one for unmute.

thanks

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Have a look at this project http://www.codeproject.com/KB/vista/CoreAudio.aspx

They created an own mixer control, that also reports the current volumne and the mute/unmute state:

defaultDevice.AudioEndpointVolume.OnVolumeNotification += new AudioEndpointVolumeNotificationDelegate(
    AudioEndpointVolume_OnVolumeNotification);
// .. snip ..
void AudioEndpointVolume_OnVolumeNotification(AudioVolumeNotificationData data)
{
    Console.WriteLine("New Volume {0}", data.MasterVolume);
    Console.WriteLine("Muted      {0}", data.Muted);
}

Does this help you?

EDIT: With this code and the class from the project you should be able to set and unset mute directly (without toggle):

MMDeviceEnumerator devEnum = new MMDeviceEnumerator();
MMDevice defaultDevice = devEnum.GetDefaultAudioEndpoint(EDataFlow.eRender, ERole.eMultimedia);
defaultDevice.AudioEndpointVolume.Mute = true; // or false

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...