gpt4 book ai didi

c# - AudioSwitcher API 改变输出设备

转载 作者:可可西里 更新时间:2023-11-01 10:02:41 26 4
gpt4 key购买 nike

我用 Arduino 制作了一个小工具,它通过串行向我的 c# 程序发送 2 个值(音量电位器值和切换输出设备的开关按钮)。音量部分已经完成,但我无法在两个输出设备(监听音频和耳机)之间切换。

我目前的代码:

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,它应该允许我做我想做的事,但我找不到方法。

最佳答案

首先你需要得到所有的PlaybackDevices

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

然后你可以做一个函数来改变你的默认播放设备,像这样的全名

private void ChangeOutput(string op)
{
foreach (CoreAudioDevice d in devices)
{
if (d.FullName == op)
d.SetAsDefault();
}
}

关于c# - AudioSwitcher API 改变输出设备,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51413442/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com