c++ - 有没有办法无延迟地播放声音?
问题描述
我的程序正在播放声音PlaySound
。
该程序运行良好,我可以听到声音,但是当歌曲结束时,有大约 1 秒的延迟,然后歌曲再次播放。
我问了谷歌,他给了我这个问题 - PlaySound() 延迟
回答的那个人说,SND_SYNC
我们需要使用SND_ASYNC
,我听他说做了,但我什么也听不见。
你有什么建议吗 ?
顺便说一句,这是我目前用于这个项目的歌曲 - Nyan Cat
我希望这首歌会立即重新开始,因为用户不会听到有延迟。
最终代码:
#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;
}
解决方案
该标志在Microsoft DocsSND_LOOP
中描述如下:
声音重复播放,直到再次调用 PlaySound并将pszSound参数设置为NULL。如果设置了此标志,您还必须设置SND_ASYNC标志。
注意最后一句话,因此下面的代码可能会更好:
#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;
}
推荐阅读
- jar - 如何在 Java 规则中设置 Bazel --warn_duplicate_resources 标志?
- java - JAX-WS call does not work when calling Java from Javascript using JXBrowser
- javascript - 使用“map”函数返回多个数组
- java - 如何修复 Spring 中的 JSON 解码错误?
- big-o - 了解何时使用 theta 来计算时间复杂度
- javascript - 使用 URL 反应路由器打开页面
- json - 使用数据工厂将嵌套对象从 SQL Server 复制到 Azure CosmosDB
- c# - Transaction.commit 在实体框架中抛出争论空异常
- c# - 如何使用 C# ZipFile 处理空的 zip 文件
- c++ - @class AVPlayerLayer 的预期不合格 ID