Using SndPlaySound to Play Wave Files

Last reviewed: August 17, 1995
Article ID: Q133064
The information in this article applies to:
  • Microsoft Windows Software Development Kit (SDK), version 3.1
  • Microsoft Win32 Software Development Kit (SDK), version 3.5

The sndPlaySound() multimedia API function is capable of playing memory images using the SND_MEMORY flag if the waveform audio exists as a .WAV file, a wave resource, or is dynamically constructed by the application.

When using the SND_MEMORY flag, the data must be allocated from global memory using the GPTR and GMEM_SHARED flags and possess both the RIFF and PCM information.

The following code demonstrates how to use the SND_MEMORY flag.

   {  // Sample code to demonstrate SND_MEMORY flag and memory files.

      // Sound resource bound within executable.

      if (hWaveRes = FindResource(ghInst,"TADA","WAVE"))

         {   // Resource intact; load into GLOBAL MEMORY/GMEM_SHARED memory

            if (hGlobMem = LoadResource(ghInst,hWaveRes))

               {  // Load resource into global memory and play.

                  // Play sound resource via sndPlaySound() using
                  //  SND_MEMORY flag.

                  // Application waits until sndPlaySound completes
                  //  given SND_SYNC.

                  // SND_MEMORY (first parameter is ptr to memory image
                  //  vs. filename).

                  sndPlaySound((LPSTR)LockResource(hGlobMem),
                                                   SND_SYNC | SND_MEMORY);

                  FreeResource(hGlobMem);  // Required in 16-bit
                                           //  applications.


               }  // Load resource into global memory and play.

               else MessageBox(NULL,"No resource!","Multimedia Sampler!",
                               MB_ICONHAND);

            GlobalFree(hGlobMem);

         }  // Resource found.

         else MessageBox(NULL,"Lost resource!","Multimedia Sampler!",
                         MB_ICONHAND);

   }


Additional reference words: 3.10 3.50 kbinf win16sdk mmio mmioOpen mmioRead
KBCategory: kbmm kbcode
KBSubcategory: MMWave



THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

Last reviewed: August 17, 1995
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.