-
Recently, I have found that voice verification codes are becoming more and more popular. For example, I have seen them in the registered Gmail mailbox. There are also voice verification codes on MSN pages, and some foreign websites.
After taking the time to research, the main difference between voice verification codes and general verification codes lies in how to make the verification code play. The principle of voice verification code in this article: generate verification code from server,
And save it to the cookie (getcode.aspx.cs). When you click to listen to the verification code, call the javascirpt operation (jquery is used here) to read the verification code from the cookie.
Then transfer the verification code to the codevoice.aspx page, then synthesize the verification code in order to generate an mp3 file, and finally transfer this file to flash for playback.
The sound you will hear is: "The current verification code is 5678, please enter it." This principle is similar to the voice verification code used by most websites.
Source code download: Download (please use VS2008 SP1 or VS2010 to open)
Place verification code image page code on the page
view plaincopy to clipboardprint?
<form id="form1" runat="server">
<div>
<input type="text" name="txtCode" id="txtCode" maxlength="8" />
<img onclick="this.src='getcode.aspx';" src="getcode.aspx" mce_src="getcode.aspx" align="absmiddle" style="cursor: pointer" mce_style="cursor: pointer" alt ="Can't see clearly, please change another one" title="Can't see clearly, please change another one" />
<img id="imgRead" src="image/maintb.gif" mce_src="image/maintb.gif" align="absmiddle" style="cursor: pointer" mce_style="cursor: pointer" alt="Listen to verification code " title="Listen to the verification code" onclick="playvoice('player');" />
<span id="player"></span>
</div>
</form>
<form id="form1" runat="server">
<div>
<input type="text" name="txtCode" id="txtCode" maxlength="8" />
<img onclick="this.src='getcode.aspx';" src="getcode.aspx" mce_src="getcode.aspx" align="absmiddle" style="cursor: pointer" mce_style="cursor: pointer" alt ="Can't see clearly, please change another one" title="Can't see clearly, please change another one" />
<img id="imgRead" src="image/maintb.gif" mce_src="image/maintb.gif" align="absmiddle" style="cursor: pointer" mce_style="cursor: pointer" alt="Listen to verification code " title="Listen to the verification code" onclick="playvoice('player');" />
<span id="player"></span>
</div>
</form>
The js function called when clicking to listen to the verification code is as follows:
view plaincopy to clipboardprint?
function playvoice(id) {
var voiceid = document.getElementById(id);
var voicecode = $.cookie('ValidateCode');
voiceid.innerHTML = "<embed id='sound_play' name='sound_play' src="sound_play.swf?" + (new Date().getTime()) + "" mce_src="sound_play.swf?" + (new Date().getTime()) + ""
FlashVars='isPlay=1&url=codevoice.aspx&code=" + voicecode + "' width='0' height='0' allowScriptAccess='always'
ype='application/x-shockwave-flash' pluginspage='http://www.macromedia.com/go/getflashplayer' /></embed>";
}
function playvoice(id) {
var voiceid = document.getElementById(id);
var voicecode = $.cookie('ValidateCode');
voiceid.innerHTML = "<embed id='sound_play' name='sound_play' src="sound_play.swf?" + (new Date().getTime()) + "" mce_src="sound_play.swf?" + (new Date().getTime()) + ""
FlashVars='isPlay=1&url=codevoice.aspx&code=" + voicecode + "' width='0' height='0' allowScriptAccess='always'
type='application/x-shockwave-flash' pluginspage='http://www.macromedia.com/go/getflashplayer' /></embed>";
}
Among them, $.cookie('ValidateCode') is to read the cookie verification code. A jquery cookie plug-in is used here.
The code to generate the mp3 page is as follows:
//Read the verification code to generate mp3, including header begin.mp3 and tail end.mp3
view plaincopy to clipboardprint?
Response.ContentType = "audio/mpeg";
Response.WriteFile("sound/begin.mp3");
string checkCode = HttpContext.Current.Request.QueryString["code"].ToString();// string checkCode = "8888";
if (checkCode.Length > 0)
for (int i = 0; i < checkCode.Length; i++)
{
Response.WriteFile("sound/"+checkCode[i] + ".mp3");
}
Response.WriteFile("sound/end.mp3");
Response.ContentType = "audio/mpeg";
Response.WriteFile("sound/begin.mp3");
string checkCode = HttpContext.Current.Request.QueryString["code"].ToString();// string checkCode = "8888";
if (checkCode.Length > 0)
for (int i = 0; i < checkCode.Length; i++)
{
Response.WriteFile("sound/"+checkCode[i] + ".mp3");
}
Response.WriteFile("sound/end.mp3");
[The author of this article published it simultaneously on cnblogs, csdn, and http://www.ajaxcn.net . Please keep this note when reprinting]
The flash playback code mainly performs the right-click action on the first key frame. Insert the following code according to the incoming playback number mp3 address.
view plaincopy to clipboardprint?
var mysound = new Sound();
var mysong = url;
var isPlay = 1;
var intnum:Number = setInterval(playSong, 500);
function playSong() {
if (isPlay == 1) {
mysound.loadSound(mysong+"?code="+code, true);
mysound.start();
clearInterval(intnum);
isPlay = 0;
}