{"id":239134,"date":"2022-04-08T10:41:01","date_gmt":"2022-04-08T02:41:01","guid":{"rendered":"https:\/\/lrxjmw.cn\/?p=239134"},"modified":"2022-03-29T10:42:27","modified_gmt":"2022-03-29T02:42:27","slug":"system-collections-linux","status":"publish","type":"post","link":"https:\/\/lrxjmw.cn\/system-collections-linux.html","title":{"rendered":"\u7b80\u5355\u4ecb\u7ecdC#\u5b9e\u73b0\u6587\u5b57\u8f6c\u8bed\u97f3\u529f\u80fd"},"content":{"rendered":"\n\n\n
\u5bfc\u8bfb<\/td>\n\u8fd9\u7bc7\u6587\u7ae0\u4e3b\u8981\u4e3a\u5927\u5bb6\u8be6\u7ec6\u4ecb\u7ecd\u4e86C#\u5b9e\u73b0\u6587\u5b57\u8f6c\u8bed\u97f3\u529f\u80fd\uff0c\u6587\u4e2d\u793a\u4f8b\u4ee3\u7801\u4ecb\u7ecd\u7684\u975e\u5e38\u8be6\u7ec6\uff0c\u5177\u6709\u4e00\u5b9a\u7684\u53c2\u8003\u4ef7\u503c\uff0c\u611f\u5174\u8da3\u7684\u5c0f\u4f19\u4f34\u4eec\u53ef\u4ee5\u53c2\u8003\u4e00\u4e0b<\/strong><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n

\u672c\u6587\u5b9e\u4f8b\u4e3a\u5927\u5bb6\u5206\u4eab\u4e86C#\u5b9e\u73b0\u6587\u5b57\u8f6c\u8bed\u97f3\u7684\u5177\u4f53\u4ee3\u7801\uff0c\u4f9b\u5927\u5bb6\u53c2\u8003\uff0c\u5177\u4f53\u5185\u5bb9\u5982\u4e0b<\/p>\n

\u5ba2\u6237\u63d0\u51fa\u8981\u6c42\uff0c\u5c06\u6587\u5b57\u5185\u5bb9\u8f6c\u4e3a\u8bed\u97f3\uff0c\u56e0\u4e3a\u5185\u7f51\u73af\u5883\uff0c\u6ca1\u529e\u6cd5\u91c7\u7528\u8054\u7f51\uff0c\u5728\u7ebf\u8fd9\u79cd\u65b9\u5f0f\uff0c\u7075\u673a\u4e00\u52a8\uff0c\u80fd\u5426\u5199\u4e00\u4e2a\u7b80\u5355\u7684\u4f8b\u5b50\u5462\uff0c\u641c\u7d22\u76f8\u5173\u8d44\u6599\u8fd8\u771f\u884c\uff0c\u8bdd\u4e0d\u591a\u8bf4\uff0c\u6709\u56fe\u6709\u771f\u76f8<\/p>\n

\"\"<\/p>\n

\u5173\u952e\u662f\uff0cc#\u6709\u73b0\u6210\u7684\u4e00\u4e2a\u5f15\u7528<\/p>\n

\u53f3\u952e\u70b9\u51fb\u9879\u76ee > \u6dfb\u52a0\u5f15\u7528 > .Net > \u627e\u5230System.Speech\u70b9\u51fb\u786e\u5b9a<\/p>\n

\u63a7\u5236\u53f0\u7a0b\u5e8f\u4ee3\u7801\uff1a<\/p>\n

\r\nusing System;\r\nusing System.Collections.Generic;\r\nusing System.IO;\r\nusing System.Linq;\r\nusing System.Speech.Synthesis;\r\nusing System.Text;\r\nusing System.Threading.Tasks;\r\nusing System.Windows.Forms;\r\n  \r\n  \r\n  \r\nnamespace TxtToVoice\r\n{\r\n    class Program\r\n    {\r\n        [STAThread] \/\/\u9ed8\u8ba4\u7ebf\u7a0b\u6a21\u578b\u662f\u5355\u7ebf\u7a0b\u5355\u5143 (STA) \u6a21\u5f0f\r\n        static void Main(string[] args)\r\n        {\r\n  \r\n            \/\/Application.EnableVisualStyles();\r\n            \/\/Application.SetCompatibleTextRenderingDefault(false);\r\n            \/\/Application.Run(new Form1());\r\n  \r\n            \/\/return;\r\n           \r\n  \r\n               OpenFileDialog open = new OpenFileDialog();\r\n  \r\n               open.Title = \"\u8bf7\u9009\u62e9\u6587\u672c\"; \/\/\u6253\u5f00\u7684\u6587\u4ef6\u9009\u62e9\u5bf9\u8bdd\u6846\u4e0a\u7684\u6807\u9898\r\n  \r\n               open.Filter = \"\u6587\u672c\u6587\u4ef6(*.txt)|*.txt|\u6240\u6709\u6587\u4ef6(*.*)|*.*\";\/\/\u8bbe\u7f6e\u6587\u4ef6\u7c7b\u578b\r\n  \r\n               open.InitialDirectory = @\"D:\\project\\\";\/\/\u9ed8\u8ba4\u6253\u5f00\u76ee\u5f55\r\n  \r\n               open.FilterIndex = 1;\/\/\u8bbe\u7f6e\u9ed8\u8ba4\u6587\u4ef6\u7c7b\u578b\u663e\u793a\u987a\u5e8f\r\n  \r\n               open.RestoreDirectory = false;\/\/\u662f\u5426\u8bb0\u5fc6\u4e0a\u6b21\u6253\u5f00\u7684\u76ee\u5f55\r\n  \r\n               \/\/open.Multiselect = true;\/\/\u662f\u5426\u5141\u8bb8\u591a\u9009\r\n  \r\n               string content=string.Empty;\r\n  \r\n               if (open.ShowDialog() == DialogResult.OK)\/\/\u6309\u4e0b\u786e\u5b9a\u9009\u62e9\u7684\u6309\u94ae\r\n               {\r\n  \r\n                   string[] filename = open.FileNames;\/\/\u83b7\u53d6\u591a\u4e2a\u6587\u4ef6\u7684\u8def\u5f84\u53ca\u6587\u4ef6\u540d\u5e76\u5b58\u5165\u6570\u7ec4\r\n  \r\n                   MessageBox.Show(filename[0]);\r\n  \r\n                  \/\/ MessageBox.Show(filename[1]);\r\n  \r\n                  \/\/ MessageBox.Show(open.FileName); \/\/\u83b7\u53d6\u8def\u5f84\u53ca\u6587\u4ef6\u540d\r\n  \r\n                  \/\/ MessageBox.Show(open.SafeFileName);\/\/\u83b7\u53d6\u6587\u4ef6\u540d\r\n  \r\n                   content = ReadFile(filename[0]);\r\n  \r\n               }\r\n            \/\/-----------------------------------\u8bfb\u51fa\u6587\u4ef6\u5185\u5bb9---------------------------------\r\n            \r\n               SpeechSynthesizer voice = new SpeechSynthesizer();   \/\/\u521b\u5efa\u8bed\u97f3\u5b9e\u4f8b\r\n               voice.Rate = -1; \/\/\u8bbe\u7f6e\u8bed\u901f,[-10,10]\r\n               voice.Volume = 100; \/\/\u8bbe\u7f6e\u97f3\u91cf,[0,100]\r\n               \/\/voice.SpeakAsync(\"Hellow Word\");  \/\/\u64ad\u653e\u6307\u5b9a\u7684\u5b57\u7b26\u4e32,\u8fd9\u662f\u5f02\u6b65\u6717\u8bfb\r\n  \r\n               \/\/\u4e0b\u9762\u7684\u4ee3\u7801\u4e3a\u4e00\u4e9bSpeechSynthesizer\u7684\u5c5e\u6027\uff0c\u770b\u5b9e\u9645\u60c5\u51b5\u662f\u5426\u9700\u8981\u4f7f\u7528\r\n  \r\n               voice.SpeakAsyncCancelAll();  \/\/\u53d6\u6d88\u6717\u8bfb\r\n               voice.Speak(content);  \/\/\u540c\u6b65\u6717\u8bfb\r\n               voice.Pause();  \/\/\u6682\u505c\u6717\u8bfb\r\n               voice.Resume(); \/\/\u7ee7\u7eed\u6717\u8bfb\r\n  \r\n               voice.Dispose();  \/\/\u91ca\u653e\u6240\u6709\u8bed\u97f3\u8d44\u6e90\r\n  \r\n        }\r\n  \r\n        \/\/\/ \r\n        \/\/\/ \u8bfb\u53d6\u6587\u4ef6\uff0c\u8fd4\u56de\u76f8\u5e94\u5b57\u7b26\u4e32\r\n        \/\/\/ <\/summary>\r\n        \/\/\/ \u6587\u4ef6\u8def\u5f84\r\n        \/\/\/ \u8fd4\u56de\u6587\u4ef6\u5185\u5bb9<\/returns>\r\n        private static string ReadFile(string fileName)\r\n        {\r\n            StringBuilder str = new StringBuilder();\r\n            using (FileStream fs = File.OpenRead(fileName))\r\n            {\r\n                long left = fs.Length;\r\n                int maxLength = 100;\/\/\u6bcf\u6b21\u8bfb\u53d6\u7684\u6700\u5927\u957f\u5ea6\r\n                int start = 0;\/\/\u8d77\u59cb\u4f4d\u7f6e\r\n                int num = 0;\/\/\u5df2\u8bfb\u53d6\u957f\u5ea6\r\n                while (left > 0)\r\n                {\r\n                    byte[] buffer = new byte[maxLength];\/\/\u7f13\u5b58\u8bfb\u53d6\u7ed3\u679c\r\n                    char[] cbuffer = new char[maxLength];\r\n                    fs.Position = start;\/\/\u8bfb\u53d6\u5f00\u59cb\u7684\u4f4d\u7f6e\r\n                    num = 0;\r\n                    if (left < maxLength)\r\n                    {\r\n                        num = fs.Read(buffer, 0, Convert.ToInt32(left));\r\n                    }\r\n                    else\r\n                    {\r\n                        num = fs.Read(buffer, 0, maxLength);\r\n                    }\r\n                    if (num == 0)\r\n                    {\r\n                        break;\r\n                    }\r\n                    start += num;\r\n                    left -= num;\r\n                    str = str.Append(Encoding.UTF8.GetString(buffer));\r\n                }\r\n            }\r\n            return str.ToString();\r\n        }\r\n    }\r\n}<\/pre>\n

\u7a97\u4f53\u4ee3\u7801\uff1a<\/p>\n

\r\nusing System;\r\nusing System.Collections.Generic;\r\nusing System.ComponentModel;\r\nusing System.Data;\r\nusing System.Drawing;\r\nusing System.IO;\r\nusing System.Linq;\r\nusing System.Speech.Synthesis;\r\nusing System.Text;\r\nusing System.Threading;\r\nusing System.Threading.Tasks;\r\nusing System.Windows.Forms;\r\n  \r\nnamespace TxtToVoiceForm\r\n{\r\n    public partial class Form2 : Form\r\n    {\r\n        private SpeechSynthesizer speech;\r\n  \r\n        \/\/\/ \r\n        \/\/\/ \u97f3\u91cf\r\n        \/\/\/ <\/summary>\r\n        private int value = 100;\r\n        \/\/\/ \r\n        \/\/\/ \u8bed\u901f\r\n        \/\/\/ <\/summary>\r\n        private int rate;\r\n  \r\n        public Form2()\r\n        {\r\n            InitializeComponent();\r\n            ReadlocalFile();\r\n            comboBox1.SelectedIndex = 0;\r\n        }\r\n  \r\n        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)\r\n        {\r\n            rate = Int32.Parse(comboBox1.Text);\r\n        }\r\n  \r\n        \/\/private void \u6253\u5f00\u6587\u4ef6ToolStripMenuItem_Click(object sender, EventArgs e)\r\n        \/\/{\r\n        \/\/    this.ReadlocalFile();\r\n  \r\n        \/\/}\r\n  \r\n        \/\/\/ \r\n        \/\/\/ \u8bfb\u53d6\u672c\u5730\u6587\u672c\u6587\u4ef6\u7684\u65b9\u6cd5\r\n        \/\/\/ <\/summary>\r\n        private void ReadlocalFile()\r\n        {\r\n            var open = new OpenFileDialog();\r\n  \r\n            open.ShowDialog();\r\n  \r\n            \/\/\u5f97\u5230\u6587\u4ef6\u8def\u5f84\r\n            string path = open.FileName;\r\n  \r\n            if (path.Trim().Length == 0)\r\n            {\r\n  \r\n                return;\r\n            }\r\n  \r\n            var os = new StreamReader(path, Encoding.UTF8);\r\n            string str = os.ReadToEnd();\r\n            textBox1.Text = str;\r\n        }\r\n        private void \u6e05\u7a7a\u5185\u5bb9ToolStripMenuItem_Click(object sender, EventArgs e)\r\n        {\r\n            textBox1.Text = \"\";\r\n        }\r\n  \r\n        private void button1_Click(object sender, EventArgs e)\r\n        {\r\n            string text = textBox1.Text;\r\n  \r\n            if (text.Trim().Length == 0)\r\n            {\r\n                MessageBox.Show(\"\u4e0d\u80fd\u9605\u8bfb\u7a7a\u5185\u5bb9!\", \"\u9519\u8bef\u63d0\u793a\");\r\n                return;\r\n            }\r\n  \r\n            if (button1.Text == \"\u8bed\u97f3\u8bd5\u542c\")\r\n            {\r\n  \r\n                speech = new SpeechSynthesizer();\r\n  \r\n                new Thread(Speak).Start();\r\n  \r\n                button1.Text = \"\u505c\u6b62\u8bd5\u542c\";\r\n  \r\n            }\r\n            else if (button1.Text == \"\u505c\u6b62\u8bd5\u542c\")\r\n            {\r\n  \r\n                speech.SpeakAsyncCancelAll();\/\/\u505c\u6b62\u9605\u8bfb\r\n  \r\n                button1.Text = \"\u8bed\u97f3\u8bd5\u542c\";\r\n            }\r\n        }\r\n  \r\n        private void Speak()\r\n        {\r\n  \r\n            speech.Rate = rate;\r\n            \/\/speech.SelectVoice(\"Microsoft Lili\");\/\/\u8bbe\u7f6e\u64ad\u97f3\u5458\uff08\u4e2d\u6587\uff09\r\n            \/\/speech.SelectVoice(\"Microsoft Anna\"); \/\/\u82f1\u6587\r\n            speech.Volume = value;\r\n            speech.SpeakAsync(textBox1.Text);\/\/\u8bed\u97f3\u9605\u8bfb\u65b9\u6cd5\r\n            speech.SpeakCompleted += speech_SpeakCompleted;\/\/\u7ed1\u5b9a\u4e8b\u4ef6\r\n        }\r\n  \r\n        \/\/\/ \r\n        \/\/\/ \u8bed\u97f3\u9605\u8bfb\u5b8c\u6210\u89e6\u53d1\u6b64\u4e8b\u4ef6\r\n        \/\/\/ <\/summary>\r\n        \/\/\/ \r\n        \/\/\/ \r\n        void speech_SpeakCompleted(object sender, SpeakCompletedEventArgs e)\r\n        {\r\n            button1.Text = \"\u8bed\u97f3\u8bd5\u542c\";\r\n        }\r\n  \r\n        \/\/\/ \r\n        \/\/\/ \u62d6\u52a8\u8fdb\u5ea6\u6761\u4e8b\u4ef6\r\n        \/\/\/ <\/summary>\r\n        \/\/\/ \r\n        \/\/\/ \r\n        private void trackBar1_Scroll(object sender, EventArgs e)\r\n        {\r\n            \/\/\u56e0\u4e3atrackBar1\u7684\u503c\u4e3a\uff080-10\uff09\u4e4b\u95f4\u800c\u97f3\u91cf\u503c\u4e3a\uff080-100\uff09\u6240\u4ee5\u8981\u4e5810\uff1b\r\n            value = trackBar1.Value * 10;\r\n        }\r\n  \r\n        private void button2_Click(object sender, EventArgs e)\r\n        {\r\n  \r\n  \r\n  \r\n            string text = textBox1.Text;\r\n  \r\n            if (text.Trim().Length == 0)\r\n            {\r\n                MessageBox.Show(\"\u7a7a\u5185\u5bb9\u65e0\u6cd5\u751f\u6210!\", \"\u9519\u8bef\u63d0\u793a\");\r\n                return;\r\n            }\r\n  \r\n            this.SaveFile(text);\r\n  \r\n        }\r\n  \r\n        \/\/\/ \r\n        \/\/\/ \u751f\u6210\u8bed\u97f3\u6587\u4ef6\u7684\u65b9\u6cd5\r\n        \/\/\/ <\/summary>\r\n        \/\/\/ \r\n        private void SaveFile(string text)\r\n        {\r\n            speech = new SpeechSynthesizer();\r\n            var dialog = new SaveFileDialog();\r\n            dialog.Filter = \"*.wav|*.wav|*.mp3|*.mp3\";\r\n            dialog.ShowDialog();\r\n  \r\n            string path = dialog.FileName;\r\n            if (path.Trim().Length == 0)\r\n            {\r\n                return;\r\n            }\r\n            speech.SetOutputToWaveFile(path);\r\n            speech.Volume = value;\r\n            speech.Rate = rate;\r\n            speech.Speak(text);\r\n            speech.SetOutputToNull();\r\n            MessageBox.Show(\"\u751f\u6210\u6210\u529f!\u5728\" + path + \"\u8def\u5f84\u4e2d\uff01\", \"\u63d0\u793a\");\r\n  \r\n        }\r\n  \r\n        private void label1_Click(object sender, EventArgs e)\r\n        {\r\n  \r\n        }\r\n  \r\n        private void label3_Click(object sender, EventArgs e)\r\n        {\r\n            this.ReadlocalFile();\r\n        }\r\n  \r\n  \r\n  \r\n    }\r\n}<\/pre>\n

\u610f\u5916\u5f97\u77e5C#\u4e30\u5bcc\u7684\u529f\u80fd\uff0c\u8fd8\u662f\u81ea\u5df1\u52a8\u624b\uff0c\u6ca1\u6709\u60f3\u8c61\u7684\u90a3\u4e48\u96be\uff0c\u5e0c\u671b\u80fd\u5e2e\u5230\u6709\u9700\u8981\u7684\u5c0f\u4f19\u4f34\u4eec\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"

\u672c\u6587\u5b9e\u4f8b\u4e3a\u5927\u5bb6\u5206\u4eab\u4e86C#\u5b9e\u73b0\u6587\u5b57\u8f6c\u8bed\u97f3\u7684\u5177\u4f53\u4ee3\u7801\uff0c\u4f9b\u5927\u5bb6\u53c2\u8003\uff0c\u5177\u4f53\u5185\u5bb9\u5982\u4e0b \u5ba2\u6237\u63d0\u51fa\u8981\u6c42\uff0c\u5c06\u6587\u5b57\u5185\u5bb9\u8f6c\u4e3a\u8bed\u97f3\uff0c […]<\/p>\n","protected":false},"author":362,"featured_media":235333,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[55],"tags":[],"class_list":["post-239134","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-thread"],"acf":[],"_links":{"self":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/239134","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/users\/362"}],"replies":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/comments?post=239134"}],"version-history":[{"count":3,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/239134\/revisions"}],"predecessor-version":[{"id":239138,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/239134\/revisions\/239138"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media\/235333"}],"wp:attachment":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media?parent=239134"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/categories?post=239134"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/tags?post=239134"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}