如何同时播放多个wav文件且无延迟
我有一个有一些 wav 文件的服务器,当客户端连接时,他可以要求服务器播放每个文件,当多个客户端连接时,或者当客户端在完成一次之前两次请求相同的 wav 文件时,我需要播放它再次,并行。
SoundPlayer
不允许这样做。我尝试了 WindowsMediaPlayer
对象,但是当以以下方式播放它时:
WindowsMediaPlayer wmp = new WindowsMediaPlayer();
wmp.URL = sounds_folder;
它开始有延迟。
有谁知道如何做到这一点?或者也许有一种方法可以在初始化时使用媒体播放器加载缓冲区,以便播放速度更快?
I have a server that has some wav files, and when client connects he can ask the server to play each of them, when multiple clients connected, or when a client requests the same wav file twice before it completed once, I need to play it again, in parallel.
SoundPlayer
doesn't allow that. I tried WindowsMediaPlayer
object but when playing it in the following way:
WindowsMediaPlayer wmp = new WindowsMediaPlayer();
wmp.URL = sounds_folder;
It starts with a delay.
Does anyone knows a way to do this? Or maybe a way to load the buffers with the mediaplayer in the initialization so that that playing will be faster?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我想说你使用了错误的API。媒体播放器旨在渲染媒体文件(消费媒体),而不是简短的直接样本/剪辑
Google 向我展示了
http://msdn.microsoft.com/en-us/library/ms229685.aspx
http://www.eggheadcafe.com/articles/20030302.asp
第二个甚至假设您预先将整个样本加载到内存中,因此无需担心缓冲时间:)
下一步:关于并行声音;你应该尝试其他 API。您可能很幸运它们是异步的,否则可以通过任何方法
ThreadPool.QueueUserWorkItem 使用线程池
delegate.BeginInvoke(...)
手动创建一个新的 Thread()... 和 Start() 。谨防线程陷阱(不要做任何工作,当然也不要在线程之间没有锁定的情况下共享数据),但是对于简单的“即发即忘”背景声音,这一切都将很简单(前提是您将声音保留在缓冲区中)数据足够长的时间 - 即使它们静态)
I would say you are using the wrong API. Media player is designed to render media files (consumer media) not short direct samples/clips
Google showed me
http://msdn.microsoft.com/en-us/library/ms229685.aspx
http://www.eggheadcafe.com/articles/20030302.asp
the second even presumes you load the whole sample in-memory upfront so there is no need to worry about buffering times :)
Next: on parallel sounds; you should try the other API's. You might be in luck with them being async, otherwise use the thread pool by any of the methods
ThreadPool.QueueUserWorkItem
delegate.BeginInvoke(...)
of manually create a new Thread()... and Start() that. Beware of threading pitfalls (don't do any work and certainly don't share data without locking between threads) but for a simple 'fire and forget' background sound this is all going to be simple (provided that you keep the buffers with sound data around long enough - i.e. make them static)
尝试wmp.controls.play();。
这将消除延迟,但不幸的是,使用 WindowsMediaPlayer,您将无法比每大约 200 毫秒更频繁地播放声音,而不会错过其中一些声音。
Try
wmp.controls.play();
.That'll get rid of the lag, but unfortunately with WindowsMediaPlayer, you won't be able to play sounds more frequently than every approx 200ms without some of them being missed.