Solved

using PlatSound function

Posted on 1997-10-20
4
213 Views
Last Modified: 2013-11-20
I have a   buzz.wav wave file to play. I use
 PlaySound("buzz.wav",NULL, SND_ASYNC);

to play it. It works no problem (buzz.wav is 10K much less than
recommended 100K, fine). problem is I do want to include buzz.wav
to my project as import resource and use PlaySound to acess
to the resource directly (static binding of wav resource);
How shoul I do it ?  SAY MY RESOURCE id is ID_WAV_BUZZ.

I tried several combinations but it wont take
BOOL PlaySound( LPCSTR pszSound, HMODULE hmod, DWORD fdwSound );

I only know fdwSound  =  SND_RESOURCE|SND_ASYNC

but I need to give my resource id  ID_WAV_BUZZ also how ?
is it to load the resource first and use the handle ?

thanks
0
Comment
Question by:has
  • 2
  • 2
4 Comments
 
LVL 2

Accepted Solution

by:
wpinto earned 50 total points
ID: 1308498
Try the following, it should work

--------------------------------------------------------------------------------------
PlaySound("ID_WAV_BUZZ", AfxGetInstanceHandle(), SND_RESOURCE | SND_ASYNC);

In the .rc file
ID_WAV_BUZZ   WAVE   buzz.wav
--------------------------------------------------------------------------------------

If you want to load the resource, try the following code:

--------------------------------------------------------------------------------------
    BOOL bRtn;
    LPSTR lpRes;
    HANDLE hResInfo, hRes;
 
    /* Find the WAVE resource. */
 
    hResInfo = FindResource(AfxGetInstanceHandle(), lpName, "WAVE");
    if (hResInfo == NULL)
        return FALSE;
 
    /* Load the WAVE resource. */
 
    hRes = LoadResource(AfxGetInstanceHandle(), hResInfo);
    if (hRes == NULL)
        return FALSE;
 
    /* Lock the WAVE resource and play it. */
 
    lpRes = LockResource(hRes);

    if (lpRes != NULL) {
        bRtn = sndPlaySound(lpRes, SND_MEMORY | SND_SYNC |
            SND_NODEFAULT);
        UnlockResource(hRes);
    }
    else
        bRtn = 0;
 
    /* Free the WAVE resource. */
 
    FreeResource(hRes);
--------------------------------------------------------------------------------------
0
 

Author Comment

by:has
ID: 1308499
In the .rc file

IDR_WAV_BUZZ   WAVE DISCARDABLE  "buzz.wav"

in the .cpp file i tried

PlaySound("IDR_WAV_BUZZ",AfxGetInstanceHandle(),SND_RESOURCE|SND_ASYNC);     did not work, i also tried AfxGetResourceHandle()
nothing !

I tried
hRes = LoadResource(AfxGetInstanceHandle(), hResInfo);

but hRes is NULL always. with  AfxGetResourceHandle() also NULL.

any ideas ?

0
 
LVL 2

Expert Comment

by:wpinto
ID: 1308500
Its definitely strange since it worked fine for me
(VC++ 4.2, Windows NT 4.0)

Some troubleshooting tips:
* Make sure you are changing the right file (ie. files from your current project)
* Make sure that you have included the file <mmsystem.h> & linked your project with winmm.lib
* I am assuming here that the .wav file is in the same directory as the project. Please double-click on this .wav file (from Explorer) &  check if you get any sound
* Make sure that the .rc file is getting compiled

If all this fails, try creating a simple project using AppWizard.

In the .rc file add
IDR_WAV_BUZZ WAVE DISCARDABLE "buzz.wav"

in the .cpp file (OnAppAbout() function) add
PlaySound("IDR_WAV_BUZZ",AfxGetInstanceHandle(),SND_RESOURCE|SND_ASYNC);
before creating the About dialog box.
Also add #include <mmsystem.h> to this cpp file

& link this project with winmm.lib

Now whenever you click on Help->About(), you should get some sound. This is how I tested it & it worked fine for me.

Hope this helps

Wilfred
0
 

Author Comment

by:has
ID: 1308501
thank you very much
0

Featured Post

Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction: Dynamic window placements and drawing on a form, simple usage of windows registry as a storage place for information. Continuing from the first article about sudoku.  There we have designed the application and put a lot of user int…
Introduction: The undo support, implementing a stack. Continuing from the eigth article about sudoku.   We need a mechanism to keep track of the digits entered so as to implement an undo mechanism.  This should be a ‘Last In First Out’ collec…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…

831 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question