-
烙印99
我参加聚会有点晚了,但是如果您现在正在寻找的话,有一个可用的nuget包(AudioSwitcher.AudioApi.CoreAudio)可以简化音频交互。安装后,它很简单:CoreAudioDevice defaultPlaybackDevice = new CoreAudioController().DefaultPlaybackDevice;Debug.WriteLine("Current Volume:" + defaultPlaybackDevice.Volume);defaultPlaybackDevice.Volume = 80;
-
青春有我
这是代码:using System;using System.Windows.Forms;using System.Runtime.InteropServices;namespace Test{ public class Test { private const int APPCOMMAND_VOLUME_MUTE = 0x80000; private const int APPCOMMAND_VOLUME_UP = 0xA0000; private const int APPCOMMAND_VOLUME_DOWN = 0x90000; private const int WM_APPCOMMAND = 0x319; [DllImport("user32.dll")] public static extern IntPtr SendMessageW(IntPtr hWnd, int Msg, IntPtr wParam, IntPtr lParam); private void Mute() { SendMessageW(this.Handle, WM_APPCOMMAND, this.Handle, (IntPtr)APPCOMMAND_VOLUME_MUTE); } private void VolDown() { SendMessageW(this.Handle, WM_APPCOMMAND, this.Handle, (IntPtr)APPCOMMAND_VOLUME_DOWN); } private void VolUp() { SendMessageW(this.Handle, WM_APPCOMMAND, this.Handle, (IntPtr)APPCOMMAND_VOLUME_UP); } }}在dotnetcurry上找到使用WPF时,您需要使用new WindowInteropHelper(this).Handle而不是this.Handle (感谢Alex Beals)
-
慕村9548890
如果其他答案中提供的教程过于复杂,则可以使用keybd_event函数尝试实现这种方式[DllImport("user32.dll")]static extern void keybd_event(byte bVk, byte bScan, uint dwFlags, int dwExtraInfo);用法:keybd_event((byte)Keys.VolumeUp, 0, 0, 0); // increase volumekeybd_event((byte)Keys.VolumeDown, 0, 0, 0); // decrease volume