Thread: PlaySound doesn't play when called!

  1. #1
    Registered User Queatrix's Avatar
    Join Date
    Apr 2005
    Posts
    1,342

    Thumbs down PlaySound doesn't play when called!

    When (WM_COMMAND,1,0) is called it won't play my ambient like I want, instead it will play the last sound I asked it to once I close my window.

    Code:
    #include <windows.h>
    #include <commctrl.h>
    #include <mmsystem.h>
    /*  Declare Windows procedure  */
    LRESULT CALLBACK WindowProcedure (HWND, UINT, WPARAM, LPARAM);
    /*  Make the class name into a global variable  */
    char szClassName[ ] = "AmbientWindow";
    /*  This is the main function where the stuff happens  */
    int WINAPI WinMain (HINSTANCE hThisInstance,
    					HINSTANCE hPrevInstance,
    					LPSTR lpszArgument,
    					int nFunsterStil)
    {
    	/* This purge loads all the sounds into memory */
    	PlaySound("", hThisInstance, SND_PURGE);
    	HWND hwnd;			   /* This is the handle for our window */
    	MSG messages;			/* Here messages to the application are saved */
    	WNDCLASSEX wincl;		/* Data structure for the windowclass */
    	InitCommonControls();
    	/* The Window structure */
    	wincl.hInstance = hThisInstance;
    	wincl.lpszClassName = szClassName;
    	wincl.lpfnWndProc = WindowProcedure;	  /* This function is called by windows */
    	wincl.style = CS_DBLCLKS;				 /* Catch double-clicks */
    	wincl.cbSize = sizeof (WNDCLASSEX);
    	/* Use default icon and mouse-pointer */
    	wincl.hIcon = LoadIcon(hThisInstance, "A");
    	wincl.hIconSm = LoadIcon(hThisInstance, "A");
    	wincl.hCursor = LoadCursor (NULL, IDC_ARROW);
    	wincl.lpszMenuName = NULL;				 /* No menu */
    	wincl.cbClsExtra = 0;					  /* No extra bytes after the window class */
    	wincl.cbWndExtra = 0;					  /* structure or the window instance */
    	/* Use Windows's default color as the background of the window */
    	wincl.hbrBackground = (HBRUSH) COLOR_BACKGROUND;
    	/* Register the window class, and if it fails quit the program */
    	if (!RegisterClassEx (&wincl))
    		return 0;
    	int winX = 434;
    	int winY = 265;
    	/* The class is registered, let's create the program*/
    	hwnd = CreateWindowEx (
    		   0,				   /* Extended possibilites for variation */
    		   szClassName,		 /* Classname */
    		   "Ambient",	   /* Title Text */
    		   WS_OVERLAPPEDWINDOW&~WS_THICKFRAME&~WS_MAXIMIZEBOX,
    		   GetSystemMetrics(SM_CXSCREEN)/2-winX/2,	   /* Windows decides the position */
    		   GetSystemMetrics(SM_CYSCREEN)/2-winY/2,	   /* where the window ends up on the screen */
    		   winX,				 /* The programs width */
    		   winY,				 /* and height in pixels */
    		   HWND_DESKTOP,		/* The window is a child-window to desktop */
    		   NULL,				/* No menu */
    		   hThisInstance,	   /* Program Instance handler */
    		   NULL				 /* No Window Creation data */
    		   );
    	/* Make the window visible on the screen */
    	ShowWindow (hwnd, nFunsterStil);
    	/* Run the message loop. It will run until GetMessage() returns 0 */
    	while (GetMessage (&messages, NULL, 0, 0))
    	{
    		/* Translate virtual-key messages into character messages */
    		TranslateMessage(&messages);
    		/* Send message to WindowProcedure */
    		DispatchMessage(&messages);
    	}
    	/* The program return-value is 0 - The value that PostQuitMessage() gave */
    	return messages.wParam;
    }
    /*  This function is called by the Windows function DispatchMessage()  */
    LRESULT CALLBACK WindowProcedure (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
    {
    	switch (message)				  /* handle the messages */
    	{
    		case WM_CREATE:
    		{
    			char spAddStrings[][99] = { { "White" },
    										{ "B" },
    										{ "h" },
    										{ "h" },
    										{ "!" } };
    			int i;
    			CreateWindowEx(WS_EX_CLIENTEDGE, WC_LISTBOX, "Hello", WS_CHILD | WS_VISIBLE | TVS_HASLINES | WS_VSCROLL, 10, 10, 180, 200, hwnd, (HMENU)1, GetModuleHandle(NULL), NULL);
    			SendDlgItemMessage(hwnd, 1, WM_SETFONT, (WPARAM)GetStockObject(DEFAULT_GUI_FONT), MAKELPARAM(TRUE, 0));
    			for(i = 0; spAddStrings[i][0] != '\0'; i++)
    			SendMessage(GetDlgItem(hwnd,1), LB_ADDSTRING, 0,(LPARAM)spAddStrings[i]);
    		}
    		break;
    		   
    	  case WM_COMMAND:
    	  {
    		 switch(LOWORD(wParam))
    		 {
    			case 1: 
    			{
    				int nSelection = SendMessage(GetDlgItem(hwnd,1), LB_GETCURSEL, (WPARAM)0, (LPARAM)0);  
    				char szSelection[MAX_PATH];
    				ZeroMemory(szSelection,MAX_PATH);
    				wsprintf(szSelection,"AMBIENT%d",nSelection);
    				PlaySound(szSelection, hThisInstance, SND_SYNC | SND_LOOP);
    				return 0;
    			}
    			break;
    		 }	 
    		 break;
    	  }
    	  break;
    	  case WM_DESTROY:
    	  {
    		 /* This purge loads just stops all sounds before quiting */
    		 PlaySound("", NULL, SND_PURGE);
    		 PostQuitMessage(0);
    	  }
    	  break;
    	  default:
    	  return DefWindowProc(hwnd, message, wParam, lParam);
    	}
    	return 0;
    }
    I have tried some other combanations of code just to make it worse. And I still haven't got it. How can I get PlaySound() to play when I want it too?

    Thanks for any help, August.

  2. #2
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,868
    Looks like your sounds are resources, so add the SND_RESOURCE flag.

    As you have not specified resource, file or alias, PlaySound() will look for an alias (system event sound ie critical stop), if not found it thinks szSelection is a file name. The file name will not be valid as it has no path or extension hence no 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

  3. #3
    Registered User Queatrix's Avatar
    Join Date
    Apr 2005
    Posts
    1,342
    I added it, but it still doesn't play. But as I said before it DOES play the last sound I wanted it to play when I close the window.

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

    You have an extra break in the WM_COMMAND....

    Where are you setting hThisInstance?

    Have you tried hard coding a valid sound into the szSelection string and seeing what plays?

    Tried a break point in the case 1: hander? (should be using higher numbers fro your resources as the compiler/IDE can allocate numbers under 40,000 and so cause errors)

    Have you put a break point into the callback and seen where the code jumps/ what gets called?

    Checked for API failed calls and used GetLastError()?
    "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

  5. #5
    Registered User Queatrix's Avatar
    Join Date
    Apr 2005
    Posts
    1,342
    >> Where are you setting hThisInstance?

    It looks like I messed up moving the code in:
    I have it like this:
    Code:
    HINSTANCE hGlblInstance;
    
    ...
    
    InitCommonControls();
    /* The Window structure */
    hGlblInstance = hThisInstance;
    wincl.hInstance = hGlblInstance;
    
    ...
    
    // NOT "PlaySound(szSelection, hThisInstance, SND_SYNC | SND_LOOP);" THIS:
    PlaySound(szSelection, hGlblInstance, SND_SYNC | SND_LOOP);
    But my instance isn't the problem, it DOES play, just not when I wan't it to.

    >> Have you tried hard coding a valid sound into the szSelection string and seeing what plays?

    Yes, and it does the same thing.

    >> Tried a break point in the case 1: hander? (should be using higher numbers fro your resources as the compiler/IDE can allocate numbers under 40,000 and so cause errors)

    I tried using 40010 and it still won't work.

    >> Have you put a break point into the callback and seen where the code jumps/ what gets called?

    Yes and it does reach PlaySound().

    >> Checked for API failed calls and used GetLastError()?

    PlaySound doesn't return any error.

    I'm still at a loss here, please help.

  6. #6
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,868
    Remove the PURGE calls.

    Put a .wav file in the project folder (working directory)

    Hard code it into the szSelection

    ie
    sprintf(szSelection,".\SomeSound.wav");

    call
    Code:
    //cancel sound
    int iReturn = PlaySound(NULL, NULL, SND_FILENAME| SND_ASYNC);
    //error check return
    iReturn = PlaySound(szSelection, NULL ,SND_FILENAME| SND_ASYNC);
    //error check return
    "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

  7. #7
    Registered User
    Join Date
    Feb 2006
    Posts
    71

    help

    I've been trying to make a program play sound =/ but i get a linker error and an exit 1 error with this as my playsound function:
    Code:
        PlaySound("c:\\documents and settings\\...\\shared\\Battlefeild.mp3",(HMODULE)HMOD,SND_LOOP);
    is HMOD just a handle for the song thats going to be played? and can it be any format? or does it have to be a .wav =/

  8. #8
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,868
    You could always RTFM...

    MSDN
    HMOD "This parameter must be NULL unless SND_RESOURCE is specified in fdwSound. "

    If using a file make sure you use the SND_FILENAME flag (and have spelled the path correctly (BattleField or BattleFeild?)

    Make sure you have linked to Winmm.lib and included windows.h

    AFAIK it has to be a .wav file

    ie

    iReturn = PlaySound("c:\\documents and settings\\...\\shared\\Battlefeild.mp3",NULL,SND_L OOP | SND_RESOURCE | SND_ASYNC);
    "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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. PlaySound() laggage...
    By cobrasniper555 in forum Windows Programming
    Replies: 1
    Last Post: 07-22-2006, 12:43 AM
  2. Replies: 6
    Last Post: 04-21-2006, 08:49 PM
  3. How to play sound from a resource?
    By Digga in forum Windows Programming
    Replies: 6
    Last Post: 06-08-2005, 06:35 PM
  4. PlaySound function
    By GaPe in forum Windows Programming
    Replies: 8
    Last Post: 10-20-2003, 06:20 PM
  5. Gui Class With Tic Tac Toe
    By xxYukoxx in forum C++ Programming
    Replies: 1
    Last Post: 06-01-2003, 04:28 PM