NET 语言识别,语音控制操作、语音播报
System.Speech.
》》System.Speech.Synthesis; 语音播报
》》System.Speech.Recognition 语音识别
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Speech.Recognition;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Speech;
using System.Speech.Synthesis;
namespace WindowsFormsApp1
{
public partial class Form2 : Form
{
SpeechRecognitionEngine recognitionEngine;
public Form2()
{
recognitionEngine = new SpeechRecognitionEngine();
Choices choices = new Choices();
choices.Add(new string[] { "开始", "Start", "Go", "停止", "Stop", "Over" });
GrammarBuilder gb = new GrammarBuilder(choices);
Grammar grm = new Grammar(gb);
recognitionEngine.LoadGrammarAsync(grm);
//音频输入
recognitionEngine.SetInputToDefaultAudioDevice();
recognitionEngine.SpeechRecognized += RecognitionEngine_SpeechRecognized;
InitializeComponent();
}
private void RecognitionEngine_SpeechRecognized(object sender, SpeechRecognizedEventArgs e)
{
string info = e.Result.Text;
switch (info)
{
case "开始":
case "Start":
case "Go":
richTextBox1.Text += info;
break;
case "停止":
case "Stop":
case "Over":
richTextBox1.Text += info;
break;
}
}
private void Form2_Load(object sender, EventArgs e)
{
this.btn_StopSpeek.Enabled = false;
}
private void btn_startSpeek_Click(object sender, EventArgs e)
{
this.btn_StopSpeek.Enabled = true;
recognitionEngine.RecognizeAsync(RecognizeMode.Multiple);
this.btn_startSpeek.Enabled = false;
}
private void btn_StopSpeek_Click(object sender, EventArgs e)
{
this.btn_StopSpeek.Enabled = false;
recognitionEngine.RecognizeAsyncStop();
this.btn_startSpeek.Enabled = true;
}
private void button1_Click(object sender, EventArgs e)
{
SpeechSynthesizer sp = new SpeechSynthesizer();
PromptBuilder pb = new PromptBuilder();
pb.AppendText("123");
sp.Speak(pb);
}
}
}
语言播报
SpeechSynthesizer sp = new SpeechSynthesizer();
sp.Rate = 1;//语速 -10 到 10 之间
sp.Volume = 50;//音量 (0 到 100)
PromptBuilder pb = new PromptBuilder();
pb.AppendText("123");
sp.Speak(pb);
获取语言包、异步播报、暂停、停止、继续语言播报、保存音频
》》异步播报,就是不阻塞其它操作
SpeechSynthesizer sp = new SpeechSynthesizer();
sp.Rate = 1;//语速 -10 到 10 之间
sp.Volume = 50;//音量 (0 到 100)
PromptBuilder pb = new PromptBuilder();
pb.AppendText("1234564878564135415648145");
//同步播报
//sp.Speak(pb);
//异步播报
sp.SpeakAsync(pb);
》》获取语言包
SpeechSynthesizer sp = new SpeechSynthesizer();
foreach (var item in sp.GetInstalledVoices())
{
this.comboBox1.Items.Add(item.VoiceInfo.Name);
}
》》 异步播报 暂停、继续
if (sp.State == SynthesizerState.Speaking)
{
// 正在播报 暂停
sp.Pause();
}
else if (sp.State == SynthesizerState.Paused)
{
// 已经 暂停,继续播放
sp.Resume();
}
》》停止
if (sp.State == SynthesizerState.Speaking)
{
//取消所有排队、异步、语音合成操作。
sp.SpeakAsyncCancelAll();
}
》》保存音频
//使用using才能在结束后自动保存语音文件
using (SpeechSynthesizer sp = new SpeechSynthesizer())
{
string path = @"D:\\zenvideo\";
sp.SetOutputToWaveFile(path + "1.wav");
//这句不会播报的,会把声音生成到1.wav
sp.Speak("13213213213213");
}
免责声明:我们致力于保护作者版权,注重分享,被刊用文章因无法核实真实出处,未能及时与作者取得联系,或有版权异议的,请联系管理员,我们会立即处理! 部分文章是来自自研大数据AI进行生成,内容摘自(百度百科,百度知道,头条百科,中国民法典,刑法,牛津词典,新华词典,汉语词典,国家院校,科普平台)等数据,内容仅供学习参考,不准确地方联系删除处理! 图片声明:本站部分配图来自人工智能系统AI生成,觅知网授权图片,PxHere摄影无版权图库和百度,360,搜狗等多加搜索引擎自动关键词搜索配图,如有侵权的图片,请第一时间联系我们,邮箱:ciyunidc@ciyunshuju.com。本站只作为美观性配图使用,无任何非法侵犯第三方意图,一切解释权归图片著作权方,本站不承担任何责任。如有恶意碰瓷者,必当奉陪到底严惩不贷!

