C#實(shí)現(xiàn)文本轉(zhuǎn)語(yǔ)音功能
由于最近的工作需要用到文本轉(zhuǎn)語(yǔ)音的功能,在網(wǎng)上找到的資料有些不完整,特此記錄下整個(gè)完整功能。
這種方式的優(yōu)點(diǎn)在于不會(huì)被瀏覽器限制,在js的文本轉(zhuǎn)語(yǔ)音功能中,谷歌高版本的瀏覽器會(huì)阻止通過(guò)模擬點(diǎn)擊的自動(dòng)播放,而ie不會(huì)阻止.
一.確認(rèn)研發(fā)環(huán)境
操作系統(tǒng):win10或win7(我自己用的是win10 據(jù)說(shuō)有些閹割版的win7會(huì)報(bào)錯(cuò))
IDE:VS2012 (可高于此版本)
.NET framework 4.0(可高于此版本)
二.系統(tǒng)自帶語(yǔ)音識(shí)別功能
1.C:\Windows文件夾下有Speech
2.控制面板有語(yǔ)音識(shí)別

三.DLL引用
1.選中要使用該功能的程序右鍵選擇"添加引用"
2.選中"程序集"--"框架"下的System.Speech

四.代碼
需要注意的是:
1.頁(yè)面需要設(shè)置為異步

2.通過(guò)委托代理的方式調(diào)用,防止頁(yè)面無(wú)響應(yīng)
3.頁(yè)面代碼如下:
aspx:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="yy.aspx.cs" Inherits="yy" Async="true" %> ? <!DOCTYPE html> ? <html xmlns="http://www.w3.org/1999/xhtml"> <head id="Head1" runat="server"> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/> ? ? <title>文字轉(zhuǎn)語(yǔ)音測(cè)試</title> </head> <body> ? ? <form id="form1" runat="server"> ? ? <div> ? ? <asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" /> ? ? </div> ? ? ? ?? ? ? </form> </body> </html>
aspx.cs:
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Speech.Synthesis;
using System.Threading.Tasks;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
?
public partial class yy : System.Web.UI.Page
{
? ? protected void Page_Load(object sender, EventArgs e)
? ? {
?
? ? }
? ? /// <summary>
? ? /// 文字轉(zhuǎn)語(yǔ)音
? ? /// </summary>
? ? /// <param name="content">語(yǔ)音內(nèi)容</param>
? ? delegate void MyDelegate(string content);
? ? string content = "有新的訂單,請(qǐng)及時(shí)處理";
? ? SpeechSynthesizer synthesizer = new SpeechSynthesizer(); //點(diǎn)擊開(kāi)始按鈕?
?
? ? //開(kāi)始朗讀
? ? private void speakParagh(string text)
? ? {
? ? ? ? synthesizer.Speak(text);
? ? }
?
? ? //朗讀結(jié)束后釋放資源?
? ? private void Completed(IAsyncResult result)
? ? {
? ? ? ? synthesizer.SpeakAsyncCancelAll();
? ? }
?
? ? protected void Button1_Click(object sender, EventArgs e)
? ? {
? ? ? ? try
? ? ? ? {
? ? ? ? ? ? MyDelegate myDelegate = new MyDelegate(speakParagh); //異步調(diào)用委托?
? ? ? ? ? ? myDelegate.BeginInvoke(content, new AsyncCallback(Completed), null); //在啟動(dòng)異步線程后,主線程可以繼續(xù)工作而不需要等待
? ? ? ? }
? ? ? ? catch (Exception ex)
? ? ? ? {
? ? ? ? ? ? Console.WriteLine("報(bào)錯(cuò):" + ex.Message);
? ? ? ? }
? ? }
?
?
?
}以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- C#使用DirectX.DirectSound播放語(yǔ)音
- C#實(shí)現(xiàn)語(yǔ)音播報(bào)功能
- C# windows語(yǔ)音識(shí)別與朗讀實(shí)例
- c# 實(shí)現(xiàn)語(yǔ)音聊天的實(shí)戰(zhàn)示例
- c# 實(shí)現(xiàn)語(yǔ)音合成
- c# 開(kāi)發(fā)語(yǔ)音識(shí)別程序
- C#基于UDP實(shí)現(xiàn)的P2P語(yǔ)音聊天工具
- C#實(shí)現(xiàn)語(yǔ)音視頻錄制-附demo源碼
- C#中調(diào)用SAPI實(shí)現(xiàn)語(yǔ)音合成的2種方法
- C#實(shí)現(xiàn)文字轉(zhuǎn)語(yǔ)音功能
相關(guān)文章
Unity中的靜態(tài)批處理和動(dòng)態(tài)批處理操作
這篇文章主要介紹了Unity中的靜態(tài)批處理和動(dòng)態(tài)批處理操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2021-04-04
C#中ManualResetEvent實(shí)現(xiàn)線程的暫停與恢復(fù)
本文主要介紹了C#中ManualResetEvent實(shí)現(xiàn)線程的暫停與恢復(fù),文中通過(guò)示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-01-01
C#實(shí)現(xiàn)打開(kāi)畫(huà)圖的同時(shí)載入圖片、最大化顯示畫(huà)圖窗體的方法
這篇文章主要介紹了C#實(shí)現(xiàn)打開(kāi)畫(huà)圖的同時(shí)載入圖片、最大化顯示畫(huà)圖窗體的方法,涉及C#針對(duì)窗體及圖片操作的相關(guān)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-08-08
適用于WebForm Mvc的Pager分頁(yè)組件C#實(shí)現(xiàn)
這篇文章主要為大家分享了適用于WebForm Mvc的Pager分頁(yè)組件,由C#實(shí)現(xiàn),感興趣的小伙伴們可以參考一下2016-04-04
c# 如何實(shí)現(xiàn)不同進(jìn)程之間的通信
這篇文章主要介紹了c# 如何實(shí)現(xiàn)不同進(jìn)程之間的通信,幫助大家更好的理解和學(xué)習(xí)c#,感興趣的朋友可以了解下2020-11-11

