Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
321 views
in Technique[技术] by (71.8m points)

uri - XNA 4 Song.fromUri containing Spaces

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() don't work, the Result is a new Uri Object, containing null. Same Thing on uri.escapeDataString() or different Solutions.

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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 MusicBlah.mp3", 0 });

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.

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.

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...