zoukankan      html  css  js  c++  java
  • C# TTS 文字转语音

    使用 System.Speech 可以很容易实现TTS。

    原代码如下:

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Speech.Synthesis;
    using System.Text;
    using System.Windows.Forms;
    
    namespace ttsTest
    {
        public partial class Form1 : Form
        {
            private SpeechSynthesizer synthes = null;
    
            public Form1()
            {
                InitializeComponent();
            }
    
            private void Form1_Load(object sender, EventArgs e)
            {
                synthes = new SpeechSynthesizer();
                trackBarVolume.Value = synthes.Volume;
                trackBarRate.Value = synthes.Rate;
    
                lblVolume.Text = "音量:" + trackBarVolume.Value;
                lblRate.Text = "语速:" + trackBarRate.Value;
    
                cmbVoice.BeginUpdate();
                cmbVoice.Items.Clear();
                foreach (var r in synthes.GetInstalledVoices())
                {
                    if (r.Enabled)
                    {
                        cmbVoice.Items.Add(r.VoiceInfo.Name);
                    }
                }
                cmbVoice.EndUpdate();
    
                cmbVoice.SelectedItem = synthes.Voice.Name;
            }
    
            private void btnPlay_Click(object sender, EventArgs e)
            {
                string str = txtContent.Text.Trim();
                if (string.IsNullOrEmpty(str))
                {
                    return;
                }
    
                synthes.SpeakAsyncCancelAll();
                synthes.SelectVoice(cmbVoice.SelectedItem.ToString());
                // synthes.SelectVoiceByHints(VoiceGender.Male, VoiceAge.Senior, 2, System.Globalization.CultureInfo.CurrentCulture);
    
                synthes.Volume = trackBarVolume.Value;
                synthes.Rate = trackBarRate.Value;//-10-10,值越小,语速越慢
    
                // synthes.Speak(txtContent.Text);//同步
                synthes.SpeakAsync(str);//异步
            }
    
            private void Form1_FormClosing(object sender, FormClosingEventArgs e)
            {
                synthes.SpeakAsyncCancelAll();
            }
    
            private void trackBarVolume_Scroll(object sender, EventArgs e)
            {
                lblVolume.Text = "音量:" + trackBarVolume.Value;
            }
    
            private void trackBarRate_Scroll(object sender, EventArgs e)
            {
                lblRate.Text = "语速:" + trackBarRate.Value;
            }
        }
    }

    欢迎转载,转载请注明:转载自[ http://www.cnblogs.com/zjfree/ ]
  • 相关阅读:
    android中的AIDL进程间通信
    [Android自定义控件] Android自定义控件
    Android控件之SlidingDrawer(滑动式抽屉)详解与实例
    Android 广播大全 Intent Action 事件
    Activity和Service绑定
    String.format()用法
    Android之Handler用法总结
    android中的AIDL进程间通信
    Android: 在 TextView 里使用删除线
    fastjson生成和解析json数据
  • 原文地址:https://www.cnblogs.com/zjfree/p/15386804.html
Copyright © 2011-2022 走看看