您的位置:首页 > 其它

语音技术——自定义播放中文语音

2007-10-11 07:27 423 查看
在做称重软件过程中,由于要使用语音播报称重量的功能因此做了如下代码,希望能为初学语音技术的朋友带来帮助。(个人技术有限,欢迎大家指点)

using System;
using System.Collections.Generic;
using System.Text;
using System.Media;

namespace WeightSystem
{
public class ClsWeightMusic
{
/// <summary>
/// WAV文件路径
/// </summary>
string strMusicPath;

/// <summary>
/// 是否循环播放
/// </summary>
bool isLoop;

/// <summary>
/// 获取或设置WAV文件路径
/// </summary>
public string MusicPath
{
get
{
return this.strMusicPath;
}
set
{
this.strMusicPath = value;
}
}

/// <summary>
/// 构造函数
/// </summary>
/// <param name="musicPath">WAV文件路径</param>
public ClsWeightMusic(string musicPath, bool isLoop)
{
this.strMusicPath = musicPath;
this.isLoop = isLoop;
}

/// <summary>
/// 播放WAV文件
/// </summary>
/// <returns></returns>
public bool PlayWav()
{
SoundPlayer sndPing = new SoundPlayer(strMusicPath);

if (this.isLoop)
{
sndPing.PlayLooping();
}
else
{
sndPing.Play();
}

return true;
}
}
}

using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.Threading;
using System.Data;

namespace WeightSystem.ClassPack
{
class ClsWeightFun
{

/// <summary>
/// 构造函数
/// </summary>
public ClsWeightFun()
{

}

#region 语音控制 (播放语音文件)

/// <summary>
/// 根据参数选择读取语音文件
/// </summary>
/// <param name="sWav">语音选择参数</param>
private void wavPlayString(string sWav)
{
ClsWeightMusic clsGM;

try
{
switch (sWav)
{
case "零":
clsGM = new ClsWeightMusic(Application.StartupPath + "//Wav//零.wav", false);
clsGM.PlayWav();
Thread.Sleep(357);
break;
case "壹":
clsGM = new ClsWeightMusic(Application.StartupPath + "//Wav//壹.wav", false);
clsGM.PlayWav();
Thread.Sleep(383);
break;
case "贰":
clsGM = new ClsWeightMusic(Application.StartupPath + "//Wav//贰.wav", false);
clsGM.PlayWav();
Thread.Sleep(362);
break;
case "叁":
clsGM = new ClsWeightMusic(Application.StartupPath + "//Wav//叁.wav", false);
clsGM.PlayWav();
Thread.Sleep(445);
break;
case "肆":
clsGM = new ClsWeightMusic(Application.StartupPath + "//Wav//肆.wav", false);
clsGM.PlayWav();
Thread.Sleep(354);
break;
case "伍":
clsGM = new ClsWeightMusic(Application.StartupPath + "//Wav//伍.wav", false);
clsGM.PlayWav();
Thread.Sleep(453);
break;
case "陆":
clsGM = new ClsWeightMusic(Application.StartupPath + "//Wav//陆.wav", false);
clsGM.PlayWav();
Thread.Sleep(345);
break;
case "柒":
clsGM = new ClsWeightMusic(Application.StartupPath + "//Wav//柒.wav", false);
clsGM.PlayWav();
Thread.Sleep(458);
break;
case "捌":
clsGM = new ClsWeightMusic(Application.StartupPath + "//Wav//捌.wav", false);
clsGM.PlayWav();
Thread.Sleep(355);
break;
case "玖":
clsGM = new ClsWeightMusic(Application.StartupPath + "//Wav//玖.wav", false);
clsGM.PlayWav();
Thread.Sleep(468);
break;
case "点":
clsGM = new ClsWeightMusic(Application.StartupPath + "//Wav//点.wav", false);
clsGM.PlayWav();
Thread.Sleep(300);
break;
case "拾":
clsGM = new ClsWeightMusic(Application.StartupPath + "//Wav//拾.wav", false);
clsGM.PlayWav();
Thread.Sleep(382);
break;
case "佰":
clsGM = new ClsWeightMusic(Application.StartupPath + "//Wav//佰.wav", false);
clsGM.PlayWav();
Thread.Sleep(343);
break;
case "仟":
clsGM = new ClsWeightMusic(Application.StartupPath + "//Wav//仟.wav", false);
clsGM.PlayWav();
Thread.Sleep(446);
break;
case "万":
clsGM = new ClsWeightMusic(Application.StartupPath + "//Wav//万.wav", false);
clsGM.PlayWav();
Thread.Sleep(337);
break;
case "公斤":
clsGM = new ClsWeightMusic(Application.StartupPath + "//Wav//公斤.wav", false);
clsGM.PlayWav();
break;
case "吨":
clsGM = new ClsWeightMusic(Application.StartupPath + "//Wav//吨.wav", false);
clsGM.PlayWav();
break;
case "皮重":
clsGM = new ClsWeightMusic(Application.StartupPath + "//Wav//皮重2.wav", false);
clsGM.PlayWav();
Thread.Sleep(797);
break;
case "毛重":
clsGM = new ClsWeightMusic(Application.StartupPath + "//Wav//毛重.wav", false);
clsGM.PlayWav();
Thread.Sleep(916);
break;
case "净重":
clsGM = new ClsWeightMusic(Application.StartupPath + "//Wav//净重.wav", false);
clsGM.PlayWav();
Thread.Sleep(674);
break;
case "扣杂":
clsGM = new ClsWeightMusic(Application.StartupPath + "//Wav//扣杂.wav", false);
clsGM.PlayWav();
Thread.Sleep(626);
break;
case "警报":
clsGM = new ClsWeightMusic(Application.StartupPath + "//Wav//警报.wav", false);
clsGM.PlayWav();
Thread.Sleep(817);
break;
case "ding":
clsGM = new ClsWeightMusic(Application.StartupPath + "//Wav//ding.wav", false);
clsGM.PlayWav();
Thread.Sleep(936);
break;
case "beep":
clsGM = new ClsWeightMusic(Application.StartupPath + "//Wav//beep.wav", false);
clsGM.PlayWav();
Thread.Sleep(237);
break;
case "Beep1":
clsGM = new ClsWeightMusic(Application.StartupPath + "//Wav//Beep1.wav", false);
clsGM.PlayWav();
Thread.Sleep(575);
break;
case "Beep2":
clsGM = new ClsWeightMusic(Application.StartupPath + "//Wav//Beep2.wav", false);
clsGM.PlayWav();
Thread.Sleep(255);
break;
case "Beep3":
clsGM = new ClsWeightMusic(Application.StartupPath + "//Wav//Beep3.wav", false);
clsGM.PlayWav();
Thread.Sleep(229);
break;
}
}
catch
{
MessageBox.Show("语音播放文件出错,请检查安装是否正确!", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
finally
{
clsGM = null;
}
}

/// <summary>
/// 语音读取电子秤数值
/// </summary>
/// <param name="dWeight">电子秤数值</param>
/// <param name="sWant">皮重,毛重,净重,扣杂</param>
/// <param name="isunit">true:公斤 false:吨</param>
public void wavSpeakWeight(decimal dWeight, string sWant, bool isunit)
{
string sWeight = "";
char[] cArrWight;

try
{
sWeight = moneytoUpper.ToUpper(dWeight); //转换成大写

if (sWant == "扣杂")
{
sWeight = moneytoUpper.ToUpper2(dWeight); //转换成大写 带小数位
}

cArrWight = sWeight.ToCharArray();

wavPlayString(sWant);
for (int i = 0; i < cArrWight.Length; i++)
{
wavPlayString(cArrWight[i].ToString());
}

if (isunit)
{
wavPlayString("公斤");
}
else
{
wavPlayString("吨");
}
}
catch { }
}

#endregion

}
}

使用(线程):

private Thread threadWav; //语音线程

private void bLoad01_Click(object sender, EventArgs e)
{

threadWav = new Thread(new ThreadStart(this.pthread1));
threadWav.Priority = ThreadPriority.BelowNormal;
threadWav.Start();

}

private void pthread1()
{
this.weightFun.wavSpeakWeight(decimal.Parse(this.txtGrossWeight.Text), "毛重", true);
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: