2010-11-20 21 views
7

我得到這個代碼,靜音/取消靜音主音量C#得到主音量電平/ precent

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); 

我想知道我怎麼能得到主音量水平/ precent,因爲我想知道,如果聲音已經靜音或沒有。

編輯: 否則我想分開靜音/取消靜音,所以我將有兩個功能 - 一個用於靜音,一個用於取消靜音。

感謝

回答

1

我不能爲所有的Windows版本做(XP,VISTA & 7)。
雖然我通過使用外部程序(如NirCmd)實現了它,併發送了我需要的命令。

不是很好的解決方案,但它確實解決了我的問題。

0

This thread顯示瞭如何控制從C#主音量。

您可能也有興趣在迴應這個問題:Get Master Sound Volume in c#

尤其是NAudio託管包裝。

+0

試過了,它沒有工作。 – Ron 2010-11-20 22:10:28

+1

什麼,具體而言,沒有工作?線程中的註釋表明它可以工作。 – 2010-11-20 22:38:19

+0

使用函數GetVolume()並返回65535,在我改變了音量後,它返回了相同的值--65535。也許我使用了錯誤的函數? – Ron 2010-11-20 22:49:08

4

看一看這個項目http://www.codeproject.com/KB/vista/CoreAudio.aspx

他們創造了自己的混音器控制,這也報告當前volumne和靜音/取消靜音狀態:

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); 
} 

這是否幫助你嗎?

編輯:有了這個代碼,並從該項目中類,你應該能夠直接設置和取消靜音(不含切換):

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

CodeProject鏈接已經死了 – 2018-02-18 01:18:55

+0

這可以工作的奇蹟。請注意,您需要使用以下命名空間來引用NAudio.dll:使用NAudio.CoreAudioApi ;. MasterVolumeLevelScalar是一個線性0-1比例,而MasterVolumeLevel是一些非線性比例。 – 2018-02-18 01:53:37