In a recent chess and card game, the leader said that the chat content on the client should be able to be pronounced, that is, the text sent by the player should be automatically read, if the voice package is integrated into the client, the Client installation file will inevitably increase, and the business volume will come up with this solution: B/S side implementation, the general process is that the client requests B/S side, b/S generates the voice file and the client downloads it.
In the process of converting Text to Speech, we used anonymous + reflection. However, the generated speech file sometimes has no sound, and the file size is only a few bytes. The file is not generated successfully and the cause is unknown.
!CodeAs follows:
If (! String . Isnullorempty (context. Request. querystring [ " Txt " ]) &! String . Isnullorempty (context. Request. querystring [ " Type " ])
{
Try
{
String Type = context. Request. querystring [ " Type " ];
String TXT = context. Request. querystring [ " Txt " ];
String Filename = system. Web. Security. formsauthentication. hashpasswordforstoringinconfigfile (Type + txt, " MD5 " );
String Filepath = " /Files/ " + Filename + " . Wav " ;
If (File. exists (context. server. mappath (filepath )))
{
Context. response. Write (filename );;
}
Else
{
Dynamic synth = system. activator. createinstance (system. type. gettypefromprogid ( " SAPI. spvoice " ));
Dynamic filestream = system. activator. createinstance (system. type. gettypefromprogid ( " SAPI. spfilestream " ));
If (Type = " 2 " )
{
Synth. Voice = synth. getvoices ( " Name = VW Lily " ). Item ( 0 );
}
Else
{
Synth. Voice = synth. getvoices ( " Name = VW Liang " ). Item ( 0 );
}
Synth. Rate =- 1 ;
Filestream. Open (context. server. mappath (filepath), speechstreamfilemode. ssfmcreateforwrite, False );
Synth. audiooutputstream = filestream;
Synth. Speak (txt );
Synth. waituntildone ( 1000 );
Synth. Dispose ();
Filestream. Close ();
context. response. Write (filename);
context. response. End ();
}< BR >}< br> catch
{< br> context. response. write ( " 0 " );
context. response. end ();
}
}
Later, I used the. NET 3.0 generation method. Currently, the test is normal. The Code is as follows:
Using System;
Using System. Collections. Generic;
Using System. LINQ;
Using System. Web;
Using System. Speech. synthesis;
Using System. IO;
Using System. Threading;
namespace dokeetts
{< br> ///
/// _ default summary
///
Public class _ default: ihttphandler
{
Public void processrequest (httpcontext context)
{< br> context. response. contenttype = " text/plain " ;
StringTXT = context. Request. querystring ["Txt"];
StringType = context. Request. querystring ["Type"];
StringFilename ="";
Filename = system. Web. Security. formsauthentication. hashpasswordforstoringinconfigfile (Type + txt," MD5 " );
Thread t = New Thread () =>
{
Speechsynthesizer syth = New Speechsynthesizer ();
If (Type = " 2 " )
{
Syth. selectvoice ( " VW Lily " );
}
Else
{
Syth. selectvoice ( " VW Liang " );
}
String Filepath = " /Files/ " + Filename + " . Wav " ;
If (File. exists (context. server. mappath (filepath )))
{
Context. response. Write (filename );;
}
Else
{
Syth. setoutputtowavefile (context. server. mappath (filepath ));
Syth. Speak (txt );
}
Syth. Dispose ();
});
T. Start ();
T. Join ();
Context. response. Write (filename );
Context. response. Flush ();
Context. response. End ();
}
Public BoolIsreusable
{
Get
{
Return False;
}
}
}
}