Using System; Using System. Runtime. InteropServices; Using System. Text; Using System. IO; Namespace clsMCIPlay { /// <Summary> /// Summary of clsMci. /// </Summary> 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 ); } } |