c - PlaySound functions strange behavior -


i writing function in c play wav files. can play 1 time sound, want add loop option.

i have 2 modes of work:

  • play file name
  • play memory.

in both modes, cannot play sound more twice, after function crash.

note: solved adding code:

bool winapi playsound(lpcstr,hmodule,dword);

without problems.

my code:

#include <windows.h> #include <stdio.h>  void play(char * filename, int repeat); char* file2vector(char* filename, long int* size);  int main(int argc, char ** argv) {     if (argc > 1) {         play(argv[1], 5);     }  }   void play(char * filename, int repeat) { #define snd_sync 0 #define snd_async 1 #define snd_filename 0x20000 #define snd_nodefault 2 #define snd_memory 4 #define snd_nostop 16      int mode = snd_sync | snd_nodefault | snd_nostop;     char * sound;     int play = 1;     int i;      long int size;     unsigned char* wavfile = file2vector(filename, &size);      if (wavfile == null) {         mode |= snd_filename;         sound = filename;         printf("filename\n");     }     else {         mode |= snd_memory;         sound = wavfile;         printf("memory\n");      }       if (repeat) {         play += repeat;     }      printf("play %d times\n", play);      int res;     (i = 1; <= play; ++i) {         printf("played %i\n", i);         res = playsound(sound, null, mode);         printf("res:%d\n", res);     }      playsound(null, 0, 0);     free(wavfile);      printf("ready");   }  char* file2vector(char* filename, long int* size) {     char* vector = null;     file* file = fopen(filename, "rb");      if (null == file) {         *size = 0l;     }     else     {         fseek(file, 0l, seek_end);         *size = ftell(file);         fseek(file, 0l, seek_set);          /* ftell can return -1 on failure */         if (*size <= 0) {             *size = 0l;          }         else         {             vector = (char*)malloc(*size);             if (null != vector) {                 fread(vector, sizeof(char), *size, file);             }         }          fclose(file);     }      return vector;      } 

when run code, example:

pplay.exe c:\windows\media\chimes.wav 

it prints:

memory play 6 times played 1 res:1 played 2 res:1 played 4198705 

in computer code works properly. if play file more times. output is:

c:\users\avesudra\*****\***\*****\example\bin\debug>example.exe c:\windows\media\chimes.wav memory play 8 times played 1 res:1 played 2 res:1 played 3 res:1 played 4 res:1 played 5 res:1 played 6 res:1 played 7 res:1 played 8 res:1 ready

it's quite odd. if want, can download executable here try , if works: https://www.dropbox.com/s/iphluu1huzq48vk/example.exe


Comments

Popular posts from this blog

basic authentication with http post params android -

vb.net - Virtual Keyboard commands -

css - Firefox for ubuntu renders wrong colors -