How to convert text string to speech sound

后端 未结 6 669
無奈伤痛
無奈伤痛 2020-12-07 14:12

I am looking for a way to convert text(string) in ENG to speech(sound) in c#. do anyone know for a way or some open-source lib that can help me with this task?

6条回答
  •  自闭症患者
    2020-12-07 14:46

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows.Forms;
    using System.Speech.Synthesis; // first import this package
    
        namespace textToSpeech
        {
            public partial class home : Form
            {
                public string s = "pran"; // storing string (pran) to s
    
                private void home_Load(object sender, EventArgs e)
                    {
                        speech(s); // calling the function with a string argument
                    }
    
                private void speech(string args) // defining the function which will accept a string parameter
                    {
                        SpeechSynthesizer synthesizer = new SpeechSynthesizer();
                        synthesizer.SelectVoiceByHints(VoiceGender.Male , VoiceAge.Adult); // to change VoiceGender and VoiceAge check out those links below
                        synthesizer.Volume = 100;  // (0 - 100)
                        synthesizer.Rate = 0;     // (-10 - 10)
                        // Synchronous
                        synthesizer.Speak("Now I'm speaking, no other function'll work");
                        // Asynchronous
                        synthesizer.SpeakAsync("Welcome" + args); // here args = pran
                    }       
             }
        }
    
    • It'll be better choice to use "SpeakAsync" because when "Speak" function is executing/running none of other function will work until it finishes it's work (personally recommended)

    Change VoiceGender
    Change VoiceAge

提交回复
热议问题