Jump to content
  • Advertisement
Sign in to follow this  
Nads

Sound Help!! (C++) Urgent help required!

This topic is 4965 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi, im using C++ to make my game for my project with directx 9. and im having trouble with the sound in the game!! the code that i am currently using is: PlaySound("Sounds/Gun_44magnum.wav",NULL,SND_ASYNC); where "Sounds/Gun_44magnum.wav" is the FILENAME for the sound. Now wat actually happens is that i say when i load my level up with music and then my character shoots a bullet than the music stops playing as the gunshot sound is run. How can i stop the sound from overwriting the music? I need to set the music to the background so that nothing else interferes with it! can someone help me with it and tell me wat to do please!! as i need to hand this in real soon [couple of days]!! all help is greatly appreciated thnx.

Share this post


Link to post
Share on other sites
Advertisement
ahh man too tell u the truth it still doesnt asnwer my question!!

cause i want to use PlaySound() for the sound!!!

isnt there anyway to apply some code to that so that it never turns off even if another sound file is played???

Share this post


Link to post
Share on other sites
Not sure, but it sounds like only have one sound buffer or are always using the primary one. I belive you need a different sound buffer for every sound, or as you described, the only one that will play is the most resently started.

Matt Hughson

Share this post


Link to post
Share on other sites
Quote:
Original post by Nads
ahh man too tell u the truth it still doesnt asnwer my question!!

cause i want to use PlaySound() for the sound!!!

isnt there anyway to apply some code to that so that it never turns off even if another sound file is played???


I don't know.

On the other hand, why do you choose to you use PlaySound? I'll assume it's because of its simplicity. However, you may find that fmod is almost as easy and has more features than you would ever desire. DirectAudio is easy too, but it almost requires a wrapper library to be useful .. like most DirectX components. There's also BASS, but I do no know much about the library.

Most people will tell you not to use PlaySound because it accesses and reads the sound file every time you call the function, essentially making it slow.

Don't be discouraged if another route seems more difficult at first.

Share this post


Link to post
Share on other sites
If you read the documentation on PlaySound, it explains that only one sound can be played at a time. If you are using DirectX for the graphics, you can use it for the sound too. I use OpenGL for graphics and OpenAL for sound. FMOD and BASS are supposed to be pretty good. PlaySound is very slow cuz as said before, it loads the sound each time the function is called. The WIN32 API has functions to play MID files in the background, then you can use PlaySound over that, but I recommend the other libraries for the speed and simplicity, and in some cases 3d sound. You can't get that with WIN32.

Share this post


Link to post
Share on other sites
Hi people,

thnx alot for everyones sujjestions!

Ive done away with the PlaySound() as u lot said, and used the DirectMusic appraoch, so that i cant play two files simultaneously! EG. music with gunshots!

But ive gotten a little stuck! Ive written all the code and initialised everything in my winmain() and also initialised the sounds directory but i dont know to call the function to play the sound in my other file, cause i want to play it in my other file. [although it doesnt play anything at all at the moment!]

for example: [i want to play it here (which is in a different .cpp file]

void changeStage()
{
if (gamestage==1)
{
// Play Sound
}
...


Also heres the code ive written in the winMain:


int APIENTRY WinMain(HINSTANCE Now,HINSTANCE hPrevInst,LPSTR pCmdLine ,int nCmdShow)
{
AdventureGame.StartGame(Now);

while(true)
{
if (!AdventureGame.NotFinished())
return exitgame;
AdventureGame.MainLoop();
FireBulletLoop();
}
return donotexit;

/////////////////////////////////////////////////////////////////////////

CoInitialize(NULL);

CoCreateInstance(CLSID_DirectMusicLoader, NULL,
CLSCTX_INPROC, IID_IDirectMusicLoader8,
(void**)&g_pLoader);

CoCreateInstance(CLSID_DirectMusicPerformance, NULL,
CLSCTX_INPROC, IID_IDirectMusicPerformance8,
(void**)&g_pPerformance );
//////////////////////////////////////////////////////////////////////////
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 Sounds[512];
if( GetWindowsDirectory( Sounds, MAX_PATH+1 ) == 0 )
return 0;
strcat( Sounds, "MGS2 Main Theme" );

// Convert to Unicode.

WCHAR wstrSearchPath[MAX_PATH + 1];
MultiByteToWideChar( CP_ACP, 0, Sounds, -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"MGS2 Main Theme.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", 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.



plz try to help me ASAP as my deadline for prototype is on Thursday!!!

Thank You very much!

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!