Playing MP3 (C #)

Source: Internet
Author: User
  1. The source code is as follows: <reproduced>
  2. 1. Media playing class composed of mcisendstring using the API function.
  3. ProgramCode
  4. Using system;
  5. Using system. runtime. interopservices;
  6. Using system. text;
  7. Using system. IO;
  8. Namespace clsmciplay
  9. {
  10. /// <Summary>
  11. /// Summary of clsmci.
  12. /// </Summary>
  13. Public class clsmci
  14. {
  15. Public clsmci ()
  16. {
  17. //
  18. // Todo: add the constructor logic here
  19. //
  20. }
  21. // Define the string variable used by the API Function
  22. [Financialas (unmanagedtype. byvaltstr, sizeconst = 260)]
  23. Private string name = "";
  24. [Financialas (unmanagedtype. byvaltstr, sizeconst = 128)]
  25. Private string durlength = "";
  26. [Financialas (unmanagedtype. lptstr, sizeconst = 128)]
  27. Private string temstr = "";
  28. Int ilong;
  29. // Defines the enumerated variables of the playback status.
  30. Public Enum state
  31. {
  32. Mplaying = 1,
  33. Mpuase = 2,
  34. Mstop = 3
  35. };
  36. // Structure Variable
  37. Public struct structmci
  38. {
  39. Public bool bmut;
  40. Public int idur;
  41. Public int IPOs;
  42. Public int ivol;
  43. Public int ibal;
  44. Public String INAME;
  45. Public state;
  46. };
  47. Public structmci MC = new structmci ();
  48. // Obtain the playback file attributes
  49. Public String filename
  50. {
  51. Get
  52. {
  53. Return MC. INAME;
  54. }
  55. Set
  56. {
  57. // Asciiencoding ASC = new asciiencoding ();
  58. Try
  59. {
  60. Temstr = "";
  61. Temstr = temstr. padleft (127, convert. tochar (""));
  62. Name = Name. padleft (260, convert. tochar (""));
  63. MC. INAME = value;
  64. Ilong = apiclass. getw.pathname (MC. INAME, name, name. Length );
  65. Name = getcurrpath (name );
  66. // Name = "open" + convert. tochar (34) + name + convert. tochar (34) + "alias Media ";
  67. Name = "open" + convert. tochar (34) + name + convert. tochar (34) + "alias Media ";
  68. Ilong = apiclass. mcisendstring ("close all", temstr, temstr. length, 0 );
  69. Ilong = apiclass. mcisendstring (name, temstr, temstr. length, 0 );
  70. Ilong = apiclass. mcisendstring ("set media time format milliseconds", temstr, temstr. length, 0 );
  71. MC. State = state. mstop;
  72. }
  73. Catch
  74. {
  75. MessageBox. Show ("error! ");
  76. }
  77. }
  78. }
  79. // Play
  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. Public void puase ()
  97. {
  98. Temstr = "";
  99. Temstr = temstr. padleft (128, convert. tochar (""));
  100. Ilong = apiclass. mcisendstring ("Pause Media", temstr, temstr. length, 0 );
  101. MC. State = state. mpuase;
  102. }
  103. Private string getcurrpath (string name)
  104. {
  105. If (name. Length <1) Return "";
  106. Name = Name. Trim ();
  107. Name = Name. substring (0, name. Length-1 );
  108. Return name;
  109. }
  110. // Total time
  111. Public int duration
  112. {
  113. Get
  114. {
  115. Durlength = "";
  116. Durlength = durlength. padleft (128, convert. tochar (""));
  117. Apiclass. mcisendstring ("status media length", durlength, durlength. length, 0 );
  118. Durlength = durlength. Trim ();
  119. If (durlength = "") return 0;
  120. Return (INT) (convert. todouble (durlength)/1000f );
  121. }
  122. }
  123. // Current time
  124. Public int currentposition
  125. {
  126. Get
  127. {
  128. Durlength = "";
  129. Durlength = durlength. padleft (128, convert. tochar (""));
  130. Apiclass. mcisendstring ("status Media Position", durlength, durlength. length, 0 );
  131. MC. IPOs = (INT) (convert. todouble (durlength)/1000f );
  132. Return MC. IPOs;
  133. }
  134. }
  135. }
  136. Public class apiclass
  137. {
  138. [Dllimport ("kernel32.dll", charset = charset. Auto)]
  139. Public static extern int getaskpathname (
  140. String lpszlongpath,
  141. String delimiter file,
  142. Int cchbuffer
  143. );
  144. [Dllimport ("winmm. dll", entrypoint = "mcisendstring", charset = charset. Auto)]
  145. Public static extern int mcisendstring (
  146. String lpstrcommand,
  147. String lpstrreturnstring,
  148. Int ureturnlength,
  149. Int hwndcallback
  150. );
  151. }
  152. }

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.