C# 语音 调用NEOSPEECH问题
萧汵 2016-05-12 12:03:17 using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;//线程
using System.IO;
using System.Speech.Synthesis;
using System.Speech;
namespace YPC_TTS
{
public partial class YPC_TTS : Form
{
public YPC_TTS()
{
InitializeComponent();
WatcherStrat();
}
string[] splite1;
string s1, s2, s3;
char[] c1;
char[] c2;
string s_all1;
string s_all2;
string s_all;
Thread th;//定义线程
FileSystemWatcher watcher = new FileSystemWatcher();//文本监视
public SpeechSynthesizer synth; //语音合成对象
void WatcherStrat()
{
watcher.Path = @"D:\test";
watcher.Filter = "*.txt";
watcher.IncludeSubdirectories = true;//允许侦测此目录下的子目录
watcher.NotifyFilter = NotifyFilters.LastWrite;
watcher.Changed += new FileSystemEventHandler(OnChanged);
//watcher.Created += new FileSystemEventHandler(OnCreated);
//watcher.Renamed += new RenamedEventHandler(OnRenamed);
watcher.EnableRaisingEvents = true;//开启提交事件
}
void OnChanged(object source, FileSystemEventArgs e)
{
Thread.Sleep(330);
watcher.EnableRaisingEvents = false;
th = new Thread(calculate);
th.IsBackground = true;
th.Start();
Control.CheckForIllegalCrossThreadCalls = false;
watcher.EnableRaisingEvents = true;
}
private void calculate( )
{
StreamReader objReader = new StreamReader(@"D:\test\test.txt");
synth = new SpeechSynthesizer();
synth.Volume = 100;
synth.Rate = 0;
synth.SelectVoice("VW Lily");
string strLine = objReader.ReadLine();
if (strLine != " ")
{
splite1 = strLine.Split(',');
s1 = splite1[0];
s2 = splite1[1];
s3 = "钢卷厚度 " + splite1[2];
c1 = s1.ToCharArray();//转成字符数组
s_all1 = "钢卷号 " + c1[0] + "`" + c1[1] + "`" + c1[2] + "`" + c1[3] + "`" + c1[4] + "`" + c1[5] + "`" + c1[6] + "`" + c1[7] + "`" + c1[8] + ",";
c2 = s2.ToCharArray();
s_all2 = "钢卷宽度 " + c2[0] + "`" + c2[1] + "`" + c2[2] + "`" + c2[3] + ",";
s_all = "入口准备焊接 " + ","+s_all1 + s_all2 + s3;
objReader.Close();
objReader.Dispose();
synth.Speak(s_all);
synth.Dispose(); //这个不写第二次就没有声音,现在加了这句,但是发现长时间后又没有声音了 Thread.Sleep(1100);
}
}
}
}
求大神指点