音乐操作类
1 public class clsMCI 2 { 3 public clsMCI() 4 { 5 // 6 // TODO: 在此处添加构造函数逻辑 7 // 8 } 9 10 //定义API函数使用的字符串变量 11 [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 260)] 12 private string Name = ""; 13 [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 128)] 14 private string durLength = ""; 15 [MarshalAs(UnmanagedType.LPTStr, SizeConst = 128)] 16 private string TemStr = ""; 17 int ilong; 18 //定义播放状态枚举变量 19 public enum State 20 { 21 mPlaying = 1, 22 mPuase = 2, 23 mStop = 3 24 }; 25 //结构变量 26 public struct structMCI 27 { 28 public bool bMut; 29 public int iDur; 30 public int iPos; 31 public int iVol; 32 public int iBal; 33 public string iName; 34 public State state; 35 }; 36 37 public structMCI mc = new structMCI(); 38 39 //取得播放文件属性 40 public string FileName 41 { 42 get 43 { 44 return mc.iName; 45 } 46 47 set 48 { 49 //ASCIIEncoding asc = new ASCIIEncoding(); 50 try 51 { 52 TemStr = ""; 53 TemStr = TemStr.PadLeft(127, Convert.ToChar(" ")); 54 Name = Name.PadLeft(260, Convert.ToChar(" ")); 55 mc.iName = value; 56 ilong = APIClass.GetShortPathName(mc.iName, Name, Name.Length); 57 Name = GetCurrPath(Name); 58 //Name = "open " + Convert.ToChar(34) + Name + Convert.ToChar(34) + " alias media"; 59 Name = "open " + Convert.ToChar(34) + Name + Convert.ToChar(34) + " alias media"; 60 ilong = APIClass.mciSendString("close all", TemStr, TemStr.Length, 0); 61 ilong = APIClass.mciSendString(Name, TemStr, TemStr.Length, 0); 62 ilong = APIClass.mciSendString("set media time format milliseconds", TemStr, TemStr.Length, 0); 63 mc.state = State.mStop; 64 } 65 catch 66 { 67 MessageBox.Show("出错错误!"); 68 } 69 } 70 } 71 72 //播放 73 public void play() 74 { 75 TemStr = ""; 76 TemStr = TemStr.PadLeft(127, Convert.ToChar(" ")); 77 APIClass.mciSendString("play media", TemStr, TemStr.Length, 0); 78 mc.state = State.mPlaying; 79 } 80 //停止 81 public void StopT() 82 { 83 TemStr = ""; 84 TemStr = TemStr.PadLeft(128, Convert.ToChar(" ")); 85 ilong = APIClass.mciSendString("close media", TemStr, 128, 0); 86 ilong = APIClass.mciSendString("close all", TemStr, 128, 0); 87 mc.state = State.mStop; 88 89 } 90 91 public void Puase() 92 { 93 TemStr = ""; 94 TemStr = TemStr.PadLeft(128, Convert.ToChar(" ")); 95 ilong = APIClass.mciSendString("pause media", TemStr, TemStr.Length, 0); 96 mc.state = State.mPuase; 97 } 98 private string GetCurrPath(string name) 99 { 100 if (name.Length < 1) return ""; 101 name = name.Trim(); 102 name = name.Substring(0, name.Length - 1); 103 return name; 104 } 105 //总时间 106 public int Duration 107 { 108 get 109 { 110 durLength = ""; 111 durLength = durLength.PadLeft(128, Convert.ToChar(" ")); 112 APIClass.mciSendString("status media length", durLength, durLength.Length, 0); 113 durLength = durLength.Trim(); 114 if (durLength == "") return 0; 115 return (int)(Convert.ToDouble(durLength) / 1000f); 116 } 117 } 118 119 //当前时间 120 public int CurrentPosition 121 { 122 get 123 { 124 durLength = ""; 125 durLength = durLength.PadLeft(128, Convert.ToChar(" ")); 126 APIClass.mciSendString("status media position", durLength, durLength.Length, 0); 127 durLength = durLength.Trim(); 128 if (durLength == "") return 0; 129 mc.iPos = (int)(Convert.ToDouble(durLength) / 1000f); 130 return mc.iPos; 131 } 132 set 133 { 134 string Command = String.Format("Seek Media to {0}", value * 1000); 135 APIClass.mciSendString(Command, null, 0, 0);//使播放停止 136 mc.state = State.mStop; 137 mc.iPos = value * 1000; 138 play(); 139 } 140 } 141 142 143 144 } 145 146 public class APIClass 147 { 148 149 [DllImport("kernel32.dll", CharSet = CharSet.Auto)] 150 public static extern int GetShortPathName( 151 string lpszLongPath, 152 string shortFile, 153 int cchBuffer 154 ); 155 156 [DllImport("winmm.dll", EntryPoint = "mciSendString", CharSet = CharSet.Auto)] 157 public static extern int mciSendString( 158 string lpstrCommand, 159 string lpstrReturnString, 160 int uReturnLength, 161 int hwndCallback 162 ); 163 164 }