Dr. Dobb's Journal - January 2008 - (Page 36) Press the P key to play the sound file. Then type the 4 numbers you hear and press Enter. many audio software packages for starting or stopping playback. But the Spacebar is already a browser hot key for page down, so I settled for P as in “Play.” When the P key up is detected, KeyCheck() clears any data that may be in the text box. Then it plays the audio file in one of two ways depending on the browser and the OS. If under IE/Windows, the embedded Windows Media player is used. The Media Player has an object model that allows script control. Calling the Player’s controls.play() starts the Media Player. If the user is not on IE/Windows, the script uses whatever the user has set as the default media player. When I tested this, I found that when I used the HTML embed tag, the dynamically created sound file did not play. Using the iframe tag instead, with a 0 size frame, plays the file. When the audio has started, I shift the focus to the input text box. This sequence— activating the audio on the P key hit, shifting the focus to the input text box, and accepting the Enter key as a signal that the entry is complete—allows for a mouse-free user experience. Below KeyPress() is code that embeds the Windows Media Player if the user is on IE/Windows. The url parameter is given the value of PlaySound.php. This is the file that generates the audio. Also, autoStart is False so the audio won’t just start when the page is first loaded. You may want to change this value to True depending on how you are presenting the CAPTCHA. After the JavaScript section comes the form data. The form takes the user input for the CAPTCHA and calls the CaptchaSubmit.php file to verify the input data. Lastly, I include a div section to contain the HTML frame that may be generated in KeyPress(). They’re Playing Our Song The PlaySound.php script (Listing Two) uses PHP to dynamically form an MP3 stream by concatenating shorter MP3 files. The PHP header() function writes out the content-type as MPEG. Then, I start the stream with a short introduction that simply says, “Please enter these four numbers.” This is the Intro.mp3 file, which starts the stream. 36 Dr. Dobb’s Journal l www.ddj.com l January 2008 http://www.ddj.com
For optimal viewing of this digital publication, please enable JavaScript and then refresh the page. If you would like to try to load the digital publication without using Flash Player detection, please click here.