URI Prefix not supported

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?

Answers


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


Need Your Help

How to prematurely cancel a request in RestKit and invoke 'didFailWithError'

objective-c xcode restkit

I am using RestKit in my Objective-C project and need to specify a timeout for a call to my service of around 10 seconds.

loop through jstree to search for all occurences of a node id and then change the class of those nodes

javascript jquery jstree

I have the following javascript code which changes the class (of the <ins> tag) for each selected node in the jstree:

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.