Sound

can you give me an idea of how to make a button when I click it will make a short sound whether it's a wave or a midi sound? (include the source code or any homepage to refer to)
LVL 1
lewis_looAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

rwilson032697Commented:
You can use a Resource File o hold the wave to play... Create a file with a .RC extension and compile it with the BRCC32 program.

    Example MYWAV.RC:
    1 WAVE C:\wav\welcome.wav


    Compile it with BRCC32 MYWAV.RC
    This will generate a .RES file for you. Include this in your program with {$R mywav.res }

    You can then play the sound with this line in the Button OnClick event:

    PlaySound(pchar('1'), HInstance, SND_RESOURCE|SND_SYNC);

Cheers,

Raymond.
0
IndefreiCommented:
unit Musikplayer1;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  StdCtrls, MPlayer, ExtCtrls;

type
  TForm1 = class(TForm)
    MusikCheck: TCheckBox;
    ListBox1: TListBox;
    Timer1: TTimer;
    MediaPlayer1: TMediaPlayer;
    procedure FormCreate(Sender: TObject);
    procedure FindFile(Sender: TObject);
    procedure Timer1Timer(Sender: TObject);
    procedure MusikCheckClick(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
  private
    { Private-Deklarationen }

  public
    { Public-Deklarationen }

  end;

var
  Form1: TForm1;
  myExt : string;
  PathFileName,Path, FileName : String;
implementation

{$R *.DFM}

procedure TForm1.FormCreate(Sender: TObject);
begin
    PathFileName :='C:\win95\media\*.*';
    MyExt:='.Wav';
    FindFile(Form1);
    PathFileName :='C:\Programme\Creative\Ctsnd\Samples\*.*';
   PathFileName :='C:\Mymedia\*.*';
    MyExt:='.MID';
    FindFile(Form1);
    Mediaplayer1.autoOpen := true;
    mediaplayer1.DeviceType:=dtAutoSelect;
    MediaPlayer1.FileName :=ListBox1.Items.Strings[random(ListBox1.Items.Count - 1)];
    mediaplayer1.open;
    MusikCheckClick(form1);

end;

procedure TForm1.FindFile(Sender: TObject);
var
        srec : TSearchRec;
begin
       Path := ExtractFilePath(PathFileName);
       FileName := ExtractFileName(PathFileName);

       if findfirst(PathFileName,faanyfile,srec) = 0 then
       begin
         repeat
           If Not(srec.attr and fadirectory > 0) then
           begin
              if ExtractFileExt((Srec.Name))= myExt then
              begin
                    ListBox1.Items.Add(path + Srec.Name);
              end;

           end;
         Until findnext(srec) <> 0;
       end;
       FindClose(SRec);
end;

procedure TForm1.Timer1Timer(Sender: TObject);
begin
    Timer1.Interval := 100;
    Timer1.Enabled := True;

    case Mediaplayer1.mode of

    mpPlaying :
     begin
        Timer1.Enabled := True;
     end;
    mpStopped :
     begin
        Mediaplayer1.Close;
        MediaPlayer1.FileName :=ListBox1.Items.Strings[random(ListBox1.Items.Count - 1)];
        Mediaplayer1.open;
        Mediaplayer1.play;
     end;
    end;
end;

procedure TForm1.MusikCheckClick(Sender: TObject);
begin
     If MusikCheck.State = cbchecked Then
     begin
          Timer1Timer(form1);
     end
     else
     begin
          Timer1.enabled:=false;
          mediaplayer1.Stop;
     end;    
end;

procedure TForm1.FormDestroy(Sender: TObject);
begin
    Timer1.enabled:=false;
    mediaplayer1.Stop;
    Mediaplayer1.Close;
end;

end.
0
IndefreiCommented:
Oops
>> PathFileName :='C:\Programme\Creative\Ctsnd\Samples\*.*';
too much, eliminate it
Indi
0
Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

lewis_looAuthor Commented:
do you all have any suggestion of how to play gif animator under Delphi
0
IndefreiCommented:
lewis_loo,
That's another question...
0
lewis_looAuthor Commented:
can you give in more detail step by step ? (I am the beginnner)
is BRCC32 is on Borland C++? then how can I use in Delphi
0
IndefreiCommented:
The bcc32.exe is in
\program files\borland\delphi\bin
to compile see Raymond's answer

the method using a tmediaplayer is completely different to Raymonds solution.

My sample just plays all soundfiles of the given directory
randomly one after the other

To play simply one sound on buttomClick
Put a Buttom from the "standard" component palette
and a mediaplayer from the "system" component palette
on to your form

In the event table of your button  doubleclick on the editfield
OnClick and you get

procedure TForm1.Button1Click(Sender: TObject);
begin
// write
     Mediaplayer1.play;
end;
In the event table of your form doubleclick on the editfield
OnCreate and

procedure TForm1.FormCreate(Sender: TObject);
begin
// write
    Mediaplayer1.autoOpen := true;
    mediaplayer1.DeviceType:=dtAutoSelect;
    MediaPlayer1.FileName :='C:\Mywav\Welcome.wav';
    mediaplayer1.open;
end;
and close the mediaplayer in
OnDestroy
procedure TForm1.FormDestroy(Sender: TObject);
begin
// write
    Mediaplayer1.Close;
end;

Well, Raymond
hope, you'll forgive me, that i post this as a new answer
cheers
Indi
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
IndefreiCommented:
of course BRCC32.exe
0
lewis_looAuthor Commented:
Thanks for your help
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Delphi

From novice to tech pro — start learning today.