如何同时播放多个wav文件且无延迟

发布于 2024-10-27 13:20:01 字数 372 浏览 6 评论 0原文

我有一个有一些 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 技术交流群。

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

发布评论

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

评论(2

摘星┃星的人 2024-11-03 13:20:01

我想说你使用了错误的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)

一花一树开 2024-11-03 13:20:01

尝试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.

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