C # play MP3 source code

Source: Internet
Author: User

The Code is as follows:

1 using System; 2 using System. runtime. interopServices; 3 using System. text; 4 using System. IO; 5 using System. windows. forms; 6 namespace WindowsApplication7 7 {8 /// <summary> 9 /// summary of clsMci. 10 /// </summary> 11 public class clsMCI 12 {13 public clsMCI () 14 {15 // 16 // TODO: add the constructor logic 17 // 18} 19 20 // define the string variable 21 [exploralas (UnmanagedType. byValTStr, SizeConst = 260)] 22 private string Name = ""; 23 [financialas (UnmanagedType. byValTStr, SizeConst = 128)] 24 private string durLength = ""; 25 [financialas (UnmanagedType. LPTStr, SizeConst = 128)] 26 private string TemStr = ""; 27 int ilong; 28 // defines the playback status enumeration variable 29 public enum State 30 {31 mPlaying = 1, 32 mPuase = 2, 33 mStop = 3 34}; 35 // Structure Variable 36 public struct structMCI 37 {38 public bool bMut; 39 public int iDur; 40 public int iPos; 41 public int iVol; 42 public int iBal; 43 public string iName; 44 public State state State; 45}; 46 47 public structMCI mc = new structMCI (); 48 49 // get the playback File Attribute 50 public string FileName 51 {52 get 53 {54 return mc. iName; 55} 56 set 57 {58 // ASCIIEncoding asc = new ASCIIEncoding (); 59 try 60 {61 TemStr = ""; 62 TemStr = TemStr. padLeft (127, Convert. toChar (""); 63 Name = Name. padLeft (260, Convert. toChar (""); 64 mc. iName = value; 65 ilong = APIClass. getmediapathname (mc. iName, Name, Name. length); 66 Name = GetCurrPath (Name); 67 // Name = "open" + Convert. toChar (34) + Name + Convert. toChar (34) + "alias media"; 68 Name = "open" + Convert. toChar (34) + Name + Convert. toChar (34) + "alias media"; 69 ilong = APIClass. mciSendString ("close all", TemStr, TemStr. length, 0); 70 ilong = APIClass. mciSendString (Name, TemStr, TemStr. length, 0); 71 ilong = APIClass. mciSendString ("set media time format milliseconds", TemStr, TemStr. length, 0); 72 mc. state = State. mStop; 73} 74 catch 75 {76} 77} 78} 79 // play http://sosoft.cnblogs.com/ 80 public void play () 81 {82 TemStr = ""; 83 TemStr = TemStr. padLeft (127, Convert. toChar (""); 84 APIClass. mciSendString ("play media", TemStr, TemStr. length, 0); 85 mc. state = State. mPlaying; 86} 87 // stop 88 public void StopT () 89 {90 TemStr = ""; 91 TemStr = TemStr. padLeft (128, Convert. toChar (""); 92 ilong = APIClass. mciSendString ("close media", TemStr, 128, 0); 93 ilong = APIClass. mciSendString ("close all", TemStr, 128, 0); 94 mc. state = State. mStop; 95} 96 97 public void Puase () 98 {99 TemStr = ""; 100 TemStr = TemStr. padLeft (128, Convert. toChar (""); 101 ilong = APIClass. mciSendString ("pause media", TemStr, TemStr. length, 0); 102 mc. state = State. mPuase; 103} 104 private string GetCurrPath (string name) 105 {106 if (name. length <1) return "; 107 name = name. trim (); 108 name = name. substring (0, name. length-1); 109 return name; 110} 111 // total time 112 public int Duration113 {114 get115 {116 durLength = ""; 117 durLength = durLength. padLeft (128, Convert. toChar (""); 118 APIClass. mciSendString ("status media length", durLength, durLength. length, 0); 119 durLength = durLength. trim (); 120 if (durLength = "") return 0; 121 return (int) (Convert. toDouble (durLength)/1000f); 122} 123 124 // current time 125 public int CurrentPosition127 {126 get129 {128 durLength = ""; 130 durLength = durLength. padLeft (128, Convert. toChar (""); 132 APIClass. mciSendString ("status media position", durLength, durLength. length, 0); 133 mc. iPos = (int) (Convert. toDouble (durLength)/1000f); 134 return mc. iPos; 135} 136} 137} 138 139 public class APIClass140 {141 [DllImport ("kernel32.dll", CharSet = CharSet. auto)] 142 public static extern int getaskpathname (143 string lpszLongPath, 144 string partition file, 145 int cchBuffer146); 147 148 [DllImport ("winmm. dll ", EntryPoint =" mciSendString ", CharSet = CharSet. auto)] 149 public static extern int mciSendString (150 string lpstrCommand, 151 string lpstrReturnString, 152 int uReturnLength, 153 int hwndCallback154 ); 155} 156} 157 158 159 160 161 162 clsMCI cm = new clsMCI (); 163. fileName = "keleyi.pdf"; // file name 164. play ();

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.