//首先要引用一个类库SpeechLib.dll

using System;
using System.Collections.Generic;
using System.Windows.Forms;
using SpeechLib;
namespace TestSpeaker1
{
  static class Program
  {
    /// <summary>
    /// 应用程序的主入口点。
    /// </summary>
    [STAThread]
    static void Main()
    {
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);
        Application.Run(new Form1());
    }
  }
  public class Speach
  {
    private static Speach _Instance = null;
    private SpeechLib.SpVoiceClass voice = null;
    private Speach()
    {
        BuildSpeach();
    }
    public static Speach instance()
    {
        if (_Instance == null)
          _Instance = new Speach();
        return _Instance;
    }
    private void SetChinaVoice()
    {
        voice.Voice = voice.GetVoices(string.Empty, string.Empty).Item(3);
    }
    private void SetEnglishVoice()
    {
        voice.Voice = voice.GetVoices(string.Empty, string.Empty).Item(1);
    }
    private void SpeakChina(string strSpeak)
    {
        SetChinaVoice();
        Speak(strSpeak);
    }
    private void SpeakEnglishi(string strSpeak)

{
        SetEnglishVoice();
        Speak(strSpeak);
    }

public void AnalyseSpeak(string strSpeak)
    {
        int iCbeg = 0;
        int iEbeg = 0;
        bool IsChina = true;
        for (int i = 0; i < strSpeak.Length; i++)
        {
          char chr = strSpeak;
          if (IsChina)
          {
            if (chr <= 122 && chr >= 65)
            {
                int iLen = i - iCbeg;
                string strValue = strSpeak.Substring(iCbeg, iLen);
                SpeakChina(strValue);
                iEbeg = i;
                IsChina = false;
            }
          }
          else
          {
            if (chr > 122 || chr < 65)
            {
                int iLen = i - iEbeg;
                string strValue = strSpeak.Substring(iEbeg, iLen);

this.SpeakEnglishi(strValue);
                iCbeg = i;
                IsChina = true;
            }
          }

}//end for
        if (IsChina)
        {
          int iLen = strSpeak.Length - iCbeg;
          string strValue = strSpeak.Substring(iCbeg, iLen);
          SpeakChina(strValue);
        }
        else
        {
          int iLen = strSpeak.Length - iEbeg;
          string strValue = strSpeak.Substring(iEbeg, iLen);
          SpeakEnglishi(strValue);
        }

}
    private void BuildSpeach()
    {
        if (voice == null)
          voice = new SpVoiceClass();
    }
    public int Volume
    {
        get
        {
          return voice.Volume;
        }
        set
        {
          voice.SetVolume((ushort)(value));
        }
    }
    public int Rate

{
        get
        {
          return voice.Rate;
        }
        set
        {
          voice.SetRate(value);
        }
    }
    private void Speak(string strSpeack)
    {
        try
        {
          voice.Speak(strSpeack, SpeechVoiceSpeakFlags.SVSFlagsAsync);
        }
        catch (Exception err)
        {
          throw (new Exception("发生一个错误:" + err.Message));
        }
    }

public void Stop()
    {
        voice.Speak(string.Empty, SpeechLib.SpeechVoiceSpeakFlags.SVSFPurgeBeforeSpeak);
    }
    public void Pause()
    {
        voice.Pause();
    }
    public void Continue()
    {
        voice.Resume();
    }

}//end class

}

//Form1.cs

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DotNetSpeech;
namespace TestSpeaker1
{
  public partial class Form1 : Form
  {
    public Form1()
    {
        InitializeComponent();
    }
    private void Form1_Load(object sender, EventArgs e)
    {

}
    private void button1_Click(object sender, EventArgs e)
    {
        Speach sp = Speach.instance();
        sp.Volume = 100;
        sp.Rate = 1;
        sp.AnalyseSpeak(this.SpeakerText.Text.Trim());
        //try
        //{
        //   SpeechVoiceSpeakFlags SpFlags = SpeechVoiceSpeakFlags.SVSFlagsAsync;
        //   SpVoice Voice = new SpVoice();
        //   ///3表示是汉用,0124都表示英语,就是口音不同
        //   Voice.Voice = Voice.GetVoices(string.Empty, string.Empty).Item(0);
        //   //voice.Voice =voice.GetVoices(string.Empty, string.Empty).Item(0);
        //   Voice.Speak(this.textBox1.Text, SpFlags);
        //}
        //catch (Exception er)
        //{
        //   MessageBox.Show("An Error Occured!", "SpeechApp", MessageBoxButtons.OK, MessageBoxIcon.Error);
        //}
    }
    private void button2_Click(object sender, EventArgs e)
    {
        try
        {
          SpeechVoiceSpeakFlags SpFlags = SpeechVoiceSpeakFlags.SVSFlagsAsync;
          SpVoice Voice = new SpVoice();
          SaveFileDialog sfd = new SaveFileDialog();
          sfd.Filter = "All files (*.*)|*.*|wav files (*.wav)|*.wav";

sfd.Title = "Save to a wave file";
          sfd.FilterIndex = 2;
          sfd.RestoreDirectory = true;
          if (sfd.ShowDialog() == DialogResult.OK)
          {
            SpeechStreamFileMode SpFileMode = SpeechStreamFileMode.SSFMCreateForWrite;
            SpFileStream SpFileStream = new SpFileStream();
            SpFileStream.Open(sfd.FileName, SpFileMode, false);
            Voice.AudioOutputStream = SpFileStream;
            Voice.Speak(this.SpeakerText.Text, SpFlags);
            Voice.WaitUntilDone(100);
            SpFileStream.Close();
          }
        }
        catch (Exception er)
        {
          MessageBox.Show("An Error Occured!", "SpeechApp", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }
    }
  }
}

C#中英文混合朗读文本代码相关推荐

  1. TTS中英文混合朗读的完全设计实现

    中英文混合朗读一直是个难点,即在一段文本中要将中文和英文分离出来进行分别朗读,又不能打乱朗读的次序,所以我们设计如下的两种方法,每种方法都有各自的优点和缺点. ①采用同步朗读方式进行混合朗读: 将朗读 ...

  2. PaddleOCR加载chinese_ocr_db_crnn_server/chinese_ocr_db_crnn_modile模型进行中英文混合预测(代码)实践

    1. 环境搭建 参考:<PaddleOCR加载chinese_ocr_db_crnn_server模型进行中英文混合预测(命令行)实践> 2. 代码 import paddlehub as ...

  3. 【代码笔记】iOS-判断中英文混合的字符长度的两种方法

    一,代码. - (void)viewDidLoad {[super viewDidLoad];// Do any additional setup after loading the view.//第 ...

  4. BertTokenizer在处理中英文混合序列中遇到的问题

    直接上代码: from transformers import BertTokenizerpath = '../PTM/bert-base-zh'tokenizer = BertTokenizer.f ...

  5. Lua截取utf-8编码的中英文混合字符串

    参考博客:UTF8字符串在lua的截取和字数统计[转载] 需求 按字面个数来截取子字符串 函数(字符串, 开始位置, 截取长度)utf8sub("你好1世界哈哈",2,5) = 好 ...

  6. C#语音朗读文本 — TTS的实现

    TTS, Text To Speech的缩写,是使用语音朗读文本的技术.目前,在国内应用较多的是排队叫号系统 Windows 平台的TTS,通常使用的是微软自带的 Speech API. Window ...

  7. php截取等长UFT8中英文混合字串

    由于需要,想实现"php截取等长UFT8中英文混合字串",可是网上找了很多代码不是有乱码就是不能实现等长(以一个中文长度为单位,两个英文字母算一个长度,如'等长'长度为2,'UTF ...

  8. 我用纯C语言开发的中英文混合分词服务器3.0正式发布,词库190多万词,每秒切分5万+,同时提供 c、java、C#、delphi、js调用范例

    我用纯C语言开发的中英文混合分词服务器3.0正式发布,词库190多万词,每秒切分5万+,同时提供 c.java.C#.delphi.js调用范例 百万商业圈中英文混合分词服务器3.0正式发布, 绝对稳 ...

  9. 中英文混合拼音排序(从首字母开始,相同则往下比较)

    中英文混合拼音排序(从首字母开始,相同则往下比较) 中文/英文混合数据 进行转拼音(中文),按照26个字母顺序排序,从首字母开始比较,相同则比较第二个字母,如此类推,直到结束排序为止. pom.xml ...

最新文章

  1. php 工资 2018,2018年我国公务员级别工资标准
  2. 5-510寝室课后习题4.35
  3. M2: XAML Controls(2)
  4. POJ2446【建图建图】
  5. php 对象转换成数组,PHP把对象转换为数组的问题
  6. 解决mysql ERROR 1045(28000)问题【忘记密码,修改配置文件,无密码登录mysql修改密码】
  7. 在线教学视频的设计与实现
  8. Clojure学习02:语法
  9. Win10系统下安装ubuntu系统
  10. PHP截取文件,[转载]php做截取文件后缀名大全
  11. T9社区注册流程记录(笔记)
  12. Tensorflow2.0 之 SSD 网络结构
  13. 高中计算机会考vb试题,高中信息技术考试vb程序题及答案
  14. 安卓镜像刻录软件_手机iso刻录工具去广告版下载-安卓手机版iso刻录工具无广告版(iso写盘工具)v3.4 2020最新版_新绿资源网...
  15. C++核心编程笔记整理
  16. 化工原理 --- 流体流体 --- 习题课及复习
  17. 以太坊编程涉及那些开发语言?
  18. 中文情感极性词典 NTUSD
  19. nape.geom.MarchingSquares
  20. 国企区块链应用进展年内已经突破50起 信任传递或引发区块链遍地开花

热门文章

  1. https协议 的工作过程
  2. 计算机考神经科学,科学网—神经科学杂谈6:电脑和人脑的最大不同--计算和记忆 - 吴景鹏的博文...
  3. 高端机市场卡位战:iQOO能否博得一席之地?
  4. Python:阿基米德棋盘放米问题
  5. 重拾编程之路--jeetcode(java)--Range Sum Query - Immutable
  6. java 实现违章_基于JAVA的违章查询助手数据调用代码实例详解
  7. mysql基础(2)mysql基础知识
  8. 一台服务器最大能支持多少条TCP连接
  9. python货币兑换_零基础python作业--货币兑换的服务系统
  10. 【luogu2026】【数学】求一次函数解析式