0

我的问题如下。我用 Playsound() 函数播放声音没有问题,但是如果程序找不到合适的声音文件(因为它不存在),我想打印“文件不存在”sencence 而不是程序播放默认“bip”语音。如果 Playsound() 的播放不成功,有什么方法可以获取值。从理论上讲,如果成功则返回 TRUE,否则返回 FALSE,但我无法用变量捕获它们。我用 C 编程并使用 DevC++ 4.9.9.2 和 CodeBlocks 12.11 没有任何成功。

感谢任何帮助!

My Codes:

//Try_1:
#include <stdio.h>
#include <windows.h>
main()
{
//if (!PlaySound("R2D2.wav",NULL, SND_FILENAME )) // not working
if (!PlaySound("R2D2.wav",NULL, SND_FILENAME | SND_NODEFAULT | SND_ASYNC)) // not working properly
    {
        printf("The file is not exist");
    }

system("pause");
}
/*
If the program can't find the R2D2.wav then there isn't the default "bip" voice thanks to the SND_NODEFAULT, but the printf line will not run so I don't see the "The file is not exist" sentence.
*/
//--------------------------------------------------------------------
//Try_2:
#include <stdbool.h>
#include <stdio.h>
#include <windows.h>
main()
{
//Returns TRUE if successful or FALSE otherwise.
bool x;
x=PlaySound("R2D2.wav",NULL, SND_FILENAME | SND_NODEFAULT | SND_ASYNC); // not working properly
    if (x==FALSE)
    {
        printf("The file is not exist");
    }
system("pause");
}
/*
If the program can't find the R2D2.wav then there isn't the default "bip" voice thanks to the SND_NODEFAULT, but the printf line will not run, so I don't see the "The file is not exist" sentence.
*/
4

1 回答 1

0

此代码是检查文件。如果存在,则播放声音。如果没有,请打印一条消息。

#include <stdio.h>
#include <string.h>
#include <windows.h>

int isFileExists(char *filename);

main()
{
int b=9;
char *p,Array[9];

strcpy(Array,"R2D2.wav");
p = Array;
b=isFileExists(p);
    if(b==21)
    {
        PlaySound("R2D2.wav",NULL, SND_FILENAME | SND_NODEFAULT | SND_ASYNC);
    }
    else if (b==20)
    {
            printf("The file is not exist");
    }


system("pause");
}

int isFileExists(char *filename)
{
FILE *file;
file = fopen(filename, "r");
    if (file == NULL)
    {
        return 20; //The file is not exist
    }
fclose(file);
return 21; //The file is exist
}
于 2013-11-13T07:55:08.643 回答