Kaci Adjou
Kaci Adjou

Reputation: 11

Dynamic audio player for an XNA Game

I am currently working on a customized media player in C#, the main purpose of this thing is to let the user to play his own music files. I already have a player of this kind (https://bitbucket.org/_Bruce/media-player), it works fine but only wave files are supported. This limitation bothers me because mp3 is a tiny format (about disk space).

I have already tried and more or less succeeded in this task by using the FromUri method but there is a tricky problem: if the user name of the machine contains a space the string will be not accepted by the compiler, i have tried to resolve this with Uri.EscapeUriString but Visual Studio says that a Dos path must be with a root like C:\.

The code I am trying is below or Full code here

    string[] SongsToRead = Directory.GetFiles(Environment.CurrentDirectory, "*.mp3");
    song song;
    protected override void Initialize()
    {
        var var1 = Uri.EscapeUriString(SongsToRead[0]);
        Song = Song.FromUri("Test", new Uri(var1));
        base.Initialize();
    }

Am I on the wrong side?

Thanks!

Upvotes: 0

Views: 420

Answers (1)

mcmonkey4eva
mcmonkey4eva

Reputation: 1377

Well now that you have something to work with-

I, honestly, despite all my time working in XNA, never even knew there was a Song.FromUri(...) - I've never had any use for it.

If you're going to use XNA's built in system, just use the regular Content.Load<Song>("mysong"); method - or try loading it as a SoundEffect using SoundEffect.FromStream(new System.IO.FileStream("MyFile.mp3", System.IO.FileMode.Open)); - which is intended for .wav sounds but should be compatible with other MS-approved audio files.

If you want to avoid that because you think the Content Pipeline is annoying or SoundEffect won't work well, you have to first realize the entire XNA audio setup is rather poor as well.

I would recommend using the FMod-Ex Sound System (API) if you want dynamic control and loading-from-file-without-content-pipline of your audio.

(It's a bit complicated to setup initially, but once you get it the .dll file in the right place and the C# wrapper imported, it's a wonderfully useful thing and comparitively easy to use)

If you really want to work with your current code, the only thing I notice:

    var var1 = Uri.EscapeUriString(SongsToRead[0]);

Don't do that. That converts, for example, myfolder\myfile.mp3 to myfolder%5Cmyfile.mp3, it's used for HTTP stuff. Not good here.

Upvotes: 1

Related Questions