简体   繁体   English

C# Windows 表单应用程序 - 选择 output 音频设备

[英]C# Windows Form App - choose output audio device

I'm trying to do some basic app, with System.Speech.Synthesis in c#我正在尝试使用 c# 中的 System.Speech.Synthesis 做一些基本的应用程序

It's just a basic form with a text input and after pressing enter it reads the text using ss.SpeakAsync();它只是一个带有文本输入的基本表单,按下回车后它会使用 ss.SpeakAsync(); 读取文本。

The goal is to play that sound on a exact audio output device on Windows but I can't achieve that.目标是在 Windows 上的精确音频 output 设备上播放该声音,但我无法实现。

  1. I've tried do it by windows settings, App volume and device preferences, but it doesn't work, not sure why.我已经尝试通过 windows 设置、应用音量和设备偏好设置,但它不起作用,不知道为什么。

在此处输入图像描述

I'm setting the correct device, but it plays on the "default" anyways, no matter what I do.我正在设置正确的设备,但不管我做什么,它都以“默认”播放。 It works with any other application correctly (like browser for example) bit with mine it doesn't.它可以正确地与任何其他应用程序(例如浏览器)一起工作,而我的却不行。 It works when I change all system audio to that device, but the main goal is that i want only this app to use "cable input".当我将所有系统音频更改为该设备时,它可以工作,但主要目标是我只希望这个应用程序使用“有线输入”。

  1. I've tried to use some 3rd party software to do this ( https://github.com/a-sync/audio-router ) but it doesn't work either.我尝试使用一些第 3 方软件来执行此操作( https://github.com/a-sync/audio-router )但它也不起作用。 I set everything, and when I click "play" in my app, then my app crashes with an exception: „[17132] TrayReader.exe” exited with code -1073741819 (0xc0000005) 'Access violation'.我设置了所有内容,当我在我的应用程序中单击“播放”时,我的应用程序崩溃并出现异常:“[17132] TrayReader.exe”以代码-1073741819 (0xc0000005)“访问冲突”退出。

So maybe I'm able to set in the code the correct audio output device?所以也许我可以在代码中设置正确的音频 output 设备? Or how do I fix those previous errors?或者我该如何修复以前的错误? maybe I need to add some privileges or something?也许我需要添加一些特权或什么? I'm mainly web developer, this is my first time with windows app.我主要是 web 开发人员,这是我第一次使用 windows 应用程序。

//edit -> it works, when i change all my system audio to cable input, play some sound, and then go back to my main device. //edit -> 它可以工作,当我将所有系统音频更改为电缆输入时,播放一些声音,然后 go 回到我的主设备。 It seems like a windows bug?这似乎是一个 windows 错误?

you may use a 3rd lib Naudio;您可以使用第三个 lib Nadio; the example below shows how to return to current position in case the player was playing a song.下面的示例显示了如何在播放器正在播放歌曲的情况下返回到当前 position。 The key code is player.DeviceNumber = deviceNum;关键代码是 player.DeviceNumber = deviceNum; in which deviceNum is 0, 1, 2... depending how many speakers are available in your PC.其中 deviceNum 为 0、1、2... 取决于您的 PC 中可用的扬声器数量。 A disadvantage of Naudio is that it loads completely the song before playing, and quite slow. Naudio 的一个缺点是它会在播放前完全加载歌曲,而且速度很慢。

        var playback = player.PlaybackState;
        if (playback == PlaybackState.Stopped) // change and exit
        {
            player.DeviceNumber = deviceNum;
            return;
        }
        var currentTime = reader.CurrentTime;
        player.Stop(); // must stop to change output
        player.DeviceNumber = deviceNum;
        player.Init(reader);
        reader.CurrentTime = currentTime;
        if (playback == PlaybackState.Playing)
            player.Play();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM