在 Win32 SDK 中使用 PlaySound 时如何检测声音何时播放完毕?

发布于 2024-11-05 20:55:01 字数 174 浏览 0 评论 0原文

我正在使用 Win32 SDK 中的 PlaySound 函数来播放波形声音文件。目前,我有以下代码行:

PlaySound(szFile,NULL,SND_FILENAME );

但现在我想知道,如何检测波形文件播放完毕的时间?我想在波形停止播放时更改按钮的文本。

I am using the PlaySound function from the Win32 SDK to play a wave sound file. Currently, I have the following line of code:

PlaySound(szFile,NULL,SND_FILENAME );

But now I want to know, how I can detect the time when the wave file finished playing? I want to change a button's text when the wave stops playing.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

述情 2024-11-12 20:55:01

PlaySound 的功能非常有限。在我开发的一个产品中,我们在 DirectSound 之上构建了一个媒体播放库来克服这些限制。它所做的许多事情包括编写 WAV 文件解析器和线程代码以将 PCM 样本流维护到 DirectSound 缓冲区中。不适合胆小的人。

您可以走这条路,但这里有一些更简单的建议。

  1. 如果您知道正在播放的 WAV 文件的长度,则可以使用 SND_ASYNC 标志调用 PlaySound。安排一个计时器在声音应该完成播放的估计时间触发。如果您不知道 WAV 文件的长度,您可以编写一个解析器来读取文件头。将“fmt”块的采样率和格式与数据块中的字节数进行比较,以计算文件的时间长度。

  2. 或者...创建一个专用线程来发出 PlaySound 调用(不带 SND_ASYNC 标志)。当同步 PlaySound 函数返回时,向您的 UI 线程执行 PostMessage 以更新按钮。如果您有对 PlaySound 的重叠调用,您可能需要进行一些多线程同步,但这应该不难。

PlaySound is very limited in what it can do. In a product I work on, we built a media playback library on top of DirectSound to do get over the limitations. Among many things it did, it involved writing a WAV file parser and threading code to maintain a stream of PCM samples into a DirectSound buffer. Not for the faint of heart.

You could go this route, but here's some simpler suggestions.

  1. If you know the length of the WAV file you are playing, you can call PlaySound with the SND_ASYNC flag. Schedule a timer to fire at the estimated time the sound is supposed to finish playing. If you don't know the length of the WAV file, you could write a parser to read the header of the file. Compares the sampling rate and format from the "fmt" chunk to the number of bytes in the data chunk to compute the time length of the file.

  2. OR.... Create a dedicated thread for issuing PlaySound calls (without the SND_ASYNC flag). When the synchronous PlaySound function returns, do a PostMessage to your UI thread to update the button. If you have overlapping calls to PlaySound, you'll likely need to do some multi-threaded synchronization, but it shouldn't be hard.

陌上芳菲 2024-11-12 20:55:01

除非您指定 SND_ASYNC 标志,否则 PlaySound 函数同步,这意味着在您调用它之后,它不会将控制权返回给您的程序,直到之后它播放完声音文件。

因此,一旦函数返回一个值(如果成功则返回 TRUE,否则返回 FALSE),您就知道声音已播放完毕。

您只需在调用 PlaySound 函数后立即在下一行代码中更新按钮控件的文本即可。如果您希望确保按钮立即更新以反映您的更改,您可能需要调用 UpdateWindow 函数,也是如此。

Unless you specify the SND_ASYNC flag, the PlaySound function is synchronous, meaning that after you call it, it does not return control to your program until after it has finished playing the sound file.

Therefore, once the function returns a value (either TRUE if successful, or FALSE otherwise), you know that the sound has finished playing.

You can simply update the text of your button control in the next line of code, immediately after you call the PlaySound function. If you want to ensure that the button gets updated immediately to reflect your changes, you might need to call the UpdateWindow function, as well.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文