Recent

Author Topic: Playsound and altering volume  (Read 29077 times)

w click

  • Full Member
  • ***
  • Posts: 205
Playsound and altering volume
« on: January 24, 2011, 06:14:11 pm »
I've got a program, under Windows, that plays a wav file with Playsound.

playsound('C:\Documents and Settings\david\Desktop\buzz.wav',0,0);

It also changes the volume.  Unfortunately it won't do both at the same time.  Playsound locks the program up for the duration of the noise.  Any ideas?  Can I shell it off as a thread or something?

Also, with playsound rather than a mediaplayer, how do I extract the wav file's duration and progress through playing?

zariq

  • Full Member
  • ***
  • Posts: 109
Re: Playsound and altering volume
« Reply #1 on: January 24, 2011, 11:12:54 pm »
I think the third parameter is for specifying if the sound is a file or in memory and whether it should wait for the sound to finish or return straight away. So you want snd_async. If you google it you should get all the options available.

w click

  • Full Member
  • ***
  • Posts: 205
Re: Playsound and altering volume
« Reply #2 on: January 25, 2011, 02:03:45 pm »
Brilliant, did the trick - many thanks.

Anyone know how to find out if the Volume in muted and how to change that property's state?  In Windows.

zoiddani

  • New Member
  • *
  • Posts: 29
Re: Playsound and altering volume
« Reply #3 on: January 25, 2011, 09:04:48 pm »

Fred vS

  • Hero Member
  • *****
  • Posts: 3835
    • StrumPract is the musicians best friend
Re: Playsound and altering volume
« Reply #4 on: January 25, 2011, 09:26:55 pm »
Hello.
The most easy is to use a external lib like Bass  http://www.un4seen.com/
It works perfectly with Lazarus...
I use Lazarus 2.2.0 32/64 and FPC 3.2.2 32/64 on Debian 11 64 bit, Windows 10, Windows 7 32/64, Windows XP 32,  FreeBSD 64.
Widgetset: fpGUI, MSEgui, Win32, GTK2, Qt.

https://github.com/fredvs
https://gitlab.com/fredvs
https://codeberg.org/fredvs

w click

  • Full Member
  • ***
  • Posts: 205
Re: Playsound and altering volume
« Reply #5 on: January 26, 2011, 05:35:01 pm »
Interesting...

I tried zoidanni's suggestion (uses WaveOutSetVolume) which I've had working for ages but I just didn't realise.  It changes the Wav volume, not the overall volume (which is useful) but the Windows Master Volume panel stays unchanged.

On the otherhand, mixerSetControlDetails, another piece of code I found, changes both the Windows Master Volume panel and the actual loudness of the noise made.

w click

  • Full Member
  • ***
  • Posts: 205
Re: Playsound and altering volume
« Reply #6 on: January 27, 2011, 06:41:32 pm »
Anyone know how you can find out if Playsound (with snd_async) has actually finished playing the sound.  Better still, how far it has got?

w click

  • Full Member
  • ***
  • Posts: 205
Re: Playsound and altering volume
« Reply #7 on: January 31, 2011, 11:36:59 am »
I'm still bashing my head against the wall and going through fruitless searches on the web.

Can anyone help with finding the position and length of a sound file while using playsound?  Also mute?

Please.  Help.

Leledumbo

  • Hero Member
  • *****
  • Posts: 8835
  • Programming + Glam Metal + Tae Kwon Do = Me
Re: Playsound and altering volume
« Reply #8 on: January 31, 2011, 01:40:27 pm »
From my googling, I can't seem to find it. Looks like PlaySound was created for quick and simple audio playing, but no more advanced than that. You have to use waveOut* function family. But I suggest using a true audio library like audorra / soundlib instead.

w click

  • Full Member
  • ***
  • Posts: 205
Re: Playsound and altering volume
« Reply #9 on: January 31, 2011, 03:55:19 pm »
Thanks for the pointers.

I just want something simple and audorra looks enormous (and it doesn't mute, just turns the volume to zero).  I'll have a look at soundlib.

Is there anywhere that explains waveout?

My web searches seem to find my own question more often than anything useful.

Leledumbo

  • Hero Member
  • *****
  • Posts: 8835
  • Programming + Glam Metal + Tae Kwon Do = Me
Re: Playsound and altering volume
« Reply #10 on: January 31, 2011, 05:42:51 pm »
Quote
Is there anywhere that explains waveout?
MSDN of course. Note that the articles are spread out. So be sure to look for thoroughly. You can start from here.

w click

  • Full Member
  • ***
  • Posts: 205
Re: Playsound and altering volume
« Reply #11 on: January 31, 2011, 06:15:11 pm »
Thank you, but what does it mean?

MMRESULT waveOutGetPosition(
    HWAVEOUT hwo,
    LPMMTIME pmmt,
    UINT cbmmt
);

I can get to:-

if waveoutgetposition(goodness knows)=MMSYSERR_NOERROR then begin
    label5.caption:=inttostr(probably a variable in goodness knows);
end;

I mean, why can't Microsoft include an example in Lazarus code!!!


mas steindorff

  • Hero Member
  • *****
  • Posts: 566
Re: Playsound and altering volume
« Reply #12 on: January 31, 2011, 07:05:42 pm »
I mean, why can't Microsoft include an example in Lazarus code!!!
If they do not push the .net approch, then their master plan of the "pay each time you run" will no come to pass >:D
windows 10 &11, Ubuntu 21+ IDE 3.4 general releases

zariq

  • Full Member
  • ***
  • Posts: 109
Re: Playsound and altering volume
« Reply #13 on: January 31, 2011, 10:09:06 pm »
Use mci instead. You can get the current position, start playback from any position, pause, get length and other stuff. Have look at

http://www.programmersheaven.com/mb/mfc_coding/105580/105580/playing-wave-files/

the code is in c++ but shouldn't be a problem. Works with delphi, and about to try it with lazarus.

Also have a look at this:

http://www.delphipages.com/forum/showthread.php?t=143155
« Last Edit: January 31, 2011, 10:47:41 pm by zariq »

w click

  • Full Member
  • ***
  • Posts: 205
Re: Playsound and altering volume
« Reply #14 on: February 01, 2011, 10:30:11 am »
I'm afriad I couldn't get it to work.

Pascal and Lazarus make sense to me, but sadly all these MCI, MMSYSTEM, etc, just seem like magic words.

All I need to do is:-

1. Play a .wav file.  (Done with playsound.)
2. Change the volume.  (Done with a SetMixerVolume routine I was pointed to).
3. Fade.  (Easy with the above.)
4. Kill the sound.  (Playsound with a nil.)
5. Check and change the mute (as opposed to Volume:=0) button.
6. Something graphical with a progressbar to show how far the through the wav file we are.

And then I'm away programming the stuff I know how to do.

It's so frustrating to be 4/6 of the way there.

 

TinyPortal © 2005-2018