Sign in to follow this  
Greycrow

Simple Mp3 playback

Recommended Posts

I have been trying to code a very simple mp3 player in C# using MCI. From all the tutorials I read this should work. But it doesnt.It returns error 266 when trying to open. Any help would be appreciated thanks. :)
using System;
using System.Text;
using System.Runtime.InteropServices;

namespace Mp3
{
    class SimpleMp3
    {
        const int MCIERR_CANNOT_LOAD_DRIVER = 266; 

        //mciSendString 
        [DllImport("winmm.dll")]
        public static extern Int32 mciSendString(String command, StringBuilder buffer, Int32 bufferSize, IntPtr hwndCallback);
        
        static void Main()
        {
            int error;

            String playCommand = "open \"song.mp3\" type mpegvideo alias MediaFile";
            error = mciSendString(playCommand, null, 0, IntPtr.Zero);

            playCommand = "play MediaFile";
            error = mciSendString(playCommand, null, 0, IntPtr.Zero);

            return;

        }   
    }
}



Share this post


Link to post
Share on other sites
What happens if you drop the explicit mpegvideo device type from your open command string?

I.e., try the following command string sequence:


open song.mp3 alias blah
play blah
.. wait for however long you want ..
close blah

Share this post


Link to post
Share on other sites
No , removing the mpegvideo keyword did nothing.

I'm ready to give up. This is very frustrating.
All of the online samples and tutorials dont work either for
some reason or another. MCI playback seemed easy at first.
But it is proving to be too difficult.
The other problem is, all the other codecs, libraries ect. are
just as difficult to work with.

I'm looking for something that will run in about 10 lines of code.

Maybe I'll come up with something...

Share this post


Link to post
Share on other sites
Quote:
Original post by Greycrow
No , removing the mpegvideo keyword did nothing.

Just to be sure, you removed the "type" part as well?
Do you get a new error code when you remove the "type mpegvideo" argument?

Share this post


Link to post
Share on other sites
removing "type mpegvideo" seems to do nothing.
I can extract the error but the details are vague.
pretty sure I have the mci driver , mediaplayer works ok.

"Unknown problem while loading the specified device driver."



using System;
using System.Text;
using System.Runtime.InteropServices;
using System.Windows.Forms;

namespace Mp3
{
class SimpleMp3
{
//mciSendString
[DllImport("winmm.dll")]
public static extern Int32 mciSendString(String command, StringBuilder buffer, Int32 bufferSize, IntPtr hwndCallback);
[DllImport("winmm.dll")]
static extern Int32 mciGetErrorString(Int32 errorCode, StringBuilder errorText, Int32 errorTextSize);

static void Main()
{
int error;
StringBuilder text = new StringBuilder(128);
String song = "song.mp3";

String playCommand = "open \"" + song + "\" type mpegvideo alias MediaFile";

error = mciSendString(playCommand, null, 0, IntPtr.Zero);

if (error == 0)
{
playCommand = "play MediaFile";
mciSendString(playCommand, null, 0, IntPtr.Zero);
}
else
{
mciGetErrorString(error, text, 128);
MessageBox.Show(text.ToString());
}

return;

}
}
}


Share this post


Link to post
Share on other sites
I can't speak to MCI stuff, and generally media API's from that era aren't well supported these days. If you can't get that to work, a less simple, but definitely working solution is to use the Windows Media SDK or the QuickTime 7 API. They both have provisions for handling at least some of the playback work on their own, but I haven't personally used that side of them (I just extract the decoded samples for my video processing app, which handles its own preview sync and playback). On the Windows Media side at least, I think you'll still need to open a waveOut on your own and then send the decoded PCM audio to that. Windows Media uses C++ style COM interfaces, and QuickTime is more straight-forward C style function calls.

Once you figure out some of Apple's style and terminology (and find the right documentation), QuickTime can have a beautiful simplicity to it, but your users would need to have the QuickTime 7 player installed (doesn't need to be running; they just need the DLL's installed). The API should be able to play MP3's, MPEG-4 Audio, and various other formats, including a decent amount of video formats. Decoding performance tends to be pretty good... it is limited to being only single-threaded (for the most part) on the Windows platform, but that's not really an issue for audio stuff.

If you go with Windows Media, look at the IWMReader interface, or IWMSyncReader if you need more control. Those will handle MP3's as well as WMA's (and ASF / WMV's too, for that matter). I haven't built an audio playback thread based on it yet (although I need to), but I have used IWMSyncReader to decode and extract audio into PCM format with good results. Don't get roped into trying to implement your own buffers... it'll make you cry. The inefficient method of allocating a new INSSBuffer for every sample is simple and it works, and I still get decent performance out of that.

Share this post


Link to post
Share on other sites
It seems I have been going back and forth on the mp3 issue for some time now.

Much of what I've been reading indicates that Microsoft really doesn't want
to support mp3 anymore.

Given that, I'm left with rolling my own, or 3rd party libraries.

I've looked into mp3 codec theory for about a week, and I've come to the realization that I could easily spend years on that topic. It's very interesting, but amazingly complex. So I've decided that rolling my own is impractical.

As for 3rd party libraries, the biggest problem is there are just so many to choose from. I spent another week looking into various types and they all have good and bad points.

In then end I am taking a suggestion from Telastyn, and going with FMOD.
The license is free while I'm playing around. Perhaps I may get to the point where I would buy a hobbyist liscence for $100.00. Not too bad really.

In my humble opinion, for anyone new to mp3 programming, this appears to be the best option.

Thanks for the input from everyone.

:)

Share this post


Link to post
Share on other sites
Quote:
Original post by Greycrow
Much of what I've been reading indicates that Microsoft really doesn't want to support mp3 anymore.
How do you reach that conclusion? Windows uses DirectShow for general-purpose multimedia, and this works beautifully for MP3 playback (and doesn't require an expensive licence for commercial use, as it's part of Windows).

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