How to Play Sound??

Hi there all,

I am beginning my education in C++ and DirectX, and have developed my first Pong Game. It's in Direct3D, I managed to build it from scratch, no tutorials or anything (Other than the direct3d standard triangle tutorials). So now I have a working D3D Pong like game that works great. The only problem is I want a little .wav to play when the ball touches the paddles. For over a week now I have been pouring over code after code, tutorial after tutorial (including each directsound tutorial on this site) and nothing will work. All of them pretty much lead me to setup directMusic the same way, and when I do... I get an unhandled exception error when directsound init's. From what very very little I know about this error, I believe it is generated from a null value being passed. The error is as follows:

Unhandled exception at 0x00411472 in soundtrials.exe: 0xC0000005: Access violation reading location 0x00000000.

-g_pPerformance 0x00000000 IDirectMusicPerformance8 *
-IDirectMusicPerformance {...} IDirectMusicPerformance
-IUnknown {...} IUnknown
__vfptr CXX0030: Error: expression cannot be evaluated

I made a seperate project to simplify my trial and error of the sound war. I have copied the code verbatim from the Microsoft website. A message box is telling me that CoCreateInstance() is not initializing my DirectMusicPerformance, but I don't have any idea how to fix it... ::screams:: Here is the code:

#define INITGUID
#include <dmusici.h>


IDirectMusicLoader8* g_pLoader      = NULL;
IDirectMusicPerformance8* g_pPerformance = NULL;
IDirectMusicSegment8*  g_pSegment    = NULL;


INT APIENTRY WinMain( HINSTANCE hInst, HINSTANCE hPrevInst, 
 LPSTR pCmdLine, INT nCmdShow )
{
 CoInitialize(NULL);
 
 CoCreateInstance(CLSID_DirectMusicLoader, NULL, 
    CLSCTX_INPROC, IID_IDirectMusicLoader8,
    (void**)&g_pLoader);

 CoCreateInstance(CLSID_DirectMusicPerformance, NULL,
    CLSCTX_INPROC, IID_IDirectMusicPerformance8,
    (void**)&g_pPerformance );

if (!g_pPerformance) // This will always execute when I compile... so NULL pointer is here.

{

MessageBox(NULL, "Could not create performance!!", "Debug Test", 48);

return 0;

}

g_pPerformance->InitAudio( NULL, // IDirectMusic interface not needed. NULL, // IDirectSound interface not needed. NULL, // Window handle. DMUS_APATH_SHARED_STEREOPLUSREVERB, // Default audiopath type. 64, // Number of performance channels. DMUS_AUDIOF_ALL, // Features on synthesizer. NULL // Audio parameters; use defaults. ); // Find the Windows media directory. CHAR strPath[512]; if( GetWindowsDirectory( strPath, MAX_PATH+1 ) == 0 ) return 0; strcat( strPath, "\\media" ); // Convert to Unicode. WCHAR wstrSearchPath[MAX_PATH + 1]; MultiByteToWideChar( CP_ACP, 0, strPath, -1, wstrSearchPath, MAX_PATH ); wstrSearchPath[MAX_PATH] = 0; // Set the search directory. g_pLoader->SetSearchDirectory( GUID_DirectMusicAllTypes, // Types of files sought. wstrSearchPath, // Where to look. FALSE // Don't clear object data. ); WCHAR wstrFileName[MAX_PATH] = L"ding.wav"; if (FAILED(g_pLoader->LoadObjectFromFile( CLSID_DirectMusicSegment, // Class identifier. IID_IDirectMusicSegment8, // ID of desired interface. wstrFileName, // Filename. (LPVOID*) &g_pSegment // Pointer that receives interface. ))) { MessageBox( NULL, "Media not found, sample will now quit.", "DirectMusic Tutorial", MB_OK ); g_pPerformance->CloseDown(); g_pLoader->Release(); g_pPerformance->Release(); CoUninitialize(); return 0; } g_pSegment->Download( g_pPerformance ); g_pPerformance->PlaySegmentEx( g_pSegment, // Segment to play. NULL, // Not used. NULL, // For transitions. 0, // Flags. 0, // Start time; 0 is immediate. NULL, // Pointer that receives segment state. NULL, // Object to stop. NULL // Audiopath, if not default. ); MessageBox( NULL, "Click OK to Exit.", "Play Audio", MB_OK ); g_pPerformance->Stop( NULL, // Stop all segments. NULL, // Stop all segment states. 0, // Do it immediately. 0 // Flags. ); g_pSegment->Unload(g_pPerformance); g_pPerformance->CloseDown(); g_pLoader->Release(); g_pPerformance->Release(); g_pSegment->Release(); CoUninitialize(); return 0; // Return value for WinMain. } // End of WinMain.



Can anyone PLEASE help me with this... It's driving me crazy! :) Alternatively, if there is some easier way to play sound in a DirectX app, I would love to hear it.. I haven't found any tutorials on DirectSound that make any sense, and from what I have heard, DirectSound won't play a .Wav if you use C++ (only in C#)... I wanted to try XACT but I am running Vista, so that's out... so I need either this to work, or something else.

PS> I do NOT want to use a wrapper like FMOD or similiar, because I want to know how to do this... if they can do it.. I can do it lol.

Thanks so much in advance,

cheers ~

Az




Answer this question

How to Play Sound??

  • Varx

    Gee that's too much... Thanks!

  • nikos_22

    aka Andr3w Grammenos wrote:

    Next (I hope there won't be any, but unfortunatly it's highly unlikely) ask here for an answer first then search! It may be a lifesaver!


    http://catb.org/~esr/faqs/smart-questions.html
    (read the "Before You Ask" section)...


  • Dvlnblk

    The easiest API that works is:
    PlaySound("ball.wav",NULL,SND_ASYNC|SND_FILENAME);

    And you don't need any of the DSound/DMusic hassles. All in a single clean Win32 API call. If you're only going to play a sound every once in a while, then I highly recommend this.
    However, if you intend to have several sounds playing together with more control on playback options, then I'd go for DSound (not DMusic). The raw DSound API is easy but of course not as easy as PlaySound(). The SDK samples have a nifty sound management class based on DSound that can load/playback wave files easily and quickly. Look CSoundManager in the DirectSound sample apps...

    The only product I'm aware of that used DMusic was Final Fantasy 8... I played with it personally for a while, but never found myself in need of its functionality (I mean DMusic of course, not FF8 :P).


  • r3n

    WOW!!

    After countless hours of research and asking on Expert Exchange, GameDev.net, here on MSDN, and nothing... here it is... simple and sweet.

    Thank you soooo very much! I honestly cannot express enough how happy I am to finally have this under my belt. You are a lifesaver. Thank you thank you thank you. If you had any idea the mental anguish I've gone through trying to do just this... you'd understand my sincerest of gratitude.

    Thank you again and CHEERS!

    Az



  • stswordman

    Heh, I found this and I felt to post... It's very kind of you to say a work of "Thank You" in order to give the contributors a hand in order to keep working to make these forums and communities better! I guess it's awesome! But you must know what they said, that forums are usually the fastest way to get support! Next (I hope there won't be any, but unfortunatly it's highly unlikely) ask here for an answer first then search! It may be a lifesaver!

    Kindly,

    ~a.ka. Andr3w


  • How to Play Sound??