美文网首页
WPF_baiduYY

WPF_baiduYY

作者: 萧非子 | 来源:发表于2018-01-19 19:01 被阅读37次

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Controls.Primitives;
    using System.Windows.Data;
    using System.Windows.Documents;
    using System.Windows.Input;
    using System.Windows.Media;
    using System.Windows.Media.Imaging;
    using System.Windows.Shapes;
    using Baidu.Aip.Speech;//百度AI_百度语音
    using System.IO;

    namespace XiaoXiangGuang_project
    {
    /// <summary>
    /// BDAIX.xaml 的交互逻辑:百度AI
    /// </summary>
    public partial class BDAIX : Window
    {
    /***
    *应用名称:BDAIX
    *AppID:10313629
    APIKey:Lgk3vC7FXQfQGW76zVg4pZKO
    *SecretKey:bB8kbhl1Pnef3NG6MfG5AaYmI3SV53YC
    */
    private static string APIKEY = "Lgk3vC7FXQfQGW76zVg4pZKO";
    private static string SECRETKEY = "bB8kbhl1Pnef3NG6MfG5AaYmI3SV53YC";

        private readonly Asr _asrClient;//百度语音识别:声音识别为文本
        private readonly Tts _ttsClient;//百度语音合成:文本合成为声音
    
        public string fileName= "百度语音合成的语音.mp3";
        public BDAIX()
        {
            InitializeComponent();
    
            _ttsClient = new Tts(APIKEY, SECRETKEY);
            _asrClient = new Asr(APIKEY,SECRETKEY);
        }
    
        private void Button_Click(object sender, RoutedEventArgs e)
        {
            ButtonBase btn = e.Source as ButtonBase;
    
            switch (btn.Name)
            {
                case "btn_TTS":
                    //百度语音合成
                    BDAI_TTS(textBox_TTS.Text);
                    MP3Player._Instance().Play(fileName);
    
                    break;
                case "btn_ARS":
                    //百度语音识别
                    BDAI_ASRData();
                    break;
                default:
                    break;
            }
        }
        //百度语音合成:文本合成为声音
        public void BDAI_TTS(string str_tts)
        {
            // 可选参数
            var option = new Dictionary<string, object>
            {
                {"spd", 5}, //语速0-9,默认=5;
                {"pit", 5}, //音调0-9,默认=5;
                {"vol", 7}, // 音量0-15,默认=5;
                {"per", 4}  // 发音人选择,0为普通女声,1为普通男声,3为情感合成-度逍遥,4为情感合成-度YY,默认为普通女声;
    
            };
            var result = _ttsClient.Synthesis(str_tts, option);
    
            if (result.ErrorCode == 0)// 或 result.Success
            {
                File.WriteAllBytes(fileName, result.Data);
                //播放百度语音合成的文件
                //MP3Player _mp3Player=new MP3Player();//mp3播放
                // _mp3Player.FilePath = "百度语音合成的语音.mp3";
                // _mp3Player.Play();//mp3播放
                //MP3Player._Instance().Play(fileName);
    
            }
    
        }
        //百度语音识别——识别本地文件:声音识别为文本
        public void BDAI_ASRData()
        {
            var data = File.ReadAllBytes("语音pcm文件地址");
            var result = _asrClient.Recognize(data, "pcm", 16000);
            Console.Write(result);
        }
        //百度语音识别——识别URL中的语音文件
        public void AsrUrl()
        {
            var result = _asrClient.Recognize(
                "http://xxx.com/待识别的pcm文件地址",
                "http://xxx.com/识别结果回调地址",
                "pcm",
                16000);
            Console.WriteLine(result);
        }
    }
    

    }

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Runtime.InteropServices;
    using System.Text;
    using System.Threading.Tasks;

    namespace XiaoXiangGuang_project
    {
    public class MP3Player
    {
    private static MP3Player Instance;
    private MP3Player()
    { }
    public static MP3Player _Instance()
    {
    if (Instance==null)
    {
    Instance = new MP3Player();
    }
    return Instance;
    }
    /// <summary>
    /// 文件地址
    /// </summary>
    //private string filePath;

            /// <summary>   
            /// 播放   
            /// </summary>   
            public void Play(string filePath)
            {
                mciSendString("close all", "", 0, 0);
                mciSendString("open " + filePath + " alias media", "", 0, 0);
               // mciSendString("play media", "", 0, 0);
                mciSendString("play media", "", 0, 0);
    
        }
    
        /// <summary>   
        /// 暂停   
        /// </summary>   
        public void Pause()
            {
                mciSendString("pause media", "", 0, 0);
            }
    
            /// <summary>   
            /// 停止   
            /// </summary>   
            public void Stop()
            {
                mciSendString("close media", "", 0, 0);
            }
    
            /// <summary>   
            /// API函数   
            /// </summary>   
            [DllImport("winmm.dll", EntryPoint = "mciSendString", CharSet = CharSet.Auto)]
            private static extern int mciSendString(
             string lpstrCommand,
             string lpstrReturnString,
             int uReturnLength,
             int hwndCallback
            );
        
    }
    

    }

    相关文章

      网友评论

          本文标题:WPF_baiduYY

          本文链接:https://www.haomeiwen.com/subject/thjaoxtx.html