2015年8月24日 星期一

C# Volume Control in Windows 7 音量控制

English
========================================================================
We talk about the volume control in Windows XP last article. We are introduce the volume control in Windows 7. It is easier to control the volume in Windows 7. We can use Windows API to up, down the volume. The sample code are as below.

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

private const int APPCOMMAND_VOLUME_MUTE = 0x80000;
private const int APPCOMMAND_VOLUME_UP = 0x0a0000;
private const int APPCOMMAND_VOLUME_DOWN = 0x090000;
private const int WM_APPCOMMAND = 0x319;

When we want the volume up, we need to use

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

When we want the volume down, we need to use

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


If we want to get the current volume. We find out an easy way to do it. We need to use NAudio.dll.
There are some steps to help you to use NAudio.dll.

You need to add NAudio in reference and using NAudio.CoreAudioApi


Declare the MMDeviceEnumerator.

static MMDeviceEnumerator devEnum = new MMDeviceEnumerator();
MMDevice defaultDevice = devEnum.GetDefaultAudioEndpoint(DataFlow.Render, Role.Multimedia);

Declare the update function update the volume value.

public void UpdateVolume()
{
    //Update Volume in label.
    currVolume = defaultDevice.AudioEndpointVolume.MasterVolumeLevelScalar * 100;
    double tmp = System.Convert.ToDouble(currVolume);
    int tmp2 = (int)Math.Round(tmp);    //Avoid display float volume.

    lb_Volume.Text = tmp2.ToString();
}

The application will run as below.




中文
========================================================================
上一篇說明在 Windows XP 下控制音量的程式,現在來談談 Windows 7 下,如何調整音量大小。
在 Windows 7 控制音量大小的方式更簡單,Windows 7 內建的API就可以達成,程式碼如下:

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

private const int APPCOMMAND_VOLUME_MUTE = 0x80000;
private const int APPCOMMAND_VOLUME_UP = 0x0a0000;
private const int APPCOMMAND_VOLUME_DOWN = 0x090000;
private const int WM_APPCOMMAND = 0x319;

使用 user32.dll 內的 SendMessageW function。

當需要聲音變大,只需加入以下程式碼:

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

當需要聲音變大,只需加入以下程式碼:

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

至於要如何得到聲音大小的數值,這部分經過網路搜尋,找到比較簡單的方法就是使用 NAudio.dll 來完成,

步驟如下:
先加入 NAudio 在參考中,並使用 NAudio.CoreAudioApi


在程式碼中,先宣告 MMDeviceEnumerator:

static MMDeviceEnumerator devEnum = new MMDeviceEnumerator();
MMDevice defaultDevice = devEnum.GetDefaultAudioEndpoint(DataFlow.Render, Role.Multimedia);

宣告 update function 去更新 volume 的數值,

public void UpdateVolume()
{
    //Update Volume in label.
    currVolume = defaultDevice.AudioEndpointVolume.MasterVolumeLevelScalar * 100;
    double tmp = System.Convert.ToDouble(currVolume);
    int tmp2 = (int)Math.Round(tmp);    //Avoid display float volume.

    lb_Volume.Text = tmp2.ToString();
}

程式執行起來,會如下圖所示。



沒有留言:

OS Operating System 作業系統 恐龍書 筆記分享

發現一個作業系統說明的網站, 對於 process vs thread, semaphore vs mutex, deadlock 說明很詳細, 有興趣的人可以去以下的網頁逛逛。 附上網址連結: link   link2