Tuesday, May 4, 2010

Lesson 23 - Play MP3 Files Using C#

Play MP3 Files Using C#
This code uses the Windows Multimedia API winmm.dll library to play MP3 files. Put it into a class and call the Open() method with a parameter specifying the MP3 file that you wish to play.
On Thursday, April 17th 2008 at 12:18 AM
--------------------------------------------------------------------------------
By Andrew Pociu (View Profile)
--------------------------------------------------------------------------------
(Rated 3.7 with 7 votes)
Contextual Ads
More C# Resources
C# Tutorials
C# Code
C# Jobs
Advertisement
private string _command;
private bool isOpen;
[DllImport("winmm.dll")]

private static extern long mciSendString(string strCommand,StringBuilder strReturn,int iReturnLength, IntPtr hwndCallback);

public void Close()
{
_command = "close MediaFile";
mciSendString(_command, null, 0, IntPtr.Zero);
isOpen = false;
}

public void Open(string sFileName)
{
_command = "open \"" + sFileName + "\" type mpegvideo alias MediaFile";
mciSendString(_command, null, 0, IntPtr.Zero);
isOpen = true;
}

public void Play(bool loop)
{
if(isOpen)
{
_command = "play MediaFile";
if (loop)
_command += " REPEAT";
mciSendString(_command, null, 0, IntPtr.Zero);
}
}

No comments:

Post a Comment