项目场景:
最近做项目需要调节电脑音量,写一篇文章记录一下使用第三方库来调节系统音量
解决方案:
直接使用第三方库 AudioSwitcher Core Audio Api
安装后开始使用,UI&api如下
using AudioSwitcher.AudioApi.CoreAudio;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace test222
{
public partial class Form3 : Form
{
private CoreAudioDevice defaultPlayBackDevice = null;
public Form3()
{
InitializeComponent();
defaultPlayBackDevice = new CoreAudioController().DefaultPlaybackDevice;
//获取当前音量
double vol = defaultPlayBackDevice.Volume;
this.numCurVol.Value = Convert.ToDecimal(vol);
//是否静音
bool ismute = defaultPlayBackDevice.IsMuted;
}
private void btnSetUpVol_Click(object sender, EventArgs e)
{
//设置音量
defaultPlayBackDevice.Volume = Convert.ToDouble(this.numSetUpVol.Value);
}
private void btnMute_Click(object sender, EventArgs e)
{
//设置静音
defaultPlayBackDevice.Mute(true);
}
}
}