我System.Windows.Media.MediaPlayer
用来播放一些声音,我想从 ZIP 文件中加载这些声音。如果能够直接从 zip 文件中将这些文件作为流加载,而不必解压缩到临时目录,那就太好了。但是,MediaPlayer.open
只接受一个 URI。
那么,有没有办法创建一个引用流内容的 URI?类似于内存中的本地主机?是否有从 Stream 到 URI 的连接器?
我System.Windows.Media.MediaPlayer
用来播放一些声音,我想从 ZIP 文件中加载这些声音。如果能够直接从 zip 文件中将这些文件作为流加载,而不必解压缩到临时目录,那就太好了。但是,MediaPlayer.open
只接受一个 URI。
那么,有没有办法创建一个引用流内容的 URI?类似于内存中的本地主机?是否有从 Stream 到 URI 的连接器?
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.
System.Media.SoundPlayer 应该做你需要的(你可以完全跳过 MediaPlayer 和 URIs)。它有一个接受流的构造函数。