Thread: How to play sound from a resource?

  1. #1
    Digga
    Join Date
    May 2005
    Location
    Colorado
    Posts
    15

    How to play sound from a resource?

    I know about the PlaySound(); function. I know how to play a wav sound from memory, but what about playing the sound from a resource. I have made my .res script and want to know how you go about playing the sound from resource.
    PlaySound ((LPCSTR) IDW_RAIN, HMODULE hModule, SND_ASYNC |SND_RESOURCE);

    I basically don't know how to work the second parameter. "HMODULE hModule"

    --Digga

  2. #2
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,868
    try..

    PlaySound( MAKEINTRESOURCE(IDW_RAIN),
    GetModuleHandle(0)/* MFC AfxGetInstanceHandle()*/,
    SND_ASYNC |SND_RESOURCE);
    "Man alone suffers so excruciatingly in the world that he was compelled to invent laughter."
    Friedrich Nietzsche

    "I spent a lot of my money on booze, birds and fast cars......the rest I squandered."
    George Best

    "If you are going through hell....keep going."
    Winston Churchill

  3. #3
    Digga
    Join Date
    May 2005
    Location
    Colorado
    Posts
    15

    It compile fine, but no sound.

    I want to make the program where you simply double click on the program and it automatically plays the sounds. I was able to get it to compile without any problems. But the program does not generate any sound. I want the sounds to basically to be built into the program. I know this is posiible, but it may take a little of trial and error to fiqure it out. Anyway here is my source code, resource script, and rc script.

    Source Code:

    Code:
    // Include the following libraries.
    
    #include <windows.h>
    #include <mmsystem.h>
    #include <stdio.h>
    #include <fstream> 
    #include <iostream>
    
    #include "Resource.h"
    
    #pragma resource "xx.res"
    
    using namespace std;
    
    // Program begins here ------------------------------------------------ 
    
    LRESULT CALLBACK  WndProc (HWND hWindow, UINT msg, WPARAM wParam, LPARAM lParam);
    
    int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR szCmdLine, int iCmdShow)
    
    {
        WNDCLASSEX  wndclass;
    
    		wndclass.hIcon = LoadIcon (hInstance, MAKEINTRESOURCE (IDI_XWORDX));
    
    		HMODULE hmod;
                    hmod=GetModuleHandle("xx.rc");
    
    
    
    
    		PlaySound ((LPCSTR) IDW_BAD, hmod, SND_ASYNC | SND_RESOURCE);
    		PlaySound ((LPCSTR) IDW_BIGFART, hmod, SND_ASYNC | SND_RESOURCE);
    		PlaySound ((LPCSTR) IDW_BADDUCK, hmod, SND_ASYNC | SND_RESOURCE);
    		PlaySound ((LPCSTR) IDW_BUNNYFART, hmod, SND_ASYNC | SND_RESOURCE);
    
    	PlaySound( MAKEINTRESOURCE(IDW_BAD), 
    GetModuleHandle(0)/* MFC AfxGetInstanceHandle()*/,
    SND_ASYNC |SND_RESOURCE);
    
    return 0;
    
    }
    Resource.h

    Code:
    // 10 JUNE 2005
    
    // This includes the resources.
    // This has icon resources and sound resources
    
    //---------------------------------------------------------------------
    
    // Icons--
    
    #define IDI_XWORDX	1000
    
    // Sounds--
    
    // Sounds are in WAV formart.
    
    #define IDW_BAD	        2000
    #define IDW_BIGFART	2001
    #define IDW_BADDUCK	2002
    #define IDW_BUNNYFART	2003
    
    //---------------------------------------------------------------------
    RC Script

    Code:
    // 10 JUNE 2005
    
    // Include the resource files.
    
    //---------------------------------------------------------------------
    
    #include "Resource.h"
    
    //---------------------------------------------------------------------
    
    // *Icon Files*
    
    IDI_XWORDX	ICON	"XWordX.ico"
    
    
    
    // *Sound Files*
    
    IDW_BAD		 WAVE		"Bad.wav"	
    IDW_BIGFART      WAVE		"BigFart.wav"
    IDW_BADDUCK      WAVE		"BadDuck.wav"
    IDW_BUNNYFART	 WAVE		"BunnyFart.wav"        
    
    
    //---------------------------------------------------------------------

  4. #4
    Registered /usr
    Join Date
    Aug 2001
    Location
    Newport, South Wales, UK
    Posts
    1,273
    Quote Originally Posted by Digga
    Code:
                    hmod=GetModuleHandle("xx.rc");
    Please read the appropriate MSDN page before using GetModuleHandle, so that you know how to use it. It expects the name of a loaded executable module to get the handle for, or you can pass it NULL and it will give you the handle of the calling process (i.e. of your program). You want the second option, as novacain described.

  5. #5
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,868
    LOL!


    When I said...
    PlaySound( ..... GetModuleHandle(0)/* MFC AfxGetInstanceHandle()*/,
    ...

    I was giving you the functions for both WIN32 (use GetModuleHandle(0) ) and MFC ( AfxGetInstanceHandle() ). Use only one.

    GetModuleHandle(0) == hInstance

    This should also work

    PlaySound ((MAKEINTRESOURCE) IDW_BAD, hInstance, SND_ASYNC | SND_RESOURCE);

    Make sure you #include the script rc file containing the sounds.

    If it still does not play check the return from the PlaySound() with GetLastError()
    I have checked and this plays.
    I find cancelling any currently playing sound helps

    ie
    PlaySound(NULL,NULL,SND_FILENAME);//stop any sound currently playing
    //now call your sound
    "Man alone suffers so excruciatingly in the world that he was compelled to invent laughter."
    Friedrich Nietzsche

    "I spent a lot of my money on booze, birds and fast cars......the rest I squandered."
    George Best

    "If you are going through hell....keep going."
    Winston Churchill

  6. #6
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    Also, if you use the SND_ASYNC flag, make sure your program does not exit before the sound is played!

  7. #7
    Digga
    Join Date
    May 2005
    Location
    Colorado
    Posts
    15

    I got it to work!

    I got it to work very well.

    Thanx

    --Digga

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. DirectX Question
    By bladerunner627 in forum Game Programming
    Replies: 2
    Last Post: 04-04-2005, 11:55 AM
  2. Low latency sound effects
    By VirtualAce in forum Game Programming
    Replies: 0
    Last Post: 12-21-2004, 01:58 AM
  3. Updated sound engine code
    By VirtualAce in forum Game Programming
    Replies: 8
    Last Post: 11-18-2004, 12:38 PM
  4. Gui Class With Tic Tac Toe
    By xxYukoxx in forum C++ Programming
    Replies: 1
    Last Post: 06-01-2003, 04:28 PM
  5. sounds?
    By BODYBUILDNERD in forum C++ Programming
    Replies: 6
    Last Post: 12-06-2002, 03:34 PM