简体   繁体   中英

C# Windows Form App - choose output audio device

I'm trying to do some basic app, with System.Speech.Synthesis in c#

It's just a basic form with a text input and after pressing enter it reads the text using ss.SpeakAsync();

The goal is to play that sound on a exact audio output device on Windows but I can't achieve that.

  1. I've tried do it by windows settings, App volume and device preferences, but it doesn't work, not sure why.

在此处输入图像描述

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. 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'.

So maybe I'm able to set in the code the correct audio output device? 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.

//edit -> it works, when i change all my system audio to cable input, play some sound, and then go back to my main device. It seems like a windows bug?

you may use a 3rd lib Naudio; the example below shows how to return to current position in case the player was playing a song. The key code is player.DeviceNumber = deviceNum; in which deviceNum is 0, 1, 2... depending how many speakers are available in your PC. A disadvantage of Naudio is that it loads completely the song before playing, and quite slow.

        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();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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