最近工作没那么忙,趁着有点时间就写了个像微软和新浪注册时用的语音验证码。
主要是用微软TTS控件来生成声音的,不过声音不是很好听,至于生成的那些过时.wav文件我没做过多的处理。
有一种办法是:你可以把26个字母的语音给录下来并保存为相应的文件名(找个女生来录比较好听,不然对不起观众了,呵呵!),通过读取cookie把语音文件组成起来一并读出来,这样就不会产生过时的.wav文件。如果谁有更好的办法,欢迎大家拍砖。
生成声音的主要代码:
using System.IO;
using SpeechLib;
using System.Drawing;
using System.Threading;
// 生成音频文件
SpVoice voice = new SpVoice();
string voiceStr = "";
for (int i = 0; i < code.Length; i++)
{
voiceStr += code[i] + " ";
}
voice.Rate = 1;
//voice.Speak(voiceStr, SpeechVoiceSpeakFlags.SVSFlagsAsync); //播放音频文件
//voice.Speak(voiceStr, SpeechVoiceSpeakFlags.SVSFPurgeBeforeSpeak);
SpeechStreamFileMode SpFileMode = SpeechStreamFileMode.SSFMCreateForWrite;
SpFileStream SpFileStream = new SpFileStream();
string savePath = Server.MapPath(".") + "\\wav\\" + code + ".wav";
SpFileStream.Open(savePath, SpFileMode, false);
voice.AudioOutputStream = SpFileStream;
voice.Speak(voiceStr, SpeechVoiceSpeakFlags.SVSFlagsAsync);
voice.WaitUntilDone(Timeout.Infinite);
SpFileStream.Close();
生成验证码的程序是用51aspx.com的无刷新仿google波形扭曲彩色Asp.net验证码,个人觉得这个做得很漂亮的 :) 。
下载源代码/Files/xczt/VoiceCode.rar
无刷新仿google波形扭曲彩色Asp.net验证码 是用asp.net 2.0开发的,我把它改成了1.1版本的,如果有需要的话也一并下载吧。呵呵!/Files/xczt/GoogleVerifyCode.rar