DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Snippets has posted 5883 posts at DZone. View Full User Profile

Play MP3 Files

04.24.2007
| 6556 views |
  • submit to reddit
        
public class Player
{
  private string _command;
  private bool isOpen;
 [DllImport("winmm.dll")]
private static extern long mciSendString(string strCommand,StringBuilder strReturn,int iReturnLength, IntPtr hwndCallback);
  
public Player()
 {
   
 }
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);
   }
 }

}