C# 音频切换器API更改输出设备

C# 音频切换器API更改输出设备,c#,windows,api,audio,device,C#,Windows,Api,Audio,Device,我制作了一个带有Arduino的小装置,它通过串行方式将2个值发送到我的c#程序(音量的电位计值和改变输出设备的开关按钮)。音量部分已经完成,但我无法在两个输出设备(显示器音频和耳机)之间切换 我现在的代码是: using AudioSwitcher.AudioApi.CoreAudio; using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using Sy

我制作了一个带有Arduino的小装置,它通过串行方式将2个值发送到我的c#程序(音量的电位计值和改变输出设备的开关按钮)。音量部分已经完成,但我无法在两个输出设备(显示器音频和耳机)之间切换

我现在的代码是:

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 Volume
{
public partial class Form1 : Form
{
    //msg recieved by serial
    String msg;
    string[] tokens;

    //active device
    CoreAudioDevice defaultPlaybackDevice = new CoreAudioController().DefaultPlaybackDevice;

    String PHONES = "Headphones (Razer Kraken USB)";
    String COLUNA = "ASUS VP228-4 (NVIDIA High Definition Audio)";

    public Form1()
    {
        //open port for serial msg and start timmer
        InitializeComponent();
        serialPort1.Open();
        timer1.Enabled = true;
    }

    private void serialPort1_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
    {
        //handling of the msg
        msg = serialPort1.ReadLine();
        tokens = msg.Split('%');
    }

    private void timer1_Tick(object sender, EventArgs e)
    {
        label1.Text = tokens[0];
        label2.Text = tokens[1];
        label6.Text = defaultPlaybackDevice.FullName;

        //change volume
        defaultPlaybackDevice.Volume = Int32.Parse(tokens[0]);

        //change output device
        if (tokens[1] == "ON")
        {
            if (defaultPlaybackDevice.FullName == PHONES)
            {
                //do nothing
            }
            else
            {
                //change to monitor output
            }
        }
        else
        {
            if (defaultPlaybackDevice.FullName == COLUNA)
            {
                //do nothing
            }
            else
            {
                //change to headphones
            }
        }
    }
}
}

我正在使用一个名为AudioSwitcher.AudioApi.CoreAudio的API,它应该允许我做我想做的事情,但我无法找到如何做。

首先,您需要获得所有播放设备

IEnumerable<CoreAudioDevice> devices = new CoreAudioController().GetPlaybackDevices();

更新CoreAudioController非常慢…知道为什么吗?
private void ChangeOutput(string op)
    {
        foreach (CoreAudioDevice d in devices)
        {
            if (d.FullName == op)
                d.SetAsDefault();
        }
    }