我想使用Microsoft.Speech

时间:2019-01-09 05:58:12

标签: c# speech-to-text windows-api-code-pack

目前,我尝试使用 System.Speech和Microsoft.Speech

我只能对Microsoft.Speech进行操作,因为如果默认语言不是英语,则System.Speech无法正常工作。

首先,我尝试使用此代码从语音中获取文本。

public void Recognize()
    {
        using ( SpeechSynthesizer Synthesizer = new SpeechSynthesizer() )
        {
            // Text To Speech
            Synthesizer.SetOutputToDefaultAudioDevice();
            //Synthesizer.Speak( "Test" );

            using ( SpeechRecognitionEngine recognizer = new SpeechRecognitionEngine() )
            {
                recognizer.SpeechRecognized += new EventHandler<SpeechRecognizedEventArgs>( OnSpeechRecognized );
                recognizer.LoadGrammarCompleted += new EventHandler<LoadGrammarCompletedEventArgs>( OnLoadGrammarCompleted );
                recognizer.SetInputToDefaultAudioDevice();

                recognizer.LoadGrammar( new Grammar( "Resources/Grammar.xml" )
                {
                    Enabled = true
                } );

                Console.WriteLine( "Recognize()" );

                RecognitionResult result = recognizer.Recognize( TimeSpan.FromSeconds( 3 ) );
                if ( result != null )
                {
                    Console.WriteLine( "Result => " + result.Text );
                }
                else
                {
                    Console.WriteLine( "Text is not on list" );
                }
            }
        }
    }

我注意到,如果我讲话中的句子(或单词)没有包含在“语法项”中的“列表”中,那么它将不会应用于结果。

所以我搜索了这个问题,终于找到了这段代码。

public Grammar GrammarWithDictation()
    {
        GrammarBuilder builder = new GrammarBuilder();
        builder.Append( "begin" );
        builder.AppendDictation( "spelling" );
        builder.Append( "end" );
        Grammar grammarWithDictation = new Grammar( builder );
        grammarWithDictation.Name = "Grammar with Dictation";

        return grammarWithDictation;
    }

所以我将其应用于我的代码。

public void Recognize()
    {
        using ( SpeechSynthesizer Synthesizer = new SpeechSynthesizer() )
        {
            // Text To Speech 
            Synthesizer.SetOutputToDefaultAudioDevice();
            //Synthesizer.Speak( "Test" );

            using ( SpeechRecognitionEngine recognizer = new SpeechRecognitionEngine() )
            {
                recognizer.SpeechRecognized += new EventHandler<SpeechRecognizedEventArgs>( OnSpeechRecognized );
                recognizer.LoadGrammarCompleted += new EventHandler<LoadGrammarCompletedEventArgs>( OnLoadGrammarCompleted );
                recognizer.SetInputToDefaultAudioDevice();

                //recognizer.LoadGrammar( new Grammar( "Resources/Grammar.xml" )
                //{
                //    Enabled = true
                //} );

                recognizer.LoadGrammarAsync( GrammarWithDictation() );

                Console.WriteLine( "Recognize()" );

                RecognitionResult result = recognizer.Recognize( TimeSpan.FromSeconds( 3 ) );
                if ( result != null )
                {
                    Console.WriteLine( "Result => " + result.Text );
                }
                else
                {
                    Console.WriteLine( "No Results." );
                }
            }
        }
    }

它不起作用。

所以这是这篇长文章的重点。

在与Microsoft.Speech的演讲中如何获得整句话?

有可能吗?还是我需要使用其他东西来做到这一点?

0 个答案:

没有答案