How can I achieve this if I play a voice in a program? The following code is implemented in VS2012)
The first thing I think of is to use a voice file, which can reference the Player Plug-ins in the application and then call the APIs of these plug-ins for implementation.
Create a console application, add reference in solution, select com option, find Windows Media Player, and add it to reference. In reference, the namespace of this plug-in is WMPLib. Next, you can use this plug-in to implement audio playback.
Select to reference the added namespace:
Using WMPLib;
Then write
WindowsMediaPlayer player = newWindowsMediaPlayer (); // instantiate the player type
Player. URL = "F:/test/a.wma"; // assign the audio file path to the player.
Console. Read (); // pause the program and do not exit
It is very simple to implement the player Plug-in. In fact, the plug-in function is still very powerful, and it may be necessary to further study the player members.
Second, use the vbs script file. First, create a notepad file, open notepad, and write the following code in it:
CreateObject ("SAPI. spVoice "). speak "test the voice file. ", save and change the extension. vbs, the icon of the file changes, and then double-click the vbs file to read the voice.
This seems to have nothing to do with the program. in C #, I/O is used to process the file and call it.
First introduce the namespace
Using System. IO;
Then generate and call the script code as follows:
String path = @ "F:/test/a. vbs"; // define the script path
String content = "CreateObject (\" SAPI. SpVoice \ "). Speak \" {0} \ ""; // specifies the script content Framework
Content = string. Format (content, "Hello, this is a test speech! "); // Merge and generate script content
File. WriteAllText (path, content, Encoding. Default); // write the content to the script File.
Process. Start (path); // call the script file
It is a good idea to use a machine to read speech. However, if you still need to generate a script file, it will be a part of your work. Is there a third case to generate a speech file.
Third, use Microsoft's Speech Platform
To develop Microsoft Speech Platform applications, you must first organize the development environment
Four files need to be downloaded
1. SpeechPlatformRuntime. msi
2. MSSpeech_TTS_zh-CN_HuiHui.msi
3. MSSpeech_SR_zh-CN_TELE.msi
4. MicrosoftSpeechPlatformSDK. msi
File 1: http://www.microsoft.com/en-us/download/details.aspx? Id = 27225
Files 2 and 3 are: http://www.microsoft.com/en-us/download/details.aspx? Id = 27224
4 is: http://www.microsoft.com/en-us/download/details.aspx? Id = 27226
These files are x86 and 64-bit, depending on your OS.
After downloading and installing these Files, you can reference the installed SDK in the solution, which is installed in C: \ Program Files \ Microsoft SDKs \ Speech \ v11.0 \ Assembly \ Microsoft by default. speech. dll needs to determine the root directory based on the system path)
Reference namespace in code
UsingMicrosoft. Speech. Synthesis;
The code for Implementing Audio Playback is as follows:
SpeechSynthesizer ss = newSpeechSynthesizer (); // instantiate the playing voice type
Ss. setoutputtodefaauaudiodevice (); // sets the playing Voice device, which is the current default
Ss. Volume = 10; // set the Volume
Ss. Speak ("this is a test voice! "); // Read speech
In this way, a simple speech is implemented without generating a speech file.
The last two bases are implemented using Microsoft's TTS function, and the third is more professional.
The following describes the advantages and disadvantages of several methods:
Player Plugin |
Vbs |
Speech Platform |
|
Advantages |
Sound quality is good, and it is related to audio files. Easy to use. |
You only need to enter the voice text, and the voice information is flexible. No Installation File |
No file required or generated More powerful voice Functions |
Disadvantages |
You need to record the voice, which is not flexible. |
Rigid and monotonous Sound Quality Script file to be generated |
Rigid and monotonous Sound Quality Installation package required |
This article is from "Gui Su Wei" blog, please be sure to keep this source http://axzxs.blog.51cto.com/730810/1243632