C # play MP3 music files

Source: Internet
Author: User

Online collection and adjustment, passed the test.

Record

Using system; using system. runtime. interopservices; using system. text; using system. io; using system. windows. forms; namespace windowsapplication7 {/// <summary> /// Summary of clsmci. /// </Summary> public class clsmci {public clsmci () {// todo: here, add the constructor logic //} // defines the string variable [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 playback status enumeration variable 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;}; Public structmci MC = new structmci (); // gets the playback File Attribute Public String filename {get {return MC. INAME;} set {// asciiencoding ASC = new asciiencoding (); try {temstr = ""; temstr = temstr. padleft (127, convert. tochar (""); name = ""; name = Name. padleft (260, convert. tochar (""); MC. INAME = value; ilong = apiclass. getmediapathname (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 {}}// play public void play () {temstr = ""; temstr = temstr. padleft (127, convert. tochar (""); apiclass. mcisendstring ("play media", temstr, temstr. length, 0); MC. state = state. mplaying;} 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); ilong = apiclass. mcisendstring (name, temstr, temstr. length, 0); // ilong = apiclass. mcisendstring ("set media time format milliseconds", temstr, temstr. length, 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); durlength = durlength. trim (); If (durlength = "") {return MC. IPOs = 0;} MC. IPOs = unchecked (INT) (convert. toint32 (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 );}}

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.