2013-07-31 2 views
0

나는 wav 파일을 재생하기 위해 C로 함수를 작성하고 있습니다. 사운드를 한 번 재생할 수 있지만 루프 옵션을 추가하고 싶습니다. 메모리에서 파일 이름에서PlaySound 함수의 이상한 동작

  • 플레이
  • 플레이 :

    나는 작업의 두 가지 모드가 있습니다.

두 가지 모드 모두에서 소리가 두 번 이상 재생되지 않고 그 후에 기능이 중단 될 수 있습니다.

참고 : I 코드 이것을 추가 해결 :

BOOL WINAPI는 PlaySound (LPCSTR, HMODULE, DWORD);

나는 문제가 발생합니다.

내 코드 :

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

가 인쇄 : :이 예를 들어 코드를 실행할 때

#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; 
    for (i = 1; i <= 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;  
} 

내 컴퓨터에서

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

루프를 단계별로 실행할 때 디버거는 무엇을 표시합니까? –

답변

0

을 코드가 제대로 작동합니다. 파일을 더 많이 재생하더라도 출력은 다음과 같습니다

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

그것은 매우 이상한. 원하는 경우 여기에서 실행 파일을 다운로드하여 시도해보고 작동하는지 확인하십시오. https://www.dropbox.com/s/iphluu1huzq48vk/example.exe

+0

감사합니다. 작은 C를 사용하고 있습니다.이 헤더를 추가하고 컴파일하고 실행합니다. 런타임 실행이 실패합니다. BOOL WINAPI PlaySound (LPCSTR, HMODULE, DWORD); – carlos

+0

gcc에서 컴파일 된 코드가 궁금합니다. 컴파일러가 있지만 모르겠습니다. – avesudra

+0

이전에 잘못된 호출 규칙으로 호출 된 것일 수 있습니까? 'WINAPI'비트가 차이를 만듭니다. –