Can I use MediaPlayer to play sounds from a stream? Or can I use a URI to access the file stream?
I am 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 download these files as a stream directly from the zip file, rather than unpacking it into a temporary directory. However, MediaPlayer.open
it only accepts a URI.
So, is there a way to create a URI that will reference the content of the stream? Something like localhost in memory? Is there a connector from Stream to URI?
Since you need to support polyphony, one approach is to PInvoke the waveOutXXXX Windows API for playing sounds. Here's a good example of how to do this in C #:
http://69.10.233.10/KB/audio-video/cswavplay.aspx
This example also has code to read information such as duration, sample rate, bits per sample, etc.
If you are looking, you can find claims that the waveOutXXXX API can only play one sound at a time. This was true in Windows 95/98, but it is no longer the case.
By the way, SoundPlayer is probably the most frustrating .NET class. The .Play () method automatically starts playing any other sound played by your process (it can be any other sound played by any .NET application) before starting, so you can't do polyphony with it. It would be worse for Microsoft than a minute to add the .DontStopJustPlay () method or the StopFirst bool parameter to the .Play () method. Maybe he took it before lunch to add the Duration property.
While waveOutXXXX is more complex than you'd like from a well-designed modern API (and using it in .NET presents additional challenges), one of its unmatched advantages is that it installs on every Windows 95 machine, including Windows Mobile devices. Every other option (including MediaPlayer) means that someone always needs to install something.
source to share