Tech Off Thread

3 posts

Forum Read Only

This forum has been made read only by the site admins. No new threads or comments can be added.

Playing an MP3 from a console app

Back to Forum: Tech Off
  • User profile image
    MasterPi

    Any suggestions?

    I'm currently trying to use Winmm.dll but for some reason, nothing happens when I go to play my mp3. I can't really embed WMP, so I'm not sure what else to do.

  • User profile image
    tfraser

    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:

    [DllImport("winmm.dll")]
    
    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");
    file.Play();
    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.

  • User profile image
    MasterPi

    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:

    [DllImport("winmm.dll")]
    
    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");
    file.Play();
    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.

    Strange.

    Anyway, thanks for the sample!

Conversation locked

This conversation has been locked by the site admins. No new comments can be made.