How can I play sound with PlaySound with low volume?

Hello guys

I don't know if it is possible, but I want to play a sound "wav file" with PlaySound, I know how to do that, but I don't know how to play a music in a level volume.

I did this PlaySound(Pchar(ExtractFilePath(Application.ExeName) + '\WAV\ERROLETRA.WAV'), 1, SND_ASYNC);

But, there is a way to play this sound in a level volume? Is there any parameter that I can do this?

Thanks
Alex
LVL 1
hidrauAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
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.

Dagan HooverDeveloperCommented:
According to:

http://social.msdn.microsoft.com/forums/en-US/vssmartdevicesnative/thread/37e37989-357d-4258-bd9a-aa0e1e2fd4a6/

You'll need to use:

waveOutSetVolume()

Which has a Delphi example here:

http://www.greatis.com/delphicb/tips/lib/others-setsoundvolume.html

Which when cleaned up looks like this:

uses MMSystem;

procedure ChangeWaveOutVolume(Volume: WORD);
var
  MyWaveOutCaps : TWaveOutCaps;
begin
  If WaveOutGetDevCaps(WAVE_MAPPER,@MyWaveOutCaps,SizeOf(MyWaveOutCaps)) = MMSYSERR_NOERROR Then
    begin
    WaveOutSetVolume(WAVE_MAPPER,MakeLong(Volume,Volume));
  end;
end;

Open in new window

0
hidrauAuthor Commented:
Hi ThievingSix:

Thanks for your reply, I understand what you gave me,

but, this comand will turn down the windows sound, am I right?

0
Dagan HooverDeveloperCommented:
Correct.
0
Fundamentals of JavaScript

Learn the fundamentals of the popular programming language JavaScript so that you can explore the realm of web development.

hidrauAuthor Commented:
Hummm, I see

I thought I could turn down the volume of my file when I play it.
Let me explain what I am doing and maybe you can give me another idea.

My program works with microsoft syntetizer, it reproduces the human voice.
This program is a small project where the user can learn language, I show the user
a portuguese word and ask it the english version. According to user's type, a sound
of key is played by this command PlaySound. After the user typed the word, the sound of it
is played my syntetizer.

So, with the comand you gave me I need to turn down the sound first and then played the file that
is the sound of typing, this order:

a) turn down the computer sound
b) play the file
c) come back to the original  level sound

I notice that this comand can play the wav file together with another sound, that is, I can listen to music
with winanp or other sound program that the function PlaySound plays the wav file without any problem.

If I turn down the computer, both will be turned down, not only this file and That is gonna be a problem for me. Did you understand ?

What should you advice me to do ?



0
Dagan HooverDeveloperCommented:
Hmm, will both be turned down? Have you tried?

We aren't changing the entire system volume, just wave out as I understand? Or am I wrong and we need to look for another option?
0
systanCommented:
unit Unit1;

interface

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

type
  TForm1 = class(TForm)
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation


uses
  MMSystem;

{$R *.dfm}

function GetWaveVolume(var LVol: DWORD; var RVol: DWORD): Boolean;
var
  WaveOutCaps: TWAVEOUTCAPS;
  Volume: DWORD;
begin
  Result := False;
  if WaveOutGetDevCaps(WAVE_MAPPER, @WaveOutCaps, SizeOf(WaveOutCaps)) = MMSYSERR_NOERROR then
    if WaveOutCaps.dwSupport and WAVECAPS_VOLUME = WAVECAPS_VOLUME then
    begin
      Result := WaveOutGetVolume(WAVE_MAPPER, @Volume) = MMSYSERR_NOERROR;
      LVol   := LoWord(Volume);
      RVol   := HiWord(Volume);
    end;
end;

function SetWaveVolume(const AVolume: DWORD): Boolean;
var
  WaveOutCaps: TWAVEOUTCAPS;
begin
  Result := False;
  if WaveOutGetDevCaps(WAVE_MAPPER, @WaveOutCaps, SizeOf(WaveOutCaps)) = MMSYSERR_NOERROR then
    if WaveOutCaps.dwSupport and WAVECAPS_VOLUME = WAVECAPS_VOLUME then
      Result := WaveOutSetVolume(WAVE_MAPPER, AVolume) = MMSYSERR_NOERROR;
end;


function PlaySound( Param : String ): String;
type TPS = function (lpszSoundName: PAnsiChar; uFlags: UINT): BOOL; stdcall;
var  PS : TPS;
     h : THandle;
begin
      if FileExists( Param ) then
      begin
           h   := LoadLibrary( 'winmm.dll' );
           @PS := GetProcAddress( H, 'sndPlaySoundA' );
           if PS( Pchar( Param ) , 0 ) then
               Result := 'The sound was played...'
           else
               Result := 'The Sound wasnt played...';
           FreeLibrary( H );
      end
      else
          Result := 'The File' + Param + ' Dosnt exits...';
end;

procedure TForm1.Button1Click(Sender: TObject);
var
LVol: Word;
RVol: Word;
begin

{ // if TspinEdit is used
 if GetWaveVolume(LVol, RVol) then
  begin
    SpinEdit1.Value := LVol;
    SpinEdit2.Value := RVol;
  end;
}

LVol := 30000; //left volume
RVol := 30000; //right volume
SetWaveVolume(MakeLong(LVol, RVol));
playsound('YOUR_WAV_FILE.wav');
end;


end.
0
Dagan HooverDeveloperCommented:
Same..thing...?
0
systanCommented:
>a) turn down the computer sound
>b) play the file
>c) come back to the original  level sound


ans
a:
function GetWaveVolume(var LVol: DWORD; var RVol: DWORD): Boolean;
var
  WaveOutCaps: TWAVEOUTCAPS;
  Volume: DWORD;
begin
  Result := False;
  if WaveOutGetDevCaps(WAVE_MAPPER, @WaveOutCaps, SizeOf(WaveOutCaps)) = MMSYSERR_NOERROR then
    if WaveOutCaps.dwSupport and WAVECAPS_VOLUME = WAVECAPS_VOLUME then
    begin
      Result := WaveOutGetVolume(WAVE_MAPPER, @Volume) = MMSYSERR_NOERROR;
      LVol   := LoWord(Volume);
      RVol   := HiWord(Volume);
    end;
end;

// Save first the original Volumes
// Global variable vLVol,vRVol : Word

 if GetWaveVolume(LVol, RVol) then
  begin
    vLVol := LVol;
    vRVol := RVol;
  end;


function SetWaveVolume(const AVolume: DWORD): Boolean;
var
  WaveOutCaps: TWAVEOUTCAPS;
begin
  Result := False;
  if WaveOutGetDevCaps(WAVE_MAPPER, @WaveOutCaps, SizeOf(WaveOutCaps)) = MMSYSERR_NOERROR then
    if WaveOutCaps.dwSupport and WAVECAPS_VOLUME = WAVECAPS_VOLUME then
      Result := WaveOutSetVolume(WAVE_MAPPER, AVolume) = MMSYSERR_NOERROR;
end;

SetWaveVolume(MakeLong( 0, 0 )); //Turn Down the Volume First

ans
b:
function PlaySound( Param : String ): String;
type TPS = function (lpszSoundName: PAnsiChar; uFlags: UINT): BOOL; stdcall;
var  PS : TPS;
     h : THandle;
begin
      if FileExists( Param ) then
      begin
           h   := LoadLibrary( 'winmm.dll' );
           @PS := GetProcAddress( H, 'sndPlaySoundA' );
           if PS( Pchar( Param ) , 0 ) then
               Result := 'The sound was played...'
           else
               Result := 'The Sound wasnt played...';
           FreeLibrary( H );
      end
      else
          Result := 'The File' + Param + ' Dosnt exits...';
end;

playsound('YOUR_WAV_FILE.wav');  //Play your Sound

ans
c:
SetWaveVolume(MakeLong( vLVol ,  vRVol )); //Restore Original Volume
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
hidrauAuthor Commented:
thanks very much
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.