C#记事本语音实现

语音播报

在Form窗体中实现语音播报

以下为实现类

using System;
using System.Collections.Generic;
using System.IO;
using System.Media;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.DirectX;
using Microsoft.DirectX.DirectSound;
using System.Runtime.InteropServices;
namespace NetProgramTextEdit
{
    public class TtsUtil
    {
        private const string TTS_TMP_PATH = "voice_tmp.mp3";
        // 设置APPID/AK/SK
        private string APP_ID = "你的ID";
        private string API_KEY = "你的KEY";
        private string SECRET_KEY = "你的密钥";
        private Baidu.Aip.Speech.Tts client;
        public TtsUtil()
        {
            client = new Baidu.Aip.Speech.Tts(API_KEY, SECRET_KEY);
            client.Timeout = 60000;  // 修改超时时间
        }
        public static uint SND_ASYNC = 0x0001;
        public static uint SND_FILENAME = 0x00020000;
        [DllImport("winmm.dll")]
        public static extern uint mciSendString(string lpstrCommand, string lpstrReturnString, uint uReturnLength, uint hWndCallback);
        public void TtsPlay(string text)
        {
            // 可选参数
            var option = new Dictionary<string, object>(){
                {"spd", 5}, // 语速
                {"vol", 7}, // 音量
                {"per", 4}  // 发音人,4:情感度丫丫童声
             };
            var result = client.Synthesis(text, option);
            if (result.ErrorCode == 0)  // 或 result.Success
            {
                File.WriteAllBytes(TTS_TMP_PATH, result.Data);
                try
                {
                    mciSendString(@"close temp_music", " ", 0, 0);
                    mciSendString(@"open " + TTS_TMP_PATH + " alias temp_music", " ", 0, 0);
                    mciSendString(@"play temp_music", " ", 0, 0);
                }
                catch
                { }
                finally
                {
                    mciSendString("close media", "", 0, 0);
                }
                //Audio audio = new Audio(TTS_TMP_PATH);
                //audio.Play();
            }
        }
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值