suroMind

[C#] Sound Volume Control 본문

IT분야/C#

[C#] Sound Volume Control

suroMind 2012. 1. 6. 15:51
Visual Studio 2010에서 테스트하였습니다.


using System;
using System.Runtime.InteropServices;

namespace WindowsFormsApplication2 {
    public class Win32 {
        #region 사운드 관련
        [DllImport("winmm.dll")]
        public static extern int waveOutGetVolume(IntPtr hwo, out uint dwVolume);

        [DllImport("winmm.dll")]
        public static extern int waveOutSetVolume(IntPtr hwo, uint dwVolume);

        public static void SetSoundVolume(int volume) {
            try {
                int newVolume = ((ushort.MaxValue / 10) * volume);
                uint newVolumeAllChannels = (((uint)newVolume & 0x0000ffff) | ((uint)newVolume << 16));
                waveOutSetVolume(IntPtr.Zero, newVolumeAllChannels);
            } catch (Exception) { }
        }

        public static int GetSoundVolume() {
            int value = 0;
            try {
                uint CurrVol = 0;
                waveOutGetVolume(IntPtr.Zero, out CurrVol);
                ushort CalcVol = (ushort)(CurrVol & 0x0000ffff);
                value = CalcVol / (ushort.MaxValue / 10);
            } catch (Exception) { }
            return value;
        }
        #endregion
    }
}

TrackBar 의 Value에 바로 SetSoundVolume() 또는 GetSoundVolume()으로 표현하면 됩니다.
TrackBar의 Minimum값은 0 , Maximum값은 10으로 셋팅하여 테스트 하였습니다.

출처 : http://www.geekpedia.com/tutorial176_Get-and-set-the-wave-sound-volume.html

 
Comments