gpt4 book ai didi

c - 我怎样才能听到Playsound()'s return value and do someting else if it doesn' t找到.wav

转载 作者:行者123 更新时间:2023-11-30 17:44:35 25 4
gpt4 key购买 nike

我的问题如下。我使用 Playsound() 函数播放声音没有问题,但如果程序找不到合适的声音文件(因为它不存在),我想打印“文件不存在”句子而不是程序播放默认“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.
*/

最佳答案

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

#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
}

关于c - 我怎样才能听到Playsound()'s return value and do someting else if it doesn' t找到.wav,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19939562/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com