开发者

PlaySound() mmslib does not play existing sound

开发者 https://www.devze.com 2023-03-22 17:09 出处:网络
EDIT: Solved. Simply the .wav file was not accepted by Windows. I plucked one of Windows own files and renamed it to what my previous file was called and it plays without problem.

EDIT: Solved. Simply the .wav file was not accepted by Windows. I plucked one of Windows own files and renamed it to what my previous file was called and it plays without problem.

I don't know why this can't play the existing file. Windows gives a chime in that something 开发者_运维百科is wrong but I have no clue what.

I added a check right before to make sure it exists. I have also tried absolute paths.

string wavPath = "c:\\frog.wav";

struct stat stFileInfo;
bool blnReturn = (stat(wavPath.c_str(), &stFileInfo) == 0); //this returns true

FILE* fp = fopen(wavPath.c_str(), "r");
if (fp) {
   fclose(fp); //this triggers
}

PlaySound(wavPath.c_str(), NULL, SND_FILENAME | SND_ASYNC); //m_hinstance
//C:\\Users\\Wollan\\My Code\\A\\Debug\\frog.wav
//TEXT("frog.wav")
//TEXT(wavPath.c_str())
//(LPCSTR)"frog.wav¨

The file plays fine in WMP.


This following code perfectly works:

PlaySound(L"C:\\Windows\\Media\\Cityscape\\Windows Balloon.wav", 0, SND_FILENAME ); 

Adding SND_ASYNC fails to play.


The documentation says:

The pszSound parameter is a file name. If the file cannot be found, the function plays the default sound unless the SND_NODEFAULT flag is set.

And:

PlaySound searches the following directories for sound files: the current directory; the Windows directory; the Windows system directory; directories listed in the PATH environment variable; and the list of directories mapped in a network. If the function cannot find the specified sound and the SND_NODEFAULT flag is not specified, PlaySound uses the default system event sound instead.

No other case is specified for this outcome.

Therefore, that you hear a chime indicates that the file is not being found, despite your assurances to the contrary.

I'd double-check the result of that stat call; I can't even find stat in the documentation; it doesn't appear to be part of Windows.


PlaySound(L"C:\Windows\Media\Cityscape\Windows Balloon.wav", 0, SND_FILENAME ); Adding SND_ASYNC fails to play. This answer is right! It is because the ASYNC mode plays the music after the function returns. Your code may have exited before the music plays. use int x, cin>>x, after PlaySound function, you will find that it works well.

0

精彩评论

暂无评论...
验证码 换一张
取 消