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?
 
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
 
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
Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

 
Dagan HooverDeveloperCommented:
Correct.
0
 
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
 
hidrauAuthor Commented:
thanks very much
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.