Sign in to follow this  
Greycrow

Simple Mp3 Playback part 2 - DirectX

Recommended Posts

Ok how about this then. I gave up on MCI. Switched to DirectX I get a "Loader Lock" error when I try to run this
using Microsoft.DirectX.AudioVideoPlayback;

namespace Mp3
{
    class SimpleMp3
    {
         static void Main()
        {
            Audio song = new Audio("song.mp3"); ;

            song.Play();

        }
    }
}

Share this post


Link to post
Share on other sites
Sorry, what does that mean exactly?

That this program cannot be done in c# and I must switch to c++??

Thats fine if thats the case. Let me know . thanks :)

Share this post


Link to post
Share on other sites
It's an issue with the (long unsupported) MDX. Use SlimDX instead for DirectX.

However, DirectShow is no longer part of DirectX (and has not been for a long time!) Use DirectShow .NET to wrap up the DirectShow API. MSDN contains useful information about DirectShow, starting with the basics of how to play a file, though you'll need to be able to be able to translate from the C++ COM syntax to C# code.

Edit: A translation of the "How to play a file" article into C# may look like this:
using System.Runtime.InteropServices;
using DirectShowLib;

class Program {

const int INFINITE = -1;

static void Main(string[] args) {
IGraphBuilder pGraph = null;
IMediaControl pControl = null;
IMediaEvent pEvent = null;

// Create the filter graph manager and query for interfaces.
pGraph = (IGraphBuilder)new FilterGraph();

pControl = (IMediaControl)pGraph;
pEvent = (IMediaEvent)pGraph;

// Build the graph. IMPORTANT: Change this string to a file on your system.
pGraph.RenderFile(@"C:\Example.avi", null);

// Run the graph.
pControl.Run();

// Wait for completion.
EventCode evCode;
pEvent.WaitForCompletion(-1, out evCode);

// Note: Do not use INFINITE in a real application, because it
// can block indefinitely.
Marshal.ReleaseComObject(pControl);
Marshal.ReleaseComObject(pEvent);
Marshal.FinalReleaseComObject(pGraph);

}
}

Share this post


Link to post
Share on other sites
Quote:
Original post by Greycrow
Sorry, what does that mean exactly?


It means that if you did a google search for loader lock, then the first dozen or so links would all tell you how to work around it...

But, as benryves says, the api itself is a bit out of date. If wrapping direct show does not work for you, I've used FmodEx in the past to play mp3s in C#, and it's a great api (though not free if your project is not free).

Share this post


Link to post
Share on other sites
Ya I tried FMod, but its not free (license wise) and most of the others are either not free or horribly complicated. I think you need a PHD to know how
to write an MP3 decoder from scratch (or even 2 PHD's! ... lol).

I'm amused there is no simple way (couple of code lines), and that the simple ways of the past (MCI, and DirectX) are gone. Probably, Microsoft's usual way of not supporting anything which it can't make money on.

I'll keep looking...

Share this post


Link to post
Share on other sites
Fmod is only not free if your app isn't free. If you're actually profiting from the app you can afford the $6k (only $3k if your app is <$20 and download only).
And really, while you're developing your code, your app is free.

If the cost turns out to be more than you can afford when you are ready to sell the app, then swap it out with another API. So many projects here never get close to sale. There's no need to make your project any harder than it needs to be.

Share this post


Link to post
Share on other sites
Quote:
Original post by Greycrow
I'm amused there is no simple way (couple of code lines), and that the simple ways of the past (MCI, and DirectX) are gone. Probably, Microsoft's usual way of not supporting anything which it can't make money on.
DirectShow looks quite scary (hurrah for COM) but if you wrap up its ugliness into an Audio class (as Microsoft did in MDX) then you're good to go. (Personally, I find DirectShow's COM interface less grating in C# than FMOD's C-style API, complete with manual error checking, but YMMV).

Share this post


Link to post
Share on other sites
ya Telastyn, I like your idea.

Perhaps FMod is the best way to go.

I could encapsulate all the basic FMod mp3 functions into a dll,
and then if I really want to switch api's later ,it would be just a matter of switching out the dll.





Share this post


Link to post
Share on other sites
Quote:
Original post by benryves
Quote:
Original post by Greycrow
I'm amused there is no simple way (couple of code lines), and that the simple ways of the past (MCI, and DirectX) are gone. Probably, Microsoft's usual way of not supporting anything which it can't make money on.
DirectShow looks quite scary (hurrah for COM) but if you wrap up its ugliness into an Audio class (as Microsoft did in MDX) then you're good to go. (Personally, I find DirectShow's COM interface less grating in C# than FMOD's C-style API, complete with manual error checking, but YMMV).


Indeed, I've had more experience with C than COM stuff so found that to be the lesser of two evils to wrap into a decent interface.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this