简体   繁体   中英

Making a SAPI DLL in C#, I have a function that starts the voice, but I need a function to check the STATE. How do I do that?

So I am writing a DLL that allows a program to use Text To Speech through SAPI. Here is what I have.

public static double TestExport(string vSay)
        {

            SpeechSynthesizer synth = new SpeechSynthesizer();

           

                // Configure the audio output.   
                synth.SetOutputToDefaultAudioDevice();

                // Speak a string.  
                synth.SpeakAsync(vSay);


            return 1;
}

So this starts the voice talking with what is passed to it, but I have no way of knowing when the voice is done. I want to be able to create a function in the DLL that does this:

             double vState = (double)synth.State;

But as you can see, I need the class instance to be able to check the state, where if I call the DLL a second later to check in on the voice it won't work because synth isn't declared.

So given that to use the STATE command I need the instance of the class, how can I check through DLL if SAPI is still running?

So overall the goal is to make function that starts SAPI, and then a function that tells me if the voice is still going or not.

Quick points: The program I'm working with requires I use SAPI and can't use the UWP versions. As well as that, it requires I return a double.

You want to encapsulate your class (be it SpeechSynthesizer or your own class) in an IntPtr, so that your secondary program can pass it back to you, and you can then do stuff with it. As a (completely untested, and not even compiled) example:

public static IntPtr TestExport(string vSay)
{
   SpeechSynthesizer synth = new SpeechSynthesizer();
   // Configure the audio output.   
   synth.SetOutputToDefaultAudioDevice();

   // Speak a string.  
   synth.SpeakAsync(vSay);

   return IntPtr(synth);
}

public static bool AmIDoneYet(IntPtr handle)
{
    SpeechSynthesizer synth = (SpeechSynthesizer) handle.ToPointer();
    return synth.State() != SynthesizerState.Ready;
}
    

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