Search code examples
c#audiomp3

Play audio files in C#


I need to play mp3 file. I want to use winmm.dll (Windows 7)

class Program
{
    [DllImport("winmm.dll")]
    private static extern long mciSendString(string Cmd, StringBuilder StrReturn, int ReturnLength, IntPtr HwndCallback);

    static void Main(string[] args)
    {
        string FileName = @"F:\MUSIC\ROCK.mp3";

        string CommandString = "open " + "\"" + FileName + "\"" + " type mpegvideo alias Mp3File";
        mciSendString(CommandString, null, 0, IntPtr.Zero);
        CommandString = "play Mp3File";
        mciSendString(CommandString, null, 0, IntPtr.Zero);
        Console.ReadKey();
    }
}

But when I run my program, nothing happened. Where is a mistake?


Solution

  • string FileName = @"F:\MUSIC\ROCK.mp3";
    mciSendString("open \"" + FileName + "\" type mpegvideo alias MediaFile", null, 0, IntPtr.Zero);
    mciSendString("play " + FileName + " from 0", null, 0, IntPtr.Zero);
    

    It works correctly.