如何播放音乐文件指定的时间
我想做的是播放音乐文件指定的持续时间,然后停止播放。但是,正在播放整个音乐文件。有什么想法吗?
我尝试过启动一个新线程,但仍然不起作用。
What I'm trying to do is play a music file for a specified duration, and then stop playing. However, the whole music file is being played. Any ideas?
I've tried starting a new thread, still doesnt work.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
问题是 PlaySync 会阻塞线程,因此其他消息不会被处理。这包括来自 Tick 事件的停止命令。您必须使用常规的 Play 函数,该函数将是异步的,并创建一个新线程来播放文件。您必须根据应用程序的工作方式来处理由此产生的多线程情况。
The problem is that PlaySync blocks the thread, so other messages won't be processed. This includes your stop command from the Tick event. You have to use the regular Play function, which will be asynchronous and creates a new thread to play the file in. You will have to handle the resulting multithreading situation depending on how your application works.
我会构建大约像这样的东西:它只是在编辑窗口中手写的,所以不要指望它会像那样编译。它只是为了说明这个想法。
I would build something approximately like this: It is just written out of hand in the edit window so don't expect it to compile just like that. It is only meant to illustrate the idea.
试试这个:
try this: