C # No class is provided for playing audio files such as MP3. To write audio file programs such as MP3, you must use third-party controls or classes. This article uses the API function mciSendString to compile a class for playing audio files such as MP3. The source code is as follows:
1. Media playing class composed of mciSendString using the API function.
Using System;
Using System. Runtime. InteropServices;
Using System. Text;
Using System. IO;
Namespace clsMCIPlay
{
///
/// Summary of clsMci.
///
Public class clsMCI
{
Public clsMCI ()
{
//
// TODO: add the constructor logic here
//
}
// Define the string variable used by the API Function
[Financialas (UnmanagedType. ByValTStr, SizeConst = 260)]
Private string Name = "";
[Financialas (UnmanagedType. ByValTStr, SizeConst = 128)]
Private string durLength = "";
[Financialas (UnmanagedType. LPTStr, SizeConst = 128)]
Private string TemStr = "";
Int ilong;
// Defines the enumerated variables of the playback status.
Public enum State
{
MPlaying = 1,
MPuase = 2,
MStop = 3
};
// Structure Variable
Public struct structMCI
{
Public bool bMut;
Public int iDur;
Public int iPos;
Public int iVol;
Public int iBal;
Public string iName;
Public State state;
};
Public structMCI mc = new structMCI ();
// Obtain the playback file attributes
Public string FileName
{
Get
{
Return mc. iName;
}
Set
{
// ASCIIEncoding asc = new ASCIIEncoding ();
Try
{
TemStr = "";
TemStr = TemStr. PadLeft (127, Convert. ToChar (""));
Name = Name. PadLeft (260, Convert. ToChar (""));
Mc. iName = value;
Ilong = APIClass. getw.pathname (mc. iName, Name, Name. Length );
Name = GetCurrPath (Name );
// Name = "open" + Convert. ToChar (34) + Name + Convert. ToChar (34) + "alias media ";
Name = "open" + Convert. ToChar (34) + Name + Convert. ToChar (34) + "alias media ";
Ilong = APIClass. mciSendString ("close all", TemStr, TemStr. Length, 0 );
Ilong = APIClass. mciSendString (Name, TemStr, TemStr. Length, 0 );
Ilong = APIClass. mciSendString ("set media time format milliseconds", TemStr, TemStr. Length, 0 );
Mc. state = State. mStop;
}
Catch
{
MessageBox. Show ("error! ");
}
}
}
// Play
Public void play ()
{
TemStr = "";
TemStr = TemStr. PadLeft (127, Convert. ToChar (""));
APIClass. mciSendString ("play media", TemStr, TemStr. Length, 0 );
Mc. state = State. mPlaying;
}
// Stop
Public void StopT ()
{
TemStr = "";
TemStr = TemStr. PadLeft (128, Convert. ToChar (""));
Ilong = APIClass. mciSendString ("close media", TemStr, 128, 0 );
Ilong = APIClass. mciSendString ("close all", TemStr, 128, 0 );
Mc. state = State. mStop;
}
Public void Puase ()
{
TemStr = "";
TemStr = TemStr. PadLeft (128, Convert. ToChar (""));
Ilong = APIClass. mciSendString ("pause media", TemStr, TemStr. Length, 0 );
Mc. state = State. mPuase;
}
Private string GetCurrPath (string name)
{
If (name. Length <1) return "";
Name = name. Trim ();
Name = name. Substring (0, name. Length-1 );
Return name;
}
// Total time
Public int Duration
{
Get
{
DurLength = "";
DurLength = durLength. PadLeft (128, Convert. ToChar (""));
APIClass. mciSendString ("status media length", durLength, durLength. Length, 0 );
DurLength = durLength. Trim ();
If (durLength = "") return 0;
Return (int) (Convert. ToDouble (durLength)/1000f );
}
}
// Current time
Public int CurrentPosition
{
Get
{
DurLength = "";
DurLength = durLength. PadLeft (128, Convert. ToChar (""));
APIClass. mciSendString ("status media position", durLength, durLength. Length, 0 );
Mc. iPos = (int) (Convert. ToDouble (durLength)/1000f );
Return mc. iPos;
}
}
}
Public class APIClass
{
[DllImport ("kernel32.dll", CharSet = CharSet. Auto)]
Public static extern int getaskpathname (
String lpszLongPath,
String delimiter file,
Int cchBuffer
);
[DllImport ("winmm. dll", EntryPoint = "mciSendString", CharSet = CharSet. Auto)]
Public static extern int mciSendString (
String lpstrCommand,
String lpstrReturnString,
Int uReturnLength,
Int hwndCallback
);
}
}
Ii. Simple code used to test media playback:
Using System;
Using System. Drawing;
Using System. Collections;
Using System. ComponentModel;
Using System. Windows. Forms;
Using System. Data;
Using System. Runtime. InteropServices;
Using System. Text;
Using System. IO;
Using clsMCIPlay;
Namespace MCIPlay
{
///
/// Summary of Form1.
///
Public class Form1: System. Windows. Forms. Form
{
Private System. ComponentModel. IContainer components;
Private System. Windows. Forms. Timer timer1;
Private System. Windows. Forms. Button Play;
Private System. Windows. Forms. Button Stop;
Private System. Windows. Forms. Button Puase;
Private System. Windows. Forms. Label PlayFileName;
Private System. Windows. Forms. Label Duration;
Private System. Windows. Forms. Label CurrentPosition;
Private System. Windows. Forms. OpenFileDialog openFileDialog1;
Private System. Windows. Forms. Button BrowserFile;
ClsMCI mp = new clsMCI ();
Public Form1 ()
{
//
// Required for Windows Form Designer support
//
InitializeComponent ();
//
// TODO: add Any constructor code after InitializeComponent calls
//
}
///
/// Clear all resources in use.
///
Protected override void Dispose (bool disposing)
{
If (disposing)
{
If (components! = Null)
{
Components. Dispose ();
}
}
Base. Dispose (disposing );
}
# Region code generated by Windows Form Designer
///
/// The designer supports the required methods-do not use the code editor to modify
/// Content of this method.
///
Private void InitializeComponent ()
{
This. components = new System. ComponentModel. Container ();
This. Play = new System. Windows. Forms. Button ();
This. PlayFileName = new System. Windows. Forms. Label ();
This. Duration = new System. Windows. Forms. Label ();
This. Stop = new System. Windows. Forms. Button ();
This. Puase = new System. Windows. Forms. Button ();
This. CurrentPosition = new System. Windows. Forms. Label ();
This. timer1 = new System. Windows. Forms. Timer (this. components );
This. openFileDialog1 = new System. Windows. Forms. OpenFileDialog ();
This. BrowserFile = new System. Windows. Forms. Button ();
This. SuspendLayout ();
//
// Play
//
This. Play. Location = new