469,568 Members | 1,570 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,568 developers. It's quick & easy.

Playsound() plays only a part of the song

3
I tried to play a wav file with function Playsound() but it played only the first seconds of the song.I would like to play whole the song.What might de be wrong?Here is my code in case someone can help.




Expand|Select|Wrap|Line Numbers
  1. /*
  2. This uses the high level WAVE API PlaySound() to play a WAVE file in memory. I simply
  3. open the WAVE file called "nagelas.wav" (ie, in order for this example to
  4. work, you should make sure that you have such a WAVE file on your system). allocate
  5. some memory to completely load the entire file image into memory, and then use that
  6. for PlaySound().
  7. */
  8.  
  9. #include <windows.h>
  10. #include <stdio.h>
  11. #include <conio.h>
  12.  
  13. #include <winreg.h>
  14. #include <mmsystem.h>
  15.  
  16.  
  17.  
  18. TCHAR        WaveName[] = "nagelas.wav";
  19.  
  20.  
  21.  
  22.  
  23.  
  24.  
  25.  
  26.  
  27.  
  28.  
  29. /********************************** main() *******************************/
  30.  
  31. int main(int argc, char **argv)
  32. {
  33.     HANDLE            inHandle;
  34.     char *            wavePtr;
  35.     unsigned long    waveSize, action;
  36.     char            ch;
  37.  
  38.     /* Open the WAVE file */
  39.     if (INVALID_HANDLE_VALUE != (inHandle = CreateFile("nagelas.wav", GENERIC_READ,
  40.         FILE_SHARE_READ, 0, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL|FILE_FLAG_SEQUENTIAL_SCAN, 0)))
  41.     {
  42.         /* I'm going to skip checking that this is a WAVE file. I'll assume that it is.
  43.          *    Normally, you'd check it's RIFF header.
  44.          */
  45.  
  46.         /* Get the size of the file */
  47.         if(0xFFFFFFFF != (waveSize = GetFileSize(inHandle, 0)) && waveSize)
  48.         {
  49.             /* Allocate some memory to load the file */
  50.             if ((wavePtr = (char *)VirtualAlloc(0, waveSize, MEM_COMMIT, PAGE_READWRITE)))
  51.             {
  52.                 /* Read in WAVE file */
  53.                 if (ReadFile(inHandle, wavePtr, waveSize, &action, 0) && waveSize == action)
  54.                 {
  55.                     /* Play the WAVE */
  56.                     if (!PlaySound((LPCSTR)wavePtr, 0, SND_MEMORY | SND_NODEFAULT))
  57.                     {
  58.                         printf("Error playing WAVE!\r\n");
  59.                     }
  60.                 }
  61.                 else
  62.                 {
  63.                     printf("Error loading WAVE!\r\n");
  64.                 }
  65.  
  66.                 /* Free the memory */
  67.                 VirtualFree(wavePtr, waveSize, MEM_FREE);
  68.             }
  69.             else
  70.             {
  71.                 printf("Can't get memory!\r\n");
  72.             }
  73.         }
  74.         else
  75.         {
  76.             printf("Bad WAVE size!\r\n");
  77.         }
  78.  
  79.         /* Close the WAVE file */
  80.         CloseHandle(inHandle);
  81.     }
  82.     else
  83.     {
  84.         printf("Can't load the WAVE file!\r\n");
  85.     }
  86.     scanf("%c",&ch);
  87.     return(0);
  88. }
Apr 17 '07 #1
0 1681

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

1 post views Thread by Ben Bateman | last post: by
19 posts views Thread by bballmitch | last post: by
2 posts views Thread by Joe Thompson | last post: by
10 posts views Thread by Ot | last post: by
3 posts views Thread by tamarindm | last post: by
4 posts views Thread by Cogito | last post: by
reply views Thread by suresh191 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.