2

I am trying to load and play a wave file using:

SoundPlayer simpleSound = new SoundPlayer(@"pack://application:,,,/MyAssembly;component/Sounds/10meters.wav");
            simpleSound.Play();

With no success. I get a System.NotSupportedException :( see below.

System.NotSupportedException: The URI prefix is not recognized.
   at System.Net.WebRequest.Create(Uri requestUri, Boolean useUriBase)
   at System.Net.WebRequest.Create(Uri requestUri)
   at System.Media.SoundPlayer.LoadSync()
   at System.Media.SoundPlayer.LoadAndPlay(Int32 flags)
   at System.Media.SoundPlayer.Play()

I looked over google and SO trying to find a solution, nothing worked. Playing the file with a direct path works fine

SoundPlayer simpleSound = new SoundPlayer(@"D:\Projects\MyAssembly\Sounds\10meters.wav");
simpleSound.Play();

I also checked MyAssembly content, the resource is there. Does SoundPlayer not support packing or there anything I am not doing correctly?

GETah
  • 20,922
  • 7
  • 61
  • 103

2 Answers2

6

The pack:// URI scheme is specific to WPF, so non-WPF components don't know how to handle it... however, you can retrieve a stream for this resource, and pass it to the SoundPlayer constructor:

Uri uri = new Uri(@"pack://application:,,,/MyAssembly;component/Sounds/10meters.wav");
StreamResourceInfo sri = Application.GetResourceStream(uri);
SoundPlayer simpleSound = new SoundPlayer(sri.Stream);
simpleSound.Play();

Another option is to use the MediaPlayer class:

Uri uri = new Uri(@"pack://application:,,,/MyAssembly;component/Sounds/10meters.wav");
MediaPlayer player = new MediaPlayer();
player.Open(uri);
player.Play();

This class supports the pack:// URI scheme

Thomas Levesque
  • 286,951
  • 70
  • 623
  • 758
  • Good call on the stream constructor. – H.B. Dec 01 '11 at 16:18
  • +1 thanks. `StreamResourceInfo sri = Application.GetResourceStream(uri);` did not work as I am doing this from a dll file not the main exe application. MediaPlayer is too heavy for what I am doing :( Any other ideas? – GETah Dec 01 '11 at 16:46
  • 1
    What do you mean, "did not work"? You just need to add a reference to PresentationFramework. There's no way to access a resource with a pack URI independently from WPF, if that's what you're trying to do... – Thomas Levesque Dec 01 '11 at 17:01
  • Damn me, completely forgot to reference PresentationFramework. Thanks a lot for the pointer – GETah Dec 01 '11 at 21:46
  • @ThomasLevesque Thanks for the awesome answer. I tried both your solutions. Using StreamResourceInfo with SoundPlayer worked. But using MediaPlayer doesnot. No sound is played when I use MediaPlayer. Both solutions are referring to the exact same URI in my project. I tuned the MediaPlayer's volume to max and still cannot get any sound. – Frank Liu Sep 02 '14 at 00:39
1

F1 is your friend (in VS 2010 at least):

The string passed to the soundLocation parameter can be either a file path or a URL to a .wav file.

URIs are not URLs (unlike the other way around), this will not work. You could save the file to temporary folder on disk if you need to.

H.B.
  • 166,899
  • 29
  • 327
  • 400