Tech Off Post

Single Post Permalink

View Thread: Playing an MP3 from a console app
  • User profile image

    tfraser said:
    What you're trying to do definitely works. I tried it by implementing the MCI in the simplest form that I could and now have music playing from a blank console window.

    Put this into a new class called 'File' and remember to reference the System.Runtime.InteropServices namespace:

    static extern int mciSendString(string mciCommand, StringBuilder buffer, int bufferSize, IntPtr callback); string fileName;
    public void Send(string mciCommand) { mciSendString(mciCommand, null, 0, IntPtr.Zero); }
    public File(string fileLocation)
        fileName = fileLocation;
        Send("open " + fileName);

    public void Play() {     Send("play " + fileName); }

    Then drop this into your startup object:

    File file = new File("C:/Test.mp3");
    string input = Console.ReadLine();

    It should work now. At the moment it won't do anything other than play the file but the MSDN section on MCI describes all the additional commands that can be called.
    Hmm, I finally got around to trying your code example. It seems that when I decide to use an alias "open fileName alias MySound" it doesn't work. Without the alias and just a simple "open fileName" it works fine.


    Anyway, thanks for the sample!