在 Win32 SDK 中使用 PlaySound 时如何检测声音何时播放完毕?
我正在使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
PlaySound 的功能非常有限。在我开发的一个产品中,我们在 DirectSound 之上构建了一个媒体播放库来克服这些限制。它所做的许多事情包括编写 WAV 文件解析器和线程代码以将 PCM 样本流维护到 DirectSound 缓冲区中。不适合胆小的人。
您可以走这条路,但这里有一些更简单的建议。
如果您知道正在播放的 WAV 文件的长度,则可以使用 SND_ASYNC 标志调用 PlaySound。安排一个计时器在声音应该完成播放的估计时间触发。如果您不知道 WAV 文件的长度,您可以编写一个解析器来读取文件头。将“fmt”块的采样率和格式与数据块中的字节数进行比较,以计算文件的时间长度。
或者...创建一个专用线程来发出 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.
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.
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.
除非您指定
SND_ASYNC
标志,否则PlaySound
函数 是同步,这意味着在您调用它之后,它不会将控制权返回给您的程序,直到之后它播放完声音文件。因此,一旦函数返回一个值(如果成功则返回 TRUE,否则返回 FALSE),您就知道声音已播放完毕。
您只需在调用
PlaySound
函数后立即在下一行代码中更新按钮控件的文本即可。如果您希望确保按钮立即更新以反映您的更改,您可能需要调用UpdateWindow
函数,也是如此。Unless you specify the
SND_ASYNC
flag, thePlaySound
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, orFALSE
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 theUpdateWindow
function, as well.