Jump to content

  • Log In with Google      Sign In   
  • Create Account

Windows API "Playsound" not working.......


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
22 replies to this topic

#1 BB1995   Members   -  Reputation: -41

Like
0Likes
Like

Posted 02 April 2011 - 08:01 PM

I linked winMM, it compiles, but it doesn't play the target file.

I included a .wav file to the project, made a function declaration for the filename and such, plus flags, included mmsystem.h and used a wParam switch statement under WM_KEYUP case label with a VK_ESCAPE function so when you press escape it will call on the PlaySound function but it just makes an error noise when you play it and fails.

Here's the exact code I'm using.....

#include <d3d9.h>
#include <mmsystem.h>
BOOL PlaySound(LPCSTR FILE, HMODULE FLAG, DWORD OTHER);
LPDIRECT3D9  g_pD3D   	= NULL;
LPDIRECT3DDEVICE9 g_pD3DDevice = NULL;
HRESULT InitialiseD3D(HWND hWnd)
{
  // First of all, create the main D3D object. If it is created successfully
  // we should get a pointer to an IDirect3D8 interface.
  g_pD3D = Direct3DCreate9(D3D_SDK_VERSION);

  if(g_pD3D == NULL)
  {
    return E_FAIL;
  }

  //Get the current display mode
  D3DDISPLAYMODE d3ddm;
  if(FAILED(g_pD3D->GetAdapterDisplayMode(D3DADAPTER_DEFAULT, &d3ddm)))
  {
    return E_FAIL;
  }

  // Create a structure to hold the settings for our device
  D3DPRESENT_PARAMETERS d3dpp;
  ZeroMemory(&d3dpp, sizeof(d3dpp));

  // Fill the structure: Program shall be windowed,
  // back buffer format matches current display mode
  d3dpp.Windowed 		= TRUE;
  d3dpp.SwapEffect   	= D3DSWAPEFFECT_DISCARD;
  d3dpp.BackBufferFormat = d3ddm.Format;

  //Create a Direct3D device.
  if(FAILED(g_pD3D->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hWnd,
            D3DCREATE_SOFTWARE_VERTEXPROCESSING, &d3dpp, &g_pD3DDevice)))
  {
    return E_FAIL;
  }

  return S_OK;
}

void Render()
{
  if(g_pD3DDevice == NULL)
  {
    return;
  }

  // Clear the backbuffer to blue
  g_pD3DDevice->Clear(0, NULL, D3DCLEAR_TARGET,
                      D3DCOLOR_XRGB(0, 0, 255), 1.0f, 0);

  // Begin the scene
  g_pD3DDevice->BeginScene();

  // Fill in here the rendering of other objects

  // End the scene
  g_pD3DDevice->EndScene();

  // Fill back and front buffers so that back buffer will be visible on screen
  g_pD3DDevice->Present(NULL, NULL, NULL, NULL);
}

void CleanUp()
{
  if(g_pD3DDevice != NULL)
  {
    g_pD3DDevice->Release();
    g_pD3DDevice = NULL;
  }

  if(g_pD3D != NULL)
  {
    g_pD3D->Release();
    g_pD3D = NULL;
  }
}

void MainLoop()
{
  // Enter the main loop
  MSG  msg;
  BOOL bMessage;

  PeekMessage(&msg, NULL, 0U, 0U, PM_NOREMOVE);

  while(msg.message != WM_QUIT)
  {
    bMessage = PeekMessage(&msg, NULL, 0U, 0U, PM_REMOVE);

    if(bMessage)
    {
      // Process message
      TranslateMessage(&msg);
      DispatchMessage(&msg);
    }
    else
    {
      Render(); // No message to process -> render the scene
    }
  }// while
}

// The windows message handler
LRESULT WINAPI WinProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
  switch(msg)
  {
    case WM_DESTROY:
      PostQuitMessage(0);
      return 0;
      break;

    case WM_KEYUP:
      switch (wParam)
      {
        case VK_ESCAPE:
          // Escape key pressed -> PlaySound should return true.
          PlaySound("blah.wav", NULL, SND_FILENAME);
          break;
      }
      break;
  }// switch

  return DefWindowProc(hWnd, msg, wParam, lParam);
}

// Application main entry point
INT WINAPI WinMain(HINSTANCE hInst, HINSTANCE, LPSTR, INT)
{
  //Register the window class
  WNDCLASSEX wc = { sizeof(WNDCLASSEX), CS_CLASSDC, WinProc, 0L, 0L,
                    GetModuleHandle(NULL), NULL, NULL, NULL, NULL,
                    "DirectX Project", NULL };
  RegisterClassEx(&wc);

  // Create the application's main window
  HWND hWnd = CreateWindow("DirectX Project of mine", "MY project",
               			WS_OVERLAPPEDWINDOW, 50, 50, 500, 500,
               			GetDesktopWindow(), NULL, wc.hInstance, NULL);

  // Initialize Direct3D
  if(SUCCEEDED(InitialiseD3D(hWnd)))
  {
    // Show window
    ShowWindow(hWnd, SW_SHOWDEFAULT);
    UpdateWindow(hWnd);

    //Start game running: Enter the game loop
    MainLoop();
  }

  CleanUp();

  UnregisterClass("DirectX Project", wc.hInstance);

  return 0;
}

Anyone have an idea what the issue could be, besides DirectX's interference?

Also, it's not the fact that I'm not using <windows.h>. DirectX's <d3d9.h> includes it itself, so it's not that problem.

Sponsor:

#2 BB1995   Members   -  Reputation: -41

Like
0Likes
Like

Posted 02 April 2011 - 11:14 PM

I linked winMM, it compiles, but it doesn't play the target file.

I included a .wav file to the project, made a function declaration for the filename and such, plus flags, included mmsystem.h and used a wParam switch statement under WM_KEYUP case label with a VK_ESCAPE function so when you press escape it will call on the PlaySound function but it just makes an error noise when you play it and fails.

Here's the exact code I'm using.....

#include <d3d9.h>
#include <mmsystem.h>
BOOL PlaySound(LPCSTR FILE, HMODULE FLAG, DWORD OTHER);
LPDIRECT3D9  g_pD3D   	= NULL;
LPDIRECT3DDEVICE9 g_pD3DDevice = NULL;
HRESULT InitialiseD3D(HWND hWnd)
{
  // First of all, create the main D3D object. If it is created successfully
  // we should get a pointer to an IDirect3D8 interface.
  g_pD3D = Direct3DCreate9(D3D_SDK_VERSION);

  if(g_pD3D == NULL)
  {
    return E_FAIL;
  }

  //Get the current display mode
  D3DDISPLAYMODE d3ddm;
  if(FAILED(g_pD3D->GetAdapterDisplayMode(D3DADAPTER_DEFAULT, &d3ddm)))
  {
    return E_FAIL;
  }

  // Create a structure to hold the settings for our device
  D3DPRESENT_PARAMETERS d3dpp;
  ZeroMemory(&d3dpp, sizeof(d3dpp));

  // Fill the structure: Program shall be windowed,
  // back buffer format matches current display mode
  d3dpp.Windowed 		= TRUE;
  d3dpp.SwapEffect   	= D3DSWAPEFFECT_DISCARD;
  d3dpp.BackBufferFormat = d3ddm.Format;

  //Create a Direct3D device.
  if(FAILED(g_pD3D->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hWnd,
            D3DCREATE_SOFTWARE_VERTEXPROCESSING, &d3dpp, &g_pD3DDevice)))
  {
    return E_FAIL;
  }

  return S_OK;
}

void Render()
{
  if(g_pD3DDevice == NULL)
  {
    return;
  }

  // Clear the backbuffer to blue
  g_pD3DDevice->Clear(0, NULL, D3DCLEAR_TARGET,
                      D3DCOLOR_XRGB(0, 0, 255), 1.0f, 0);

  // Begin the scene
  g_pD3DDevice->BeginScene();

  // Fill in here the rendering of other objects

  // End the scene
  g_pD3DDevice->EndScene();

  // Fill back and front buffers so that back buffer will be visible on screen
  g_pD3DDevice->Present(NULL, NULL, NULL, NULL);
}

void CleanUp()
{
  if(g_pD3DDevice != NULL)
  {
    g_pD3DDevice->Release();
    g_pD3DDevice = NULL;
  }

  if(g_pD3D != NULL)
  {
    g_pD3D->Release();
    g_pD3D = NULL;
  }
}

void MainLoop()
{
  // Enter the main loop
  MSG  msg;
  BOOL bMessage;

  PeekMessage(&msg, NULL, 0U, 0U, PM_NOREMOVE);

  while(msg.message != WM_QUIT)
  {
    bMessage = PeekMessage(&msg, NULL, 0U, 0U, PM_REMOVE);

    if(bMessage)
    {
      // Process message
      TranslateMessage(&msg);
      DispatchMessage(&msg);
    }
    else
    {
      Render(); // No message to process -> render the scene
    }
  }// while
}

// The windows message handler
LRESULT WINAPI WinProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
  switch(msg)
  {
    case WM_DESTROY:
      PostQuitMessage(0);
      return 0;
      break;

    case WM_KEYUP:
      switch (wParam)
      {
        case VK_ESCAPE:
          // Escape key pressed -> PlaySound should return true.
          PlaySound("blah.wav", NULL, SND_FILENAME);
          break;
      }
      break;
  }// switch

  return DefWindowProc(hWnd, msg, wParam, lParam);
}

// Application main entry point
INT WINAPI WinMain(HINSTANCE hInst, HINSTANCE, LPSTR, INT)
{
  //Register the window class
  WNDCLASSEX wc = { sizeof(WNDCLASSEX), CS_CLASSDC, WinProc, 0L, 0L,
                    GetModuleHandle(NULL), NULL, NULL, NULL, NULL,
                    "DirectX Project", NULL };
  RegisterClassEx(&wc);

  // Create the application's main window
  HWND hWnd = CreateWindow("DirectX Project of mine", "MY project",
           				WS_OVERLAPPEDWINDOW, 50, 50, 500, 500,
           				GetDesktopWindow(), NULL, wc.hInstance, NULL);

  // Initialize Direct3D
  if(SUCCEEDED(InitialiseD3D(hWnd)))
  {
    // Show window
    ShowWindow(hWnd, SW_SHOWDEFAULT);
    UpdateWindow(hWnd);

    //Start game running: Enter the game loop
    MainLoop();
  }

  CleanUp();

  UnregisterClass("DirectX Project", wc.hInstance);

  return 0;
}

Anyone have an idea what the issue could be, besides DirectX's interference?

Also, it's not the fact that I'm not using <windows.h>. DirectX's <d3d9.h> includes it itself, so it's not that problem.


Also, does anyone know the easiest way of playing sounds using DirectX? I chose Windows API with its own libraries because it seems 10x easier than with the confusion of DirectX.

#3 e3d_ALiVE   Members   -  Reputation: 205

Like
0Likes
Like

Posted 03 April 2011 - 01:17 AM

if you want a GOOD FREE OpenSource library to play mp3/wav/ogg/flac then try CAudio here
it already have all dlls and libs, so u don't need to compile any sources
it's based on popular crossplatform OpenAL and have no problem working with dx,
it also has tutorials and memory manager
aside from that it can also record sounds from mic, use special effects[reverb, echo, underwater] and stream audio.

now about your problem:
PlaySound("blah.wav", NULL, SND_FILENAME);
this functions returns true if it can play sound, you need check for that first
possible reasons for not playing:
application seeks that file in debug foler, while it's located somehwere else, try "../blah.wav"
it's wrong wav file[since that api can play only spiecific wav files] try different wav file, for example the file that goes with windows

Crazy dude smoking D3D11, AngelScript, PhysX, 3D Sound, Network, DB, FBX, and some other weird things, doing that on Visual Studio 2010 + Visual Assist X on Overclocked CPU

#4 Illuminated   Members   -  Reputation: 109

Like
0Likes
Like

Posted 03 April 2011 - 02:48 AM

If i am not wrong WM_KEYUP triggers the event when you relese the button( in this case Escape ).
You can try to switch to WM_KEYDOWN, which will trigger the events when you press the button.
All DirectX APIs for playing sound are deprecated, except XACT, which too is kinda hardcore, so you can switch to what e3d_ALiVE proposed - CAudio
or use FMOD , which is superb, but if you are planning to sell your game you have to pay for license.

#5 BB1995   Members   -  Reputation: -41

Like
0Likes
Like

Posted 03 April 2011 - 05:34 PM

I tried different .wav files, changed the filename, used WM_KEYDOWN and it still doesn't work.

What could be the problem??

#6 Illuminated   Members   -  Reputation: 109

Like
0Likes
Like

Posted 04 April 2011 - 12:14 AM

By the way did you take this source code from tutorial or something?
Because playing sound with DirectSound is a lot more complicated:
tut

Also in your code you never initialize any sound system, which is like to wanna play dvd, but without dvd player :D
Also why do you declare at top PlaySound? Isnt this function declared in mmsystem.h already?
Also i guarantee that with fmod you will play background sound in less then 1 hour.
Also i got source code for this if need help with fmod.
Zomg too much also.


#7 Endurion   Crossbones+   -  Reputation: 3573

Like
0Likes
Like

Posted 04 April 2011 - 06:40 AM

Try providing the absolute path to the .wav file. The current working directory might not be where you think it is (esp. when running from IDE)
Fruny: Ftagn! Ia! Ia! std::time_put_byname! Mglui naflftagn std::codecvt eY'ha-nthlei!,char,mbstate_t>

#8 BB1995   Members   -  Reputation: -41

Like
0Likes
Like

Posted 05 April 2011 - 01:24 AM

Try providing the absolute path to the .wav file. The current working directory might not be where you think it is (esp. when running from IDE)


I did provide the absolute path, put it in a search directory, changed the declaration around, everything. A tutorial claimed that you only need to declare a prototype function and then just call on the function and it plays a sound. That's it...But it doesn't work.

I tried everything I can think of. It compiles, everything works fine, but the sound won't play.

Also, I tried those other sound libraries....They were a pain. All they did was cause the compiler to endlessly complain about missing .dll files and it was just torture.

#9 Endurion   Crossbones+   -  Reputation: 3573

Like
0Likes
Like

Posted 05 April 2011 - 01:44 AM

Does the function fail? If so, try checking the result of GetLastError().

Another cause might be a invalid wav file. Just because one player can play it doesn't mean it's a proper file. Check if other players have no problem with the file as well. Sometimes just opening the wave in an audio editor and saving again fixes problems.
Fruny: Ftagn! Ia! Ia! std::time_put_byname! Mglui naflftagn std::codecvt eY'ha-nthlei!,char,mbstate_t>

#10 e3d_ALiVE   Members   -  Reputation: 205

Like
0Likes
Like

Posted 05 April 2011 - 01:45 AM

CAudio requres you to put only 2 dlls into your folder, caudio and mp3plugin, + you need to link it with lib file, like that

#pragma comment(lib,"caudio.lib")
and add that line #include <cAudio.h>

put caudio include files into your search paths or put lib file and caudio files into your source directory


after that it's just
cAudio::IAudioManager* manager;
cAudio::IAudioSource *jumpS;
manager = cAudio::createAudioManager(true);
jumpS=manager->create("test1","Res/Audio/jump.wav",true);
that's it
Crazy dude smoking D3D11, AngelScript, PhysX, 3D Sound, Network, DB, FBX, and some other weird things, doing that on Visual Studio 2010 + Visual Assist X on Overclocked CPU

#11 BB1995   Members   -  Reputation: -41

Like
0Likes
Like

Posted 05 April 2011 - 02:08 AM

Does the function fail? If so, try checking the result of GetLastError().


How exactly would I do that?

I'm not that great with Windows API function calls and such.

I don't know if it fails, but it sure fails to play the sound when I call on the function.

#12 BB1995   Members   -  Reputation: -41

Like
0Likes
Like

Posted 05 April 2011 - 02:15 AM

CAudio requres you to put only 2 dlls into your folder, caudio and mp3plugin, + you need to link it with lib file, like that

#pragma comment(lib,"caudio.lib")
and add that line #include <cAudio.h>

put caudio include files into your search paths or put lib file and caudio files into your source directory

after that it's just

cAudio::IAudioManager* manager; cAudio::IAudioSource *jumpS; manager = cAudio::createAudioManager(true); jumpS=manager->create("test1","Res/Audio/jump.wav",true);

that's it


Sounds like a lot of crap for just "that's it". >.< Also, dude, you put the slash the wrong way for the source code closing tag ...

Not that I can't do it, but I want to resolve the stupid Windows API sound issue before just moving on elsewhere.

EDIT: Someone mentioned before, also, this:

Also in your code you never initialize any sound system, which is like to wanna play dvd, but without dvd player :D


So basically, what is a sound system and what do I do about that then? The tutorial said nary a thing about such a thing.

#13 Endurion   Crossbones+   -  Reputation: 3573

Like
0Likes
Like

Posted 05 April 2011 - 07:39 AM

PlaySound returns FALSE when it fails.

GetLastError returns an DWORD, which you can put into the error lookup tool (On Extras in Visual Studio). This should give a hint on what went wrong.


Note that PlaySound is not really that useful for gaming. It only allows one sound playing concurrently and no control over volume, pan or other effects.
Fruny: Ftagn! Ia! Ia! std::time_put_byname! Mglui naflftagn std::codecvt eY'ha-nthlei!,char,mbstate_t>

#14 Illuminated   Members   -  Reputation: 109

Like
0Likes
Like

Posted 05 April 2011 - 07:55 AM

So basically there is a class where is all the stuff wrapped for playing sound, loading sound, managind sound and etc.
This is sound system and it must be initialized somewhere in your code. Otherwise you cant play anything.
Now if you are truly hardcore you can check out the example in DirectX Sample browser about xact.
If you are not i again recomment you to look at FMOD.
And if you still dont trust me here is link with all commercial games that uses fmod:
Link , including crysis zomg.

End this topic already!

#15 BB1995   Members   -  Reputation: -41

Like
0Likes
Like

Posted 05 April 2011 - 08:47 PM

So basically there is a class where is all the stuff wrapped for playing sound, loading sound, managind sound and etc.
This is sound system and it must be initialized somewhere in your code. Otherwise you cant play anything.
Now if you are truly hardcore you can check out the example in DirectX Sample browser about xact.
If you are not i again recomment you to look at FMOD.
And if you still dont trust me here is link with all commercial games that uses fmod:
Link , including crysis zomg.

End this topic already!


Okay, I downloaded FMOD because Windows API PlaySound continually fails no matter what.....How do I actually use it? It just installs a folder filled with tons of jibberish FOBJ files and I don't have any idea on what to do next.

I wanted something simple, like with installation as well....Downloading these APIs just makes it even harder because some are just so difficult to even compile/link because the files are disorganized and I have no idea on where to put them, what kind of files does what, etc.

That's why I tried PlaySound on Windows API, but it fails so forget it....

PS: Just so you know I read the documentation and the site and neither offers installation advice.

#16 BB1995   Members   -  Reputation: -41

Like
-2Likes
Like

Posted 05 April 2011 - 08:52 PM

To add, I'm also very angry and upset and unsatisfied at the help I've been getting here.

I provided the complete code, all-around info, and no help. I've been at this for 3 weeks now and still can't even hear a sound being played in a window, sadly....It really pisses me off and I'm tired of not getting any real help.

#17 Illuminated   Members   -  Reputation: 109

Like
0Likes
Like

Posted 06 April 2011 - 02:56 AM

no worries. Will provide code today :)

#18 BB1995   Members   -  Reputation: -41

Like
0Likes
Like

Posted 06 April 2011 - 08:40 PM

no worries. Will provide code today :)


Okay, I'll be waiting. :) :)

#19 ankhd   Members   -  Reputation: 1282

Like
1Likes
Like

Posted 06 April 2011 - 09:23 PM

Most waveform-audio files use the .WAV filename extension.

The following statement plays the C:\SOUNDS\BELLS.WAV file:

PlaySound("C:\\SOUNDS\\BELLS.WAV", NULL, SND_SYNC);
If the specified file does not exist, or if the file does not fit into the available memory, PlaySound plays the default system sound. If no default system sound has been defined, PlaySound fails without producing any sound. If you do not want the default system sound to play, specify the SND_NODEFAULT flag, as shown in the following example:

PlaySound("C:\\SOUNDS\\BELLS.WAV", NULL, SND_SYNC | SND_NODEFAULT);

Try changing the flags you should start with the system sound and see if it beeps. And also the wave file may be to big and it will fail.
I would go for xact

#20 BB1995   Members   -  Reputation: -41

Like
0Likes
Like

Posted 06 April 2011 - 10:02 PM

Most waveform-audio files use the .WAV filename extension.

The following statement plays the C:\SOUNDS\BELLS.WAV file:

PlaySound("C:\\SOUNDS\\BELLS.WAV", NULL, SND_SYNC);
If the specified file does not exist, or if the file does not fit into the available memory, PlaySound plays the default system sound. If no default system sound has been defined, PlaySound fails without producing any sound. If you do not want the default system sound to play, specify the SND_NODEFAULT flag, as shown in the following example:

PlaySound("C:\\SOUNDS\\BELLS.WAV", NULL, SND_SYNC | SND_NODEFAULT);

Try changing the flags you should start with the system sound and see if it beeps. And also the wave file may be to big and it will fail.
I would go for xact


I tried...It still doesn't work. It just eithers plays the default noise or no noise at all. And the .wav file I'm using is 1 second or like 4 bytes....It can't be too large.

I changed the flags around, changed the directories, added search directories, etc. It still doesn't play any .wav file and I tried over 16 of them.

And yes, the simple Beep(100,1000) function works, but PlaySound does not.




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS