Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

PlaySound tutorial

193 views
Skip to first unread message

Henrik Erlandsson

unread,
Jun 11, 2003, 11:16:12 AM6/11/03
to
Had problems with a D6 app playing sounds with TMediaPlayer, which wouldn't
work on any other PC but that which had D6 installed (!)

So I got the tip to use PlaySound instead. The example below gives the error
"Invalid variable type conversion". No wonder, since I'm guessing blindly,
not having found any docs on how to use the PlaySound parameters or how to
know which of the constants in the D6 help corresponded to which values,
since they didn't seem to be declared in D6. That's why I've guessed that a
value of 1 starts a sound (non-looped) and 0 stops it. ;) The example uses 2
buttons, Button1 OpenDialog to select the .wav file, Button2 to play it.

How do I give PlaySound the right params to play and stop a simple .wav? Has
anyone done this and have a code snippet? Or any pointers to an API guide
which is of any use to a Delphi newbie? :)

/Henrik


implementation
{$R *.dfm}

function PSnd(fn:string;x:longword;y:longword):boolean;
stdcall; external 'WINMM.DLL' name 'PlaySound';

procedure TForm1.Button1Click(Sender: TObject);
var fn:string;
begin
if OpenDialog1.Execute then
fn := OpenDialog1.FileName;
PSnd(fn,null,1) //win32 api

end;

procedure TForm1.Button2Click(Sender: TObject);
begin
PSnd(null,null,0);

end;

end.

Ignacio Vazquez

unread,
Jun 11, 2003, 11:17:59 AM6/11/03
to
"Henrik Erlandsson" <henrik.e...@nikom.se> wrote in message
news:3ee747a8$1...@newsgroups.borland.com...

> The example below gives the error
> "Invalid variable type conversion".

You shouldn't have to import it explicitly this way; just include Windows.

> No wonder, since I'm guessing blindly,
> not having found any docs on how to use the PlaySound parameters or how to
> know which of the constants in the D6 help corresponded to which values,
> since they didn't seem to be declared in D6.

Check the Platform SDK help.

> That's why I've guessed that a
> value of 1 starts a sound (non-looped) and 0 stops it. ;) The example uses
2
> buttons, Button1 OpenDialog to select the .wav file, Button2 to play it.

BZZZT! Sorry, that's wrong. *crowd goes "awww..."* Bob, what's our
contestant's consolation prize?

Bob: Well Jim, today's consolation prize is the URL inside the Platform SDK
help that points to the PlaySound function.

ms-help://MS.PSDK.1033/multimed/mmfunc_9uxw.htm

And of course, the Platform SDK can always be downloaded from the MSDN
website (http://msdn.microsoft.com/).

Cheers,
Ignacio

Henrik Erlandsson

unread,
Jun 13, 2003, 9:56:33 AM6/13/03
to
Well, like I said, I knew beforehand that the example was wrong ;)

Your answer isn't really helpful. "Include Windows"? I of course already
have Windows in the uses clause, since it's already auto-included when you
create a New Application. I've read the Windows.Playsound help, and it
sucks, since it's not "Delphi" help but "API" help, and it doesn't provide
enough info on how to convert the variables to give the right input params,
or show where (if) the fdwSound constants to start/stop sound have been
declared.

The ms-help link is broken.

And the MS SDK help only helps C/API programmers. I still need to know how
to make Delphi pass the right params.

I pasted some code, hoping to get some code back. Anyone else, who's done
this in Delphi?

Ignacio Vazquez

unread,
Jun 13, 2003, 10:46:20 AM6/13/03
to
"Henrik Erlandsson" <henrik.e...@nikom.se> wrote in message
news:3ee9d7fd$1...@newsgroups.borland.com...

> Your answer isn't really helpful. "Include Windows"? I of course already
> have Windows in the uses clause, since it's already auto-included when you
> create a New Application.

So then you should just be able to use PlaySound directly, instead of
redefining it.

> I've read the Windows.Playsound help, and it
> sucks, since it's not "Delphi" help but "API" help, and it doesn't provide
> enough info on how to convert the variables to give the right input
params,
> or show where (if) the fdwSound constants to start/stop sound have been
> declared.

I don't have the Delphi Win32 API help installed for that reason; I find the
Platform SDK help much more useful.

> The ms-help link is broken.

It only works inside the Platform SDK help.

> And the MS SDK help only helps C/API programmers.

No, the help file is useful for anyone that has to use API functions.

> I still need to know how
> to make Delphi pass the right params.

Take a look at the Platform SDK help and it will be much clearer.

> I pasted some code, hoping to get some code back. Anyone else, who's done
> this in Delphi?

PlaySound(fn, 0, SND_ASYNC or SND_FILENAME or SND_NOWAIT);

PlaySound(nil, 0, SND_PURGE);

Cheers,
Ignacio

Henrik Erlandsson

unread,
Jun 16, 2003, 6:36:20 AM6/16/03
to
Afraid the problem isn't understanding what to pass to PlaySound. The
problem is how to make it compile.

Created New App that uses Windows, inserted your code. Won't compile,
because it knows nothing of any PlaySound function or the constants it uses.

[Error] playsndtest.pas(37): Undeclared identifier: 'PlaySound'
[Error] playsndtest.pas(37): Undeclared identifier: 'SND_ASYNC'
[Error] playsndtest.pas(37): Undeclared identifier: 'SND_FILENAME'
[Error] playsndtest.pas(44): Undeclared identifier: 'PlaySound'
[Error] playsndtest.pas(44): Undeclared identifier: 'SND_PURGE'

How do I make my compiler wiser?

Cheers,
Henrik.


AV

unread,
Jun 16, 2003, 7:33:51 AM6/16/03
to
Simply add, mmsystem to your uses section.


"Henrik Erlandsson" <henrik.e...@nikom.se> wrote in message

news:3eed...@newsgroups.borland.com...

Henrik Erlandsson

unread,
Jun 17, 2003, 9:22:39 AM6/17/03
to
Great!

So now I know the the C header file (mmsystem.h) could be used in a uses
clause ;)

Now it compiles, and I get no error when the routine is called, but I still
get no sound at all out of the computer.

Here's the code I use:

const
fn:Pchar = 'C:\windows\media\chimes.wav';

PlaySound(fn, 0, SND_SYNC or SND_FILENAME or SND_NOWAIT);

Shouldn't this work? What am I doing wrong?

/Henrik.

Ignacio Vazquez

unread,
Jun 17, 2003, 10:14:10 AM6/17/03
to
"Henrik Erlandsson" <henrik.e...@nikom.se> wrote in message
news:3eef1608$1...@newsgroups.borland.com...

> fn:Pchar = 'C:\windows\media\chimes.wav';

You can safely change this to String. Delphi casts it on the fly.

> Shouldn't this work? What am I doing wrong?

Don't get offended, but here come the basic questions.

1) Does sound work for other applications?
2) Are other applications currently using the sound card?

Cheers,
Ignacio

Henrik Erlandsson

unread,
Jun 23, 2003, 10:31:22 AM6/23/03
to
1) Yes
2) No

I'm using Win98 in a PC with a TurtleBeach sound card for this example, to
precipitate any more basic questions ;)

/Henrik

"Ignacio Vazquez" <ivazquezATorioncommunications.com> skrev i meddelandet
news:3eef236c$1...@newsgroups.borland.com...

0 new messages