Adding Audio
In my previous 2 articles – Creating a Captcha Control – Part 1 and Creating a Captcha Control – Part 2 – I have created the user control for the captcha control. The control also allows functionality to play audio to read the text on the captcha.
To achieve the audio feature, I have used a COM library – Microsoft Speech Object Library. To add the COM library, I added a reference to Microsoft Speech Object Library (%Windir%\System32\Speech\Common\Sapi.dll). To add a reference, right-click on the Reference folder, click Add Reference, click COM tab and add the library.
Once the reference is added, the SpeechLib namespace is imported as shown below.
- using SpeechLib;
Then, the Speak method in SpVoice class is used to play the audio as per below.
- protected void ReadCaptcha(object sender, EventArgs e)
- {
- SpVoice voice = new SpVoice();
- char[] text = ((string)ViewState["captcha"]).ToCharArray();
- for (int i = 0; i < text.Length; i++)
- {
- voice.Speak(text[i].ToString(), SpeechVoiceSpeakFlags.SVSFDefault);
- }
- }
As can be seen, I am reading out each of the text and not all the text at once. The reason is, SpVoice class will try to read it as a word instead of individual characters otherwise.
In the following article, I will show how to create and use the handler to generate the image. The whole code for the captcha control can be downloaded from CodePlex at http://captchadotnet.codeplex.com/
0 comments:
Post a Comment