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

Started by
13 comments, last by MENTAL 19 years, 1 month ago
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.
Advertisement
Use direct audio
how? can u help a bit more please!

cause im new to this.

thnx.
There's an article in reference section that covers the very basics.
[edit]
If you want to use background music and sound effects I'd rather suggest using some free audio library instead. A list is available here.
[/edit]

Good luck!
Pat.
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???

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
__________________________________[ Website ] [ Résumé ] [ [email=contact[at]matthughson[dot]com]Contact[/email] ][ Have I been Helpful? Hook me up! ]
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.
....[size="1"]Brent Gunning
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.


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: <br><br>void changeStage()<br> {<br> if (gamestage==1)<br> {<br> // Play Sound<br> }<br> …<br><br><br>Also heres the code ive written in the winMain:<br><br><br>int APIENTRY WinMain(HINSTANCE Now,HINSTANCE hPrevInst,LPSTR pCmdLine ,int nCmdShow)<br>{<br> AdventureGame.StartGame(Now);<br> <br> while(true)<br> {<br> if (!AdventureGame.NotFinished())<br> return exitgame;<br> AdventureGame.MainLoop();<br> FireBulletLoop();<br> }<br> return donotexit;<br><br>/////////////////////////////////////////////////////////////////////////<br><br> CoInitialize(NULL);<br> <br> CoCreateInstance(CLSID_DirectMusicLoader, NULL, <br> CLSCTX_INPROC, IID_IDirectMusicLoader8,<br> (void**)&g_pLoader);<br><br> CoCreateInstance(CLSID_DirectMusicPerformance, NULL,<br> CLSCTX_INPROC, IID_IDirectMusicPerformance8,<br> (void**)&g_pPerformance );<br>//////////////////////////////////////////////////////////////////////////<br> g_pPerformance-&gt;InitAudio( <br> NULL, // IDirectMusic interface not needed.<br> NULL, // IDirectSound interface not needed.<br> NULL, // Window handle.<br> DMUS_APATH_SHARED_STEREOPLUSREVERB, // Default audiopath type.<br> 64, // Number of performance channels.<br> DMUS_AUDIOF_ALL, // Features &#111;n synthesizer.<br> NULL // Audio parameters; use defaults.<br> );<br>/////////////////////////////////////////////////////////////////////////<br><br> // Find the Windows media directory.<br> CHAR Sounds[512];<br> if( GetWindowsDirectory( Sounds, MAX_PATH+1 ) == 0 )<br> return 0;<br> strcat( Sounds, "MGS2 Main Theme" );<br> <br> // Convert to Unicode.<br> <br> WCHAR wstrSearchPath[MAX_PATH + 1];<br> MultiByteToWideChar( CP_ACP, 0, Sounds, -1, <br> wstrSearchPath, MAX_PATH );<br> wstrSearchPath[MAX_PATH] = 0;<br><br> // Set the search directory.<br> <br> g_pLoader-&gt;SetSearchDirectory( <br> GUID_DirectMusicAllTypes, // Types of files sought.<br> wstrSearchPath, // Where to look.<br> FALSE // Don't clear object data.<br> );<br>////////////////////////////////////////////////////////////////////////<br><br>WCHAR wstrFileName[MAX_PATH] = L"MGS2 Main Theme.wav";<br> <br> if (FAILED(g_pLoader-&gt;LoadObjectFromFile(<br> CLSID_DirectMusicSegment, // Class identifier.<br> IID_IDirectMusicSegment8, // ID of desired interface.<br> wstrFileName, // Filename.<br> (LPVOID*) &g_pSegment // Pointer that receives interface.<br> )))<br> {<br> MessageBox( NULL, "Media not found, sample will now quit.", <br> "DirectMusic", MB_OK );<br> g_pPerformance-&gt;CloseDown();<br> g_pLoader-&gt;Release(); <br> g_pPerformance-&gt;Release();<br> CoUninitialize();<br> return 0;<br> }<br>///////////////////////////////////////////////////////////////////////////<br> g_pSegment-&gt;Download( g_pPerformance );<br>//////////////////////////////////////////////////////////////////////////<br><br> g_pPerformance-&gt;PlaySegmentEx(<br> g_pSegment, // Segment to play.<br> NULL, // Not used.<br> NULL, // For transitions. <br> 0, // Flags.<br> 0, // Start time; 0 is immediate.<br> NULL, // Pointer that receives segment state.<br> NULL, // Object to stop.<br> NULL // Audiopath, if not default.<br> ); <br> MessageBox( NULL, "Click OK to Exit.", "Play Audio", MB_OK );<br>////////////////////////////////////////////////////////////////////////////<br><br> g_pPerformance-&gt;Stop(<br> NULL, // Stop all segments.<br> NULL, // Stop all segment states.<br> 0, // Do it immediately.<br> 0 // Flags.<br> );<br> <br> g_pSegment-&gt;Unload(g_pPerformance);<br> g_pPerformance-&gt;CloseDown();<br> <br> g_pLoader-&gt;Release(); <br> g_pPerformance-&gt;Release();<br> g_pSegment-&gt;Release();<br> <br> CoUninitialize();<br> <br> return 0; // Return value for WinMain.<br>}// End of WinMain.<br><br><br><br>plz try to help me ASAP as my deadline for prototype is &#111;n Thursday!!!<br><br>Thank You very much!<br>
I'd just use FMOD, very quick, very simple...
No bombs, No guns, just an army of game creators...

This topic is closed to new replies.

Advertisement