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

转载 2007年10月11日 09:02:00

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

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);
        }

  

语音识别的技术原理是什么?

简要给大家介绍一下语音怎么变文字的吧。 首先说一下作为输入的时域波形。我们知道声音实际上是一种波。常见的mp3、wmv等格式都是压缩格式,必须转成非压缩的纯波形文件,比如Windows PCM文...
  • u012426662
  • u012426662
  • 2014年11月20日 21:23
  • 1719

语音合成技术及国内外发展现状

一、语音合成技术简介   语音识别和语音合成技术是实现人机语音通信,建立一个有听和讲能力的口语系统所必需的两项关键技术。使电脑具有类似于人一样的说话和听懂人说话的能力,是90年代信息产业的重要竞...
  • hanxueyu666
  • hanxueyu666
  • 2016年08月09日 19:12
  • 2075

Android 音频播放之SoundPool的使用和封装(自定义声音资源)

例如,现在在一个五子棋游戏中,我们需要在棋子落盘的时候播放一段声音。我们可以利用SoundPool,因为它时间很短,而且需要反复播放,并且我们不希望声音占用太大资源。一般大家使用的是MediaPlay...
  • huangxiaoguo1
  • huangxiaoguo1
  • 2016年12月21日 12:28
  • 1291

Android自定义控件——仿微信语音按钮

//对应三种状态 //没有操作 public static final int NOT_VOICE = 1; //正在说话 public static final in...
  • qq_14902389
  • qq_14902389
  • 2016年09月19日 23:14
  • 1578

实现语音聊天播放自定义View

  • 2016年11月10日 14:40
  • 23.6MB
  • 下载

iOS-自定义封装好的语音播放控件ZFJVoiceBubble(swift版本的)

  • 2017年05月24日 11:45
  • 2.07MB
  • 下载

iOS-自定义封装好的语音播放控件ZFJVoiceBubble

前言自己封装了一个播放语音的控件,支持左右翻转,支持图标,支持头部状态进度条展示。这是我项目中用到的,希望对你有所帮助!调用示例1.控件在左边ZFJVoiceBubble *voiceMegBtn =...
  • u014220518
  • u014220518
  • 2017年04月06日 10:22
  • 753

实现语音聊天播放自定义View

当我们需要实现一个聊天功能的时候必然要涉及语音聊天功能,语音播放时候像QQ、微信都有语音播放的View,这里我们就开始自己来实现一个这么一个View,效果如下: 这里我们使用了两种方式实现 1...
  • qq_32955807
  • qq_32955807
  • 2016年11月10日 14:29
  • 147

自定义语音通知栏 NotificationManager

前段时间接了一个项目,是物流公司要弄一款像滴滴打车那样的app,其中有一需求是在手机弹出通知栏NotificationManager后还要实现自定义文字转语音(TTS)。因为以前没接触过TTS,所以刚...
  • sunshine_mood
  • sunshine_mood
  • 2016年06月12日 11:07
  • 929

模仿微信 自定义 语音按钮

import android.app.Dialog; import android.content.Context; import android.content.DialogInterface; i...
  • qq_33265437
  • qq_33265437
  • 2017年01月06日 15:29
  • 109
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:语音技术——自定义播放中文语音
举报原因:
原因补充:

(最多只允许输入30个字)