Playing Sound with IntraWeb
Posted by Olaf Monien in Delphi, Development, IntraWeb, tags: ajax, Delphi, IntraWebA couple of IntraWeb users asked me recently how to play sounds (wav, mp3 etc) on IntraWeb forms. There is a TIWMpeg component which can be used for that.If you display a list of sound files on an IntraWeb form (maybe because your IW application is an MP3 online portal), then you possibly want to give the user the ability to listen to the sound files without leaving your web site. (I.e. no nasty download, start player and listen process).
This can easily be implemented by using IntraWeb TIWMpeg component. Just drop as many instances of that guy onto your form as you need:
![]()
There are basically three settings you have to look at:
- MPEGFile : There you have to set the file name which should be played. Remember that all files need to be in the application’s /FILES directory if you set the file via MPEGFile.Filename. You can also link to external files via MPEGFile.URL. In the latter case, remember to specify a full URL, including HTTP:// that is.
- For sound files set MPEGFile.RenderEMBED to true.
- To prevent the sounds to start playing instantly when the form shows up, we have to add AutoStart=false. This needs to be done by attaching the OnHTMLTag event:
procedure TIWForm19.MPEGFile1HTMLTag(ASender: TObject; ATag: TIWHTMLTag); begin //Prevent playing sound when page loads initially ATag.AddStringParam('autostart', 'false'); end;:
IWMpeg can not just play Mpeg files (MP3 sounds, Mpeg movies etc), but also wav, mdi and other sound files. It basically depends on the user’s browser. I’ve successfully tested “wav” files with IE, FF, Opera and Safari.
This will render one “Play Button” per IWMpeg component:

Playing a Sound as Response to an Async Event
If you want to play a sound to “visualize” that an async event just returned (e.g. OnAsyncClick of a Button), then just do this:
procedure TIWForm19.IWButton1AsyncClick(Sender: TObject;2: EventParams: TStringList);
begin
//Trigger sound to play when Async response is received by Webbrowser
WebApplication.CallBackResponse.AddJavaScriptToExecute('BEEPSOUNDIWCL.Play()');
end;
This starts the sound for a TIWMPEGFile named “BeepSound” once the async event returned. BEEPSOUNDIWCL is a “shortcut” to get access to BeepSound’s HTML representation. All IntraWeb components can be accessed in JavaScript by their name in upper case plus ‘IWCL’ (IntraWeb Class Library).
Download AjaxSoundDemo.zip





Entries (RSS)