XNA 4 Song.fromUri 包含空格 [英] XNA 4 Song.fromUri containing Spaces

查看:15
本文介绍了XNA 4 Song.fromUri 包含空格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嘿,尝试从我的硬盘 (file:///...) 加载歌曲(通过 Uri)(Song.fromUri())时,Mediaplayer 会抛出异常.经过一番谷歌搜索,我发现这个问题与 Uri 中的空间有关,所以我尝试逃避它:

Hey, When trying to load a Song (via Uri) (Song.fromUri()) from my HDD (file:///...), The Mediaplayer throws a Exception. After a bit of googling i found out, that this Problem is related to Spaces in the Uri, so i tried escaping it:

Uri.escapeUriString() 不起作用,Result 是一个新的 Uri 对象,包含 null.uri.escapeDataString() 上的相同内容或不同的解决方案.

Uri.escapeUriString() don't work, the Result is a new Uri Object, containing null. Same Thing on uri.escapeDataString() or different Solutions.

有没有更简单的方法将外部 mp3 加载到 XNA 中?

Is there an easier way to load an external mp3 into XNA?

推荐答案

最简单的方法是使用反射来访问这个内部构造函数:

The simplest method would be to use reflection to get access to this internal constructor:

internal Song(string name, string filename, int duration);

这里有一些代码可以做到这一点:

Here is some code that does just that:

var ctor = typeof(Song).GetConstructor(
        BindingFlags.NonPublic | BindingFlags.Instance, null,
        new[] { typeof(string), typeof(string), typeof(int) }, null);
song = (Song)ctor.Invoke(new object[] { "name", @"C:\My Music\Blah.mp3", 0 });

显然这并不理想.我不认为 XNA 运行时会进行次要版本更新(这意味着如果您的游戏使用 XNA 4.0,它总是相同的运行时),但我不确定.通过使用内部构造函数,您的游戏完全受 Microsoft 二进制更新的支配.您可能想要一个很好的大型异常处理程序.

Obviously this is not really ideal. I do not think the XNA runtime does minor-version updates (meaning that if your game uses XNA 4.0, it is always the same runtime), but I do not know for sure. By using an internal constructor your game is entirely at the mercy of binary updates by Microsoft. You probably want a nice big exception handler on that.

此外,我发现某些 MP3 文件会(字面意思)无法播放.此外,Duration 属性显然没有被这种方法填充.

Additionally, I found that some MP3 files would (literally) silently fail to play. Also the Duration property is obviously not filled in by this method.

(显然,这是仅适用于 Windows 的代码.但无论如何您都无法像这样在 WP7 或 Xbox 上真正访问文件系统的随机位.)

(Obviously this is Windows-only code. But you can't really access random bits of the filesystem like this on WP7 or Xbox anyway.)

这篇关于XNA 4 Song.fromUri 包含空格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆