• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
BekkiBlack

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

22 posts in this topic

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

[code]
#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;
}
[/code]

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

Share this post


Link to post
Share on other sites
[quote name='BB1995' timestamp='1301796069' post='4793669']
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.....

[code]
#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;
}
[/code]

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.
[/quote]

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

Share this post


Link to post
Share on other sites
if you want a GOOD FREE OpenSource library to play mp3/wav/ogg/flac then try CAudio [url="http://sourceforge.net/projects/caudio/"]here[/url]
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
0

Share this post


Link to post
Share on other sites
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 - [url="http://sourceforge.net/projects/caudio/"]CAudio[/url]
or use [url="http://www.fmod.org/index.php/download#FMODExProgrammersAPI"]FMOD[/url] , which is superb, but if you are planning to sell your game you have to pay for license.
0

Share this post


Link to post
Share on other sites
I tried different .wav files, changed the filename, used WM_KEYDOWN and it still doesn't work.

What could be the problem??
0

Share this post


Link to post
Share on other sites
By the way did you take this source code from tutorial or something?
Because playing sound with DirectSound is a lot more complicated:
[url="http://www.rastertek.com/dx10tut14.html"]tut[/url]

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.
[color="#1C2837"][size="2"][color="#666600"][font="CourierNew, monospace"] [/font][/color][/size][/color]
0

Share this post


Link to post
Share on other sites
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)
0

Share this post


Link to post
Share on other sites
[quote name='Endurion' timestamp='1301920821' post='4794173']
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)
[/quote]

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

Share this post


Link to post
Share on other sites
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.
0

Share this post


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

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

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


after that it's just
[code]
cAudio::IAudioManager* manager;
cAudio::IAudioSource *jumpS;
manager = cAudio::createAudioManager(true);
jumpS=manager->create("test1","Res/Audio/jump.wav",true);
[/code]
that's it
0

Share this post


Link to post
Share on other sites
[quote][color="#1C2837"][size="2"]Does the function fail? If so, try checking the result of GetLastError().[/size][/color][/quote]

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

Share this post


Link to post
Share on other sites
[quote][color="#1C2837"][size="2"]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
[source] cAudio::IAudioManager* manager; cAudio::IAudioSource *jumpS; manager = cAudio::createAudioManager(true); jumpS=manager->create("test1","Res/Audio/jump.wav",true); [/source]
that's it [/size][/color][/quote]

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:
[quote][color="#1C2837"][size="2"]Also in your code you never initialize any sound system, which is like to wanna play dvd, but without dvd player :D[/size][/color][/quote]

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

Share this post


Link to post
Share on other sites
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.
0

Share this post


Link to post
Share on other sites
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:
[url="http://en.wikipedia.org/wiki/FMOD"]Link[/url] , including crysis zomg.

End this topic already!
0

Share this post


Link to post
Share on other sites
[quote name='Illuminated' timestamp='1302011719' post='4794626']
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:
[url="http://en.wikipedia.org/wiki/FMOD"]Link[/url] , including crysis zomg.

End this topic already!
[/quote]

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

Share this post


Link to post
Share on other sites
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.
-2

Share this post


Link to post
Share on other sites
[quote name='Illuminated' timestamp='1302080195' post='4794957']
no worries. Will provide code today :)
[/quote]

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

Share this post


Link to post
Share on other sites
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
1

Share this post


Link to post
Share on other sites
[quote name='ankhd' timestamp='1302146633' post='4795330']
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
[/quote]

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

Share this post


Link to post
Share on other sites
Hi again Just tryed it, in my app and it worked for me.

Try calling

[size="2"][color="#008000"][size="2"][color="#008000"]// Init COM so we can use CoCreateInstance

[/color][/size][/color][/size][size="2"] CoInitializeEx(NULL, COINIT_MULTITHREADED);





and when you close the app you need to call this must do it or else...

[size="2"][color="#008000"][size="2"][color="#008000"]

// Cleanup COM[/color][/size][/color][/size][size="2"]

CoUninitialize();

[/size][/size]
0

Share this post


Link to post
Share on other sites
[quote name='BB1995' timestamp='1302058323' post='4794871']
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.
[/quote]On behalf of entire Gamedev, I'd like to apologize for not meeting your expectation of getting [b]FREE [/b]help from us. To improve our response and serve [b]YOU [/b]better in the future, we'll provide Pay-Pal account for your to deposit funds to get prefered treatment.
0

Share this post


Link to post
Share on other sites
[quote]On behalf of entire Gamedev, I'd like to apologize for not meeting your expectation of getting [b]FREE [/b]help from us. To improve our response and serve [b]YOU [/b]better in the future, we'll provide Pay-Pal account for your to deposit funds to get prefered treatment.
[/quote]

K-I-S-S M-Y A-S-S.
-3

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0