不支持 URI 前缀
我正在尝试使用以下方法加载和播放波形文件:
SoundPlayer simpleSound = new SoundPlayer(@"pack://application:,,,/MyAssembly;component/Sounds/10meters.wav");
simpleSound.Play();
但没有成功。我得到一个 System.NotSupportedException
:( 见下文。
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()
我查看了谷歌并试图找到解决方案,但没有任何效果。 使用直接路径播放文件效果很好
SoundPlayer simpleSound = new SoundPlayer(@"D:\Projects\MyAssembly\Sounds\10meters.wav");
simpleSound.Play();
我还检查了 MyAssembly 内容,资源就在那里。 SoundPlayer
是否不支持打包或者我有什么做得不正确的地方?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
pack://
URI 方案特定于 WPF,因此非 WPF 组件不知道如何处理它...但是,您可以检索此资源的流,并将其传递给SoundPlayer
构造函数:另一个选项是使用
MediaPlayer
类:该类支持
pack://
URI 方案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 theSoundPlayer
constructor:Another option is to use the
MediaPlayer
class:This class supports the
pack://
URI schemeF1 是你的朋友(至少在 VS 2010 中):
URI 不是 URL(与其他方式不同),这是行不通的。如果需要,您可以将文件保存到磁盘上的临时文件夹中。
F1 is your friend (in VS 2010 at least):
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.