Link to home
Start Free TrialLog in
Avatar of hidrau
hidrauFlag for Brazil

asked on

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
Avatar of ThievingSix
ThievingSix
Flag of United States of America image

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

Avatar of hidrau

ASKER

Hi ThievingSix:

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

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

Correct.
Avatar of hidrau

ASKER

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 ?



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?
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.
Same..thing...?
ASKER CERTIFIED SOLUTION
Avatar of systan
systan
Flag of Philippines image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of hidrau

ASKER

thanks very much