C# Windows Form App - choose output audio device

0

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.

enter image description here

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?

c#
windows
audio
text-to-speech
asked on Stack Overflow May 10, 2020 by alanmcknee • edited May 10, 2020 by alanmcknee

1 Answer

0

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();
answered on Stack Overflow Apr 2, 2021 by nphaibk

User contributions licensed under CC BY-SA 3.0