XNA 4 Song.fromUri containing Spaces

后端 未结 1 1299
小鲜肉
小鲜肉 2020-12-12 01:58

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, t

1条回答
  •  有刺的猬
    2020-12-12 02:23

    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 });
    

    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.)

    0 讨论(0)
提交回复
热议问题