Using System.Speech to convert mp3 file to text

前端 未结 2 1790
别那么骄傲
别那么骄傲 2021-01-31 12:24

I\'m trying to use the speech recognition in .net to recognize the speech of a podcast in an mp3 file and get the result as string. All the examples I\'ve seen are related to us

2条回答
  •  失恋的感觉
    2021-01-31 13:11

    Try reading it in a loop.

    SpeechRecognitionEngine sre = new SpeechRecognitionEngine();
    Grammar gr = new DictationGrammar();
    sre.LoadGrammar(gr);
    sre.SetInputToWaveFile("C:\\Users\\Soham Dasgupta\\Downloads\\Podcasts\\Engadget_Podcast_353.wav");
    sre.BabbleTimeout = new TimeSpan(Int32.MaxValue);
    sre.InitialSilenceTimeout = new TimeSpan(Int32.MaxValue);
    sre.EndSilenceTimeout = new TimeSpan(100000000);
    sre.EndSilenceTimeoutAmbiguous = new TimeSpan(100000000); 
    
    StringBuilder sb = new StringBuilder();
    while (true)
    {
        try
        {
            var recText = sre.Recognize();
            if (recText == null)
            {               
                break;
            }
    
            sb.Append(recText.Text);
        }
        catch (Exception ex)
        {   
            //handle exception      
            //...
    
            break;
        }
    }
    return sb.ToString();
    

    If you've a Windows Forms or WPF application, run this code in a seperate thread, otherwise it blocks the UI thread.

提交回复
热议问题