NulLReferenceException on creating SpeechSynthesizer / DefaultDeviceOut when program is launched from IIS

249 Views Asked by At

I have created a simple command line program to turn text into a .WAV file.

The program takes 2 arguments

  1. The path to create the .WAV File
  2. The text to turn into a .WAV

This is the error I am getting on the line where I try to set the output to the default audio device:

Object reference not set to an instance of an object. at System.Speech.Internal.ObjectTokens.SAPICategories.DefaultDeviceOut() at System.Speech.Internal.Synthesis.VoiceSynthesis..ctor(WeakReference speechSynthesizer) at System.Speech.Synthesis.SpeechSynthesizer.get_VoiceSynthesizer() at System.Speech.Synthesis.SpeechSynthesizer.SetOutputToNull() at System.Speech.Synthesis.SpeechSynthesizer.SetOutputStream(Stream stream, SpeechAudioFormatInfo formatInfo, Boolean headerInfo, Boolean closeStreamOnExit) at System.Speech.Synthesis.SpeechSynthesizer.SetOutputToDefaultAudioDevice() at TTS.Program.Main(String[] args) "

Any help on this would be greatly appreciated.

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Speech.Synthesis;
using System.Text;
using System.Threading.Tasks;

namespace TTS 
{
    class Program 
    {
        static SpeechSynthesizer synthesizer;

        static void Main(string[] args) 
        {
            //"..\\audio\\message.wav"

            using(System.IO.FileStream stream = System.IO.File.Create(args[0])) 
            {
                try 
                {
                    synthesizer = new SpeechSynthesizer();
                    Console.WriteLine("Creating WAV");
                    synthesizer.SetOutputToWaveStream(stream);
                    Console.WriteLine("Speaking Words");
                    synthesizer.Speak(args[1]);
                    Console.WriteLine("Disposing");
                    synthesizer.Dispose();
                    Console.WriteLine("Finished");
                } catch (Exception ex) 
                {
                    Console.WriteLine(ex.Message);
                    Console.WriteLine(ex.StackTrace);
                }
            }
        }
    }
}
0

There are 0 best solutions below