[译][C#]Vista的核心音频API主音量控制

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;
using System.Windows.Forms;
//CoreAudioApi支持Vista/Win7,下面的代码 是最早的API 函数 支持不好  不推荐使用
namespace SwitchIP
{
    class VolumeControl
    {
        [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
        static extern IntPtr SendMessage(IntPtr hWnd, uint Msg, uint wParam, uint lParam);
        const uint WM_APPCOMMAND = 0x319;
        const uint APPCOMMAND_VOLUME_UP = 0x0a;
        const uint APPCOMMAND_VOLUME_DOWN = 0x09;
        const uint APPCOMMAND_VOLUME_MUTE = 0x08;
        //增大音量
        public  void VolumeUp(){
            SendMessage(this.Handle, WM_APPCOMMAND, 0x30292, APPCOMMAND_VOLUME_UP * 0x10000);   
        }
        //降低音量
        public  void VolumeDown() {
            SendMessage(this.Handle, WM_APPCOMMAND, 0x30292, APPCOMMAND_VOLUME_DOWN * 0x10000); 

        }
        //该函数会自动在静音和非静音状态下切换
        public  void VolumeMute() {
            SendMessage(myForm.Handle, WM_APPCOMMAND, 0x200eb0, APPCOMMAND_VOLUME_MUTE * 0x10000); 
        }

            
    }
}

参考 : http://www.codeproject.com/KB/vista/CoreAudio.aspx     


非原创




using CoreAudioApi;
/*[email protected]
需要引用CoreAudioApi.dll
可在下面的链接中找到
*/
......
private MMDevice defaultDevice = null;

        */
        //判断当前系统音频状态
        private bool isMuted()
        {
           return defaultDevice.AudioEndpointVolume.Mute;
        }

        //静音
        private void setMute()
        {
            defaultDevice.AudioEndpointVolume.Mute = true;
        }
        //解除静音
        private void setUnMute()
        {
            defaultDevice.AudioEndpointVolume.Mute = false;
        }
        //初始化MMDevice
        private void muteInit()
        {
            MMDeviceEnumerator devEnum = new MMDeviceEnumerator();
            defaultDevice =
             devEnum.GetDefaultAudioEndpoint(EDataFlow.eRender, ERole.eMultimedia);
        }


作者:qq283868910 发表于2011-12-7 15:57:53 原文链接
阅读:56 评论:0 查看评论

你可能感兴趣的:(api)