If this is your first visit, be sure to
check out the FAQ by clicking the
link above. You may have to register or Login
before you can post: click the register link above to proceed. To start viewing messages,
select the forum that you want to visit from the selection below.

[RESOLVED] [VB6]Set output volume level of MY application

I've already asked for this same question in several forums but I've not had valid answers or in some cases I've not had at all. So, I hope that here there's someone that could help me.

I'm trying to build up a little game and, as well as all games have a volume control, I wish to insert the same feature in my app.

Looking for on the net I discovered the waveOutSetVolume API function, that it might do what I want, but it doesn't. This API changes the WAVE output volume level of ALL programs currently running in the workstation, and not my only.

What I want to do is a volume control such Windows Media Player's one (that is the slider near the Play/Stop buttons), that controls the volume of the single application without modifying the system settings.

I know this is possible 'cause most of MP3 players do that (e.g. WMP, WinAmp, ect...) and also this feature is included within the MS DirectSound libraries.

Now you can ask me why I don't use them: because both WMP.dll and DirectSound don't allow me to play sounds asynchronously, while PlaySound API function do.

So my question is: does anybody know how to set my app output volume level of wav sounds played using PlaySound function without changing system settings?

If it's not possible or nobody knows, please tell me so I can try to ask on other forums, hoping that there there's someone knows.

Re: [VB6]Set output volume level of MY application

I'm not aware of any way to set the volume of a sound being played via the PlaySound API other than the wave output volume. However, using mciSendString, you can set the level of MPEG audio playback, and it will only be for the file being played. Other sounds will not be effected. So for example, if your sounds are in mp3 format, you can do it with mciSendString.

Please remember to rate the posts and threads that you find useful.How can something be both new and improved at the same time?

Re: [VB6]Set output volume level of MY application

Throughout my research on the Internet, I had already seen your kind of solution. But the example I downloaded was wrong: in fact each time I tried to run it I received a critical error and VB closed itself. So I didn't test it.

Anyway I've some doubts about this solution. I wish to use PlaySound function 'cause it lets me to play sounds both in sync and async mode (that's one of my requests). So I don't know if also the mciSendString allows me to do it. And what about if I pass as param a file name that doesn't exist?

If you're so kind to give me an example showing how to use this function to play sounds following the requests above, I'd be very very grateful.

Re: [VB6]Set output volume level of MY application

Yes, mciSendString will allow you to play files synchronously or asynchronously. Like most API functions, if you pass mciSendString an invalid parameter, the function will return an error. It is up to you how to handle any errors, but it shouldn't cause crashes unless your code produces one.

The basic sequence you'll need to do is something like:

1) Verify the file exists before attempting to open it.
2) Use GetShortPathName to make sure the filename and path can be passed without any trouble.
3) Use mciSendString to open the file using "MPEGVideo" for the device type (even for mp3 audio).
4) Use mciSendString and "setaudio" to set the volume.
5) Use mciSendString to play/stop/close the file.

For more examples than you'd ever need, search at www.planetsourcecode.com. Yeah, there is no shortage of poor code samples out there. But as you become familiar with the kinds of things you need to do, you'll be better able to tell when a sample is lacking.

Last edited by WizBang; December 11th, 2012 at 12:50 PM.
Reason: Additional info

Please remember to rate the posts and threads that you find useful.How can something be both new and improved at the same time?

Re: [VB6]Set output volume level of MY application

I have only two questions to do before to rate this thread as excellent

The first one is: I found that to play sound syncronously I've to insert into the command line the keyword "WAIT". Is it true? And is it needed to not insert it to play asyncronoulsy?

The second one is: is the max output volume level equals to 1000 or not?

Moreover I'd like to have MS documentation about MCI functions. So I've seen both MSDN and other sites, and all the links to the page where should be the documentation is unavailable. Have you a direct link to that? If you don't, it's the same: you've already done a lot for me!

Re: [VB6]Set output volume level of MY application

According to the documentation, the wait flag "Directs the device to wait until the requested action is complete before returning to the application". As I recall, this does not pause your app until an mp3 has finished playing.

If you want to play two or more files sequentially, you could detect when one has finished before starting the next. Using an alias for each can make it easier to work with them. In that way, you can easily play more than one at a time, switch seamlessly between them, etc.

Re: [VB6]Set output volume level of MY application

I tested the WAIT flag within a large file (more than 3 minutes long) and the WHOLE app stops itself until playback is finished. This means that you won't be able to receive any other event from it before the end of the sound. And that's what I wanted

Just to give all info to future readers, if you don't want to lock the app, but only waiting the end of a sound, you must follow the suggestion of WizBang...

Re: [VB6]Set output volume level of MY application

I've never tried it with specifying both start and end points. I suppose that's what makes the difference.

No, it isn't. The code also works without specifying them. Moreover, you can both send commands to MCI devices and get errors if something goes wrong with ONLY ONE line of code if you use mciExecute call instead of mciSendString (I found this 'new' API some minutes ago).

The only limit is that using mciExecute you cannot get back information about the file you're playing. If you need this info you must use the mciSendString.

WizBang, I need your suggestion about the Play method I'm going to write. But as well as it hasn't nothing to do with this thread, I wish to talk with you in private. Can I?