我可以使用 MediaPlayer 播放流中的声音吗? 或者,我可以使用 URI 来访问文件流吗?
我正在使用 System.Windows.Media.MediaPlayer 来播放一些声音,并且我想从 ZIP 文件加载这些声音。 如果能够直接从 zip 文件将这些文件作为流加载,而不必解压缩到临时目录,那就太好了。 但是,MediaPlayer.open
仅接受 URI。
那么,有没有办法创建一个引用流内容的 URI? 像内存中的本地主机之类的东西? 是否有从 Stream 到 URI 的连接器?
I'm using System.Windows.Media.MediaPlayer
to play some sounds, and I would like to load these sounds from a ZIP file. It would be nice to be able to load these files as a stream directly from the zip file instead of having to unzip to a temp directory. However, MediaPlayer.open
only accepts an URI.
So, is there a way to create a URI that will reference the contents of a stream? Something like an in-memory localhost? Is there any connector from Stream to URI?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
System.Media.SoundPlayer 应该满足您的需要(您可以完全跳过 MediaPlayer 和 URI)。 它有一个接受流的构造函数。
System.Media.SoundPlayer should do what you need (you can skip MediaPlayer and URIs altogether). It has a constructor that takes a stream.
由于您需要支持复调音乐,一种方法是 PInvoke 到 waveOutXXXX Windows API 中以播放声音。 下面是如何在 C# 中执行此操作的一个很好的示例:
https://www .codeproject.com/KB/audio-video/cswavplay.aspx
此示例还包含用于读取持续时间、采样率、每个样本位数等信息的代码。
如果您进行搜索,您可能会发现声称 waveOutXXXX API 可以一次只播放一种声音。 这在 Windows 95/98 中是正确的,但现在不再正确了。
顺便说一句,SoundPlayer 可能是最令人沮丧的 .NET 类。 .Play() 方法会在开始之前自动停止播放您的进程播放的任何其他声音(可能是任何 .NET 应用程序播放的任何其他声音),这就是您不能用它进行复调的原因。 微软最差的实习生只需要不到一分钟的时间就能将 .DontStopJustPlay() 方法或 StopFirst bool 参数添加到 .Play() 方法中。 他可能要等到午餐时间才能添加 Duration 属性。
尽管 waveOutXXXX 比您想要的精心设计的现代 API 更棘手(并且在 .NET 中使用它会带来额外的问题),但它拥有的一个无与伦比的优势是,自 Windows 95 以来,它已预装在每台 Windows 计算机上,包括Windows 移动设备。 所有其他选项(包括 MediaPlayer)都意味着总是需要有人安装某些东西。
Since you need to support polyphony, one approach is to PInvoke into the waveOutXXXX Windows API in order to play the sounds. Here is a good example of how to do this in C#:
https://www.codeproject.com/KB/audio-video/cswavplay.aspx
This example also has code for reading info like duration, sample rate, bits per sample etc.
If you search, you may find claims that the waveOutXXXX API can only play one sound at a time. This was true in Windows 95/98, but is not true any longer.
By the way, SoundPlayer is probably the most frustrating .NET class. The .Play() method automatically stops playback of any other sound played by your process (it might be any other sound played by any .NET app) before starting, which is why you can't do polyphony with it. It would have taken Microsoft's worst intern less than a minute to add a .DontStopJustPlay() method or a StopFirst bool parameter to the .Play() method. It might have taken him until lunch time to add a Duration property.
Although waveOutXXXX is trickier than you would want from a well-designed modern API (and using it in .NET introduces additional problems), the one unmatched advantage it has is that it's come pre-installed on every single Windows computer since Windows 95, including Windows Mobile devices. Every other option (including MediaPlayer) means somebody will always have to install something.