不支持 URI 前缀

发布于 2024-12-19 02:01:20 字数 885 浏览 0 评论 0原文

我正在尝试使用以下方法加载和播放波形文件:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

我喜欢麦丽素 2024-12-26 02:01:20

pack:// URI 方案特定于 WPF,因此非 WPF 组件不知道如何处理它...但是,您可以检索此资源的流,并将其传递给SoundPlayer 构造函数:

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

另一个选项是使用 MediaPlayer 类:

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

该类支持 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 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

情独悲 2024-12-26 02:01:20

F1 是你的朋友(至少在 VS 2010 中):

传递给 soundLocation 参数的字符串可以是文件路径,也可以是 .wav 文件的 URL。

URI 不是 URL(与其他方式不同),这是行不通的。如果需要,您可以将文件保存到磁盘上的临时文件夹中。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文