问题描述
我的程序正在播放声音PlaySound
.
程序运行正常,我可以听到声音,但是当歌曲结束时,有大约 1 秒的延迟,然后歌曲再次播放.
我问过谷歌,他给了我这个问题 - PlaySound() Delay
回答的人说,我们需要使用 SND_ASYNC
而不是 SND_SYNC
,我听他说并做了,但我什么也听不到.>
你有什么建议吗?
顺便说一句,这是我目前用于这个项目的歌曲 - Nyan Cat
我希望这首歌立即重新开始,让用户听不到有延迟.
最终代码:
#include #include #include #pragma 注释(lib,winmm.lib")int main(){std::string pathtosound = "C:\\Users\\roile\\Documents\\Dragonite\\nyan.wav";而(真){播放声音(pathtosound.c_str(),0,SND_SYNC);}返回0;}
SND_LOOP
标志在 Microsoft Docs:
声音重复播放,直到 PlaySound 被再次调用pszSound 参数设置为 NULL.如果设置了这个标志,你还必须设置SND_ASYNC 标志.
请注意最后一句,因此以下代码可能会更好:
#include #include #include #pragma 注释(lib,winmm.lib")int main(){std::string pathtosound = "C:\\Users\\roile\\Documents\\Dragonite\\nyan.wav";播放声音(pathtosound.c_str(), 0, SND_ASYNC | SND_LOOP);而(真){//在某一点停止循环}播放声音(NULL, 0, 0);//停止采样返回0;}
My program is playing a sound PlaySound
.
The program works fine and I can hear the sound, but when the song end, there is a delay for like 1 second, and then the song play again.
I asked Google, and he gave me this question - PlaySound() Delay
The guy who answerd , said that instead SND_SYNC
we need to use SND_ASYNC
, I listened to him and did it, but I can't hear anything.
Do you have any suggestions ?
Btw, this is the song I'm currently using for this project - Nyan Cat
I want that this song will be start again immediately, for the user to not hear that there is a Delay.
Final Code:
#include <iostream>
#include <Windows.h>
#include <string>
#pragma comment(lib, "winmm.lib")
int main()
{
std::string pathtosound = "C:\\Users\\roile\\Documents\\Dragonite\\nyan.wav";
while (true) {
PlaySound(pathtosound.c_str(), 0, SND_SYNC);
}
return 0;
}
The SND_LOOP
flag is described as follows in Microsoft Docs:
Pay attention to the last sentence, hence the following code will probably work better:
#include <iostream>
#include <Windows.h>
#include <string>
#pragma comment(lib, "winmm.lib")
int main()
{
std::string pathtosound = "C:\\Users\\roile\\Documents\\Dragonite\\nyan.wav";
PlaySound(pathtosound.c_str(), 0, SND_ASYNC | SND_LOOP);
while (true) {
// Stop loop at some point
}
PlaySound(NULL, 0, 0); // Stop sample
return 0;
}
这篇关于有没有办法立即播放声音?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!