Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

play wave file 1

Status
Not open for further replies.

jbpelletier

Programmer
Sep 8, 2001
232
0
0
CA
hi,

i whant to know how to play a wave file without passing by
the mediaplayer.. because the media player takes too long to start and execute..

tanx
jb

 
I Presume you mean the mediaplayer component in Delphi? This shouldnt take very long to start, I use it in a couple of games for sound events, it hasnt been a problem, but there are ways to play media directly with API calls.
this one might do it.

BOOL PlaySound(LPCSTR pszSound, HMODULE hmod, DWORD fdwSound);

Has anyone got some usage code for this ?



Steve
Be excellent to each other and Party on!
 
Try this:
Code:
uses
  mmsystem;
...
...
...
...
...
procedure TForm1.Button1Click(Sender: TObject);
begin
  sndPlaySound(PChar('C:\Windows\Media\Tada.wav'), SND_NODEFAULT Or SND_ASYNC Or SND_LOOP);
end;

-----------------------------------------------------
-"There is always a different way to solve it"

//Nordlund
 
...If you want to load a soudfile from a embedded .res file.

Review this thread for using resource files:
Then, use this code to play the sound from the resource:
Code:
{$R sounds.res}
uses
  mmsystem;
var
  theSound: Pointer;

procedure TForm1.Button1Click(Sender: TObject);
begin
  sndPlaySound(theSound, SND_MEMORY or SND_NODEFAULT or SND_ASYNC);
end;

initialization
  theSound := Pointer(FindResource(hInstance, 'sound', 'wav'));
  if theSound <> nil then 
  begin
    theSound := Pointer(LoadResource(hInstance, HRSRC(theSound)));
    if theSound <> nil then tada := LockResource(HGLOBAL(theSound));
  end;

-----------------------------------------------------
-"There is always a different way to solve it"

//Nordlund
 
Tanx Nordlund

sndPlaySound was what i was looking for.. i just didn't remeber the syntax.

Also i think the res file thing will be better for me, ill try it asp

jb
 
hi,

using sndPlaySound
do there is a way to set a variable that contain the sound file (not the path).. i would like to know the syntax too.

because each time i call sndPlaySound(path,...); it seem to load the sounds (i have a delay of like half a sec)

tanx
jb
 
How large is you soundfile?
The res version should store the sound in the memory, and when you click the button, it just plays from the memory...

Unless the soundfile is gigantic and it has to be stored in the windows swapfile...



KungTure-RX.jpg

//Nordlund
 
My sounds files are very small, like from 4 to maybe 10 k.
Just to give an idea, all my sounds have a length of 1 sec (in the media player).

Actually i have difficulties with the res thing(i havent tried with .wav into res so far) but all my attemp to simply use cursor (.cur) added to my project res file have failed.

ex: as mentioned in the help file
-i add a cursor using the image editor called 'NewCursor'
in my projet res file.
- then i pasted this code in the projet
const crMyCursor = 5;
procedure TForm1.FormCreate(Sender: TObject);
begin
Screen.Cursors[crMyCursor] := LoadCursor(HInstance, 'NewCursor');
Cursor := crMyCursor;
end;

but it dont work, i still have the "default" cursor

Q1: maybe i miss something.. or i dont add my file as it should be in my res file.. ?
Q2: how do u insert a wav into the res file, so far i only found about adding bitmap,cursor and icon.

tanx
jb
 
Hi.
The first step is to create a thesound.rc file containing this text
Code:
sound wav sound.wav

The next step is to copy sound.wav the the same directory as the .rc file.

Then you start a command prompt and write:
Code:
brcc32 thesound.rc

Now you have a thesound.res file in the directory.
NOte: You have to add thesound.res in the delphi sourcecode:

Code:
type
  TForm1 = class(TForm)
    ABSGroupBox1: TABSGroupBox;
    ABSStatusBar1: TABSStatusBar;
    Label1: TLabel;
    Button1: TButton;
    procedure FormCreate(Sender: TObject);
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation
{$R *.dfm}

var
  theSound: Pointer;

[b]{$R thesound.res}[/b]

procedure TForm1.Button1Click(Sender: TObject);
begin
  sndPlaySound(theSound, SND_MEMORY or SND_NODEFAULT or SND_ASYNC);
end;

initialization
  theSound := Pointer(FindResource(hInstance, 'sound', 'wav'));
  if theSound <> nil then
  begin
    theSound := Pointer(LoadResource(hInstance, HRSRC(theSound)));
    if theSound <> nil then theSound := LockResource(HGLOBAL(theSound));
  end;

end.

Then compile it and enjoy... :)


KungTure-RX.jpg

//Nordlund
 
Tanx again Nordlund,
clear answer

Also,

i think my problems with resfile have something to do with the {$R thesound.res} line.

in my projet i have this instead {$R .res} (delphi type it on his own)
i guess its because i was adding my other stuff in the default projet res file

jb
 
Hi.
Yes, that's might be the problem.
My suggestion is to bundle all external resources in one resource file. I've seen problems when using several external resourcefiles, but it was a while ago...


I wouldn't touch the project .res file unless I had "a gun pointed at my head", you never know what delphi does with it...

KungTure-RX.jpg

//Nordlund
 
Add the {$R thesound.res} line behind the {$R *.res}
and not instead of the {$R *.res} line.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top