Sound with GWT using Delicious Playtagger
Delicous introduced a nice little tool called playtagger which allows you to turn links to mp3’s into playable links. That is it inserts a little playbutton next to all mp3 links on a page. Its a nice small script that they’ve released for anyone to use.
This was a perfect solution for getting sound effects in gpokr, which is a texas holdem poker application written with GWT.
Its easy to put sound effects into a GWT application. Follow these steps:
- Create a HTML widget:
HTML sound = new HTML();
- Make a function to play a sound with a filename as a parameter. This function sets the HTML in the HTML widget to an flash object which refers to the delicious playtagger swf file. The object size is set to 1×1 pixels since making it invisible causes the sound to not be heard in some browsers.
public void PlaySound( String soundname ) { sound.setHTML( "<object classid='clsid:d27cdb6e-ae6d-11cf-96b8-444553540000' codebase='http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,0,0' width='1' height='1' id='player' align='absmiddle' VIEWASTEXT>" + "<param name='wmode' value='transparent' />"+ "<param name='allowScriptAccess' value='sameDomain' />"+ "<param name='flashVars' value='theLink=http://www.gpokr.com/"+ soundname +".mp3' />"+ "<param name='movie' value='http://del.icio.us/static/swf/playtagger.swf' />"+ "<param name='quality' value='high' />" + "<embed src='http://del.icio.us/static/swf/playtagger.swf' flashVars='theLink=http://www.gpokr.com/"+soundname+".mp3' quality='high' wmode='transparent' width='1' height='1' name='player' align='absmiddle' allowScriptAccess='sameDomain' type='application/x-shockwave-flash' pluginspage='http://www.macromedia.com/go/getflashplayer' />"+ "</object>" ); }
That’s it! Now every time you call PlaySound the mp3 will play in your gwt app. I’ll leave this up to the reader to wrap this up nicely in a class.