如何在c#中使用speechRecognition获取收听开始时间

时间:2016-05-04 16:50:26

标签: c# speech-recognition

我在C#中做的项目是从视频文件中读取并将其转换为字幕文本,但我希望得到每个文本行的开始时间,以便在合适的时间显示在视频中,我试过AudioPosition,但效果不好,有没有办法做到这一点?

using System;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Speech.Recognition;
using System.Threading.Tasks;

namespace project
{
public partial class Form1 : Form
{
    StringBuilder sb = new StringBuilder();

    public Form1()
    {
        InitializeComponent();

    }

    private void Form1_Load(object sender, EventArgs e)
    {

    }

    string[] tokens;
    private void button1_Click(object sender, EventArgs e)
    {
        openFileDialog1.Filter = "WAV|*.wav";
        if (openFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
        {           
        }

        SpeechRecognitionEngine sre = new SpeechRecognitionEngine();
        Grammar gr = new DictationGrammar();
        sre.LoadGrammar(gr);
        sre.SetInputToWaveFile(@openFileDialog1.FileName);

        while (true)
        {
            try
            {
                var recText = sre.Recognize();
                sb.Append(recText.Audio.AudioPosition + " * ");
                textBox4.Text = sb.ToString();



        tokens = sb.ToString().Split('@');
        for (int i = 0; i < tokens.Length - 1; i++)
        {
            textBox2.AppendText(tokens.Length.ToString());
            textBox2.AppendText(i+" - "+tokens[i]);
            textBox2.AppendText(Environment.NewLine);

        }

            }
            catch (Exception ex)
            {
                break;
            }
        }
    }

0 个答案:

没有答案