CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 13 of 13
  1. #1
    Join Date
    Dec 2012
    Posts
    38

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

    Hi all,

    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.

    Thanks

  2. #2
    Join Date
    Dec 2001
    Posts
    6,332

    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?

  3. #3
    Join Date
    Dec 2012
    Posts
    38

    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.

    Thanks

  4. #4
    Join Date
    Dec 2001
    Posts
    6,332

    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 01: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?

  5. #5
    Join Date
    Dec 2012
    Posts
    38

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

    Yeah, you was right: that site is full of examples! Nice work, man

    Now I've downloaded lots of codes, and now I only have to try them. Anyway, if something is missing, can I ask you for?

    Thank you very much,
    bye

  6. #6
    Join Date
    Dec 2001
    Posts
    6,332

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

    Sure, if you get stuck, or have any questions, feel free to continue the thread, or start a new one as appropriate.
    Please remember to rate the posts and threads that you find useful.
    How can something be both new and improved at the same time?

  7. #7
    Join Date
    Dec 2012
    Posts
    38

    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!


  8. #8
    Join Date
    Dec 2001
    Posts
    6,332

    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.

    Yes, I'm pretty sure the volume takes a max value of 1000.

    For info on MCI commands, these may be of help to you:
    http://www.tactilemedia.com/info/MCI_Control_Info.html
    http://msdn.microsoft.com/en-us/libr...(v=vs.85).aspx
    Last edited by WizBang; December 12th, 2012 at 02:37 PM.
    Please remember to rate the posts and threads that you find useful.
    How can something be both new and improved at the same time?

  9. #9
    Join Date
    Dec 2012
    Posts
    38

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

    From MSDN
    For example, the following play command will not return control to the application until the playback completes:
    Code:
    mciSendString("play mydevice from 0 to 100 wait", 
        lpszReturnString, lstrlen(lpszReturnString), NULL);
    It seems that this flag is the same as the SND_SYNC flag for PlaySound API. I've to test it with longer sounds to understand if it really works as I want or not... Otherwise, I'll try your suggestion!

    However, thank you very very much, you give me a lot of help. Thanks again,

  10. #10
    Join Date
    Dec 2012
    Posts
    38

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

    Finally I did it (but how much work before )

  11. #11
    Join Date
    Dec 2001
    Posts
    6,332

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

    Quote Originally Posted by Cereal Killer View Post
    From MSDN
    For example, the following play command will not return control to the application until the playback completes:
    Code:
    mciSendString("play mydevice from 0 to 100 wait", 
        lpszReturnString, lstrlen(lpszReturnString), NULL);
    Interesting. I've never tried it with specifying both start and end points. I suppose that's what makes the difference.

    Anyway, glad you got it working. You should mark the thread resolved if you're satisfied with the answer.
    Please remember to rate the posts and threads that you find useful.
    How can something be both new and improved at the same time?

  12. #12
    Join Date
    Dec 2012
    Posts
    38

    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?

  13. #13
    Join Date
    Dec 2001
    Posts
    6,332

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

    Sure, go ahead and PM me, or you can begin a new thread if it will help others.
    Please remember to rate the posts and threads that you find useful.
    How can something be both new and improved at the same time?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured