HOME > > TUTORIALS TABLE OF CONTENTS - - - / - - / - - / - - / - - - Other material for programmers

Delphi tutorial: Playing .WAV files with MediaPlayer. (Level Three)

This is still in a draft form.... it is probably mostly right, but I make no promises just yet!!!

This has good information, and there's a search button at the bottom of the page.

Please don't dismiss it just because it isn't full of graphics, scripts, cookies, etc!

Click here if you want to know more about the source and format of these pages.

Dt3f: Playing .WAV files with the MediaPlayer



This is a level 3 tutorial not because it is very complicated, but because the issues are significant to a small group of programs.

As usual, set up a directory for the exclusive use of the application we will develop during this tutorial. COPY (don't MOVE!) into it at least two .WAV files. I used ding.wav and chimes.wav which, in my in Windows 3.1 machine, were in C:\windows. Files you are going to play do not NEED to be in the application's directory, but it pays to eliminate distractions when exploring a new topic.

Don't start Delphi yet! FIRST: make sure that you speakers, soundcard, etc, etc are all behaving. You may be able to hear the WAV files simply by double clicking on the names from within Windows Explorer. Failing that...

Win 3.1: Control Panel| Sound. Navigate to directory your .WAV files are in. Select one. Click "Test". CLick CANCEL... otherwise you'll reassign the sound you tested to the event that the Sound app had selected.

Win 95: Depending on how your computer was set up, you may have...
Programs| Accessories| Multimedia| Media Player
available for making sure everything is ready.

Start Delphi.

Put two buttons on the form. Name them buPlay1 and buPlay2; caption them "Play 1st sound" and "Play 2nd sound" (or somesuch!)

(This is written for Delphi 1, by the way...) From the System tab of the component pallet, add a MediaPlayer component to your form. (The icon is two musical notes and a bit of movie film).

Now create an OnClick handler for buPlay1 as follows...
MediaPlayer1.filename:='ding.wav';
MediaPlayer1.open;
MediaPlauer1.wait:=true;
MediaPlayer1.play;
MediaPlayer1.close;
Get that working.

By the way: the following is functionally equivalent and more elegant...
with MediaPlayer1 do
filename:='ding.wav';
open;
wait:=true;
play;
close;
end;(*with*)
Now make an OnClickHandler for buPlay2 which is identical to the one for buPlay1, except set filename to the other .Wav file.

Don't try to use the bar with "play"/"stop","rewind", etc buttons just yet.

Some details...

The open method accesses the MediaPlayer, getting it ready to do something. Later on, the close is important as it releases the resources tied up when you did "open". You might think you could avoid the constant opening and closing as follows:

In the form's OnCreate handler,
1) Assign a valid filename, e.g. ding.wav to MediaPlayer.filename
2) Do MediaPlayer1.open
In the form's OnClose handler include MediaPlayer1.close;

However, this DOESN'T WORK, as even if you change what is in MdiaPlayer1.filename, the sound you get from Play remains the same. If you try to open the MediaPlayer without first supplying a value to MediaPlayer1.filename, the attempt to open fails. In any case, I prefer opening and closing things when I need them, rather than having them open throughout. (Similar issues arise when you work with data files.)

The MediaPlayer1.wait:=true just before MediaPlayer1.play is important. It stops the program from going straight on to MediaPlayer1.close. Take out the wait:=true and your program will still run, but you won't hear any sounds... they are stopped within milliseconds of being started. The wait:=true has to go just before the Play.

For our simple demo, it would be best to use the object inspector to set MediaPlayer1.visible:=false; However, if you want to play with the player buttons, you just need to do two things with the object inspector:
1) Put the name of a suitable file in MediaPlayer1's filename property, and
2) Change MediaPlayer1's AutoOpen property to "true".

Now if you click on the "Play" button on the row of buttons which are the visble part of MediaPlayer1, you should hear the wav file played.


Enjoy!

Tom, Chichester, England
Click this to download zip file with source code.

            powered by FreeFind
  Site search Web search
Site Map    What's New    Search This search merely looks for the words you enter. It won't answer "Where can I download InpOut32?"
The search engine is not intelligent. It merely seeks the words you specify. It will not do anything sensible with "What does the 'could not compile' error mean?" It will just return references to pages with "what", "does", "could", "not".... etc.

Also: This search only searches the material on one of my websites.

      Click here to visit another of my sites.

      Click here to visit my third site.




Click here if you're feeling kind! (Promotes my site via "Top100Borland")
Ad from page's editor: Yes.. I do enjoy compiling these things for you. I hope they are helpful. However... this doesn't pay my bills!!! Sheepdog Software (tm) is supposed to help do that, so if you found this stuff useful, (and you run a Windows or MS-DOS PC) please visit my freeware and shareware page, download something, and circulate it for me? Links on your page to this page would also be appreciated!
Click here to visit editor's freeware, shareware page.

Link to Tutorials main page
Here is how you can contact this page's author, Tom Boyd.


Valid HTML 4.01 Transitional Page WILL BE tested for compliance with INDUSTRY (not MS-only) standards, using the free, publicly accessible validator at validator.w3.org


If this page causes a script to run, why? Because of things like Google panels, and the code for the search button. Why do I mention scripts? Be sure you know all you need to about spyware.

....... P a g e . . . E n d s .....