A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© sunrise2 高级黑马   /  2014-7-16 23:33  /  945 人查看  /  1 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

  1. </blockquote></div><div class="blockcode"><blockquote>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. /// clsMci 的摘要说明。
  10. /// </summary>
  11.  public class clsMCI
  12. {
  13. public clsMCI()
  14. {
  15. //
  16. // TODO: 在此处添加构造函数逻辑
  17. //
  18.   }

  19. //定义API函数使用的字符串变量
  20.   [MarshalAs(UnmanagedType.ByValTStr,SizeConst=260)]
  21. private string Name = "" ;
  22. [MarshalAs(UnmanagedType.ByValTStr,SizeConst=128)]
  23. private string durLength = "" ;
  24. [MarshalAs(UnmanagedType.LPTStr,SizeConst=128)]
  25. private string TemStr ="";
  26. int ilong;
  27. //定义播放状态枚举变量
  28.   public enum State
  29. {
  30. mPlaying = 1,
  31. mPuase = 2,
  32. mStop = 3
  33. };
  34. //结构变量
  35.   public struct structMCI
  36. {
  37. public bool bMut;
  38. public int iDur;
  39. public int iPos;
  40. public int iVol;
  41. public int iBal;
  42. public string iName;
  43. public State state;
  44. };

  45. public structMCI mc =new structMCI() ;

  46. //取得播放文件属性
  47.   public string FileName
  48. {
  49. get
  50. {
  51. return mc.iName;
  52. }
  53. set
  54. {
  55. //ASCIIEncoding asc = new ASCIIEncoding();
  56.     try
  57. {
  58. TemStr ="";
  59. TemStr = TemStr.PadLeft(127,Convert.ToChar(" "));
  60. Name = Name.PadLeft(260,Convert.ToChar(" ")) ;
  61. mc.iName = value;
  62. ilong = APIClass.GetShortPathName(mc.iName,Name, Name.Length);
  63. Name = GetCurrPath(Name);
  64. //Name = "open " + Convert.ToChar(34) + Name + Convert.ToChar(34) + " alias media";
  65.      Name = "open " + Convert.ToChar(34) + Name + Convert.ToChar(34) + " alias media";
  66. ilong = APIClass.mciSendString("close all", TemStr, TemStr.Length , 0);
  67. ilong = APIClass.mciSendString( Name, TemStr, TemStr.Length, 0);
  68. ilong = APIClass.mciSendString("set media time format milliseconds", TemStr, TemStr.Length , 0);
  69. mc.state = State.mStop;
  70. }
  71. catch
  72. {
  73. }
  74. }
  75. }
  76. //播放  http://sosoft.cnblogs.com/
  77.   public void play()
  78. {
  79. TemStr = "";
  80. TemStr = TemStr.PadLeft(127,Convert.ToChar(" "));
  81. APIClass.mciSendString("play media", TemStr, TemStr.Length , 0);
  82. mc.state = State.mPlaying ;
  83. }
  84. //停止
  85.   public void StopT()
  86. {
  87. TemStr = "";
  88. TemStr = TemStr.PadLeft(128,Convert.ToChar(" "));
  89. ilong = APIClass.mciSendString("close media", TemStr, 128, 0);
  90. ilong = APIClass.mciSendString("close all", TemStr, 128, 0);
  91. mc.state = State.mStop ;
  92. }

  93. public void Puase()
  94. {
  95. TemStr = "";
  96. TemStr = TemStr.PadLeft(128,Convert.ToChar(" "));
  97. ilong = APIClass.mciSendString("pause media", TemStr, TemStr.Length, 0);
  98. mc.state = State.mPuase ;
  99. }
  100. private string GetCurrPath(string name)
  101. {
  102. if(name.Length <1) return "";
  103. name = name.Trim();
  104. name = name.Substring(0,name.Length-1);
  105. return name;
  106. }
  107. //总时间
  108.   public int Duration
  109. {
  110. get
  111. {
  112. durLength = "";
  113. durLength = durLength.PadLeft(128,Convert.ToChar(" ")) ;
  114. APIClass.mciSendString("status media length", durLength, durLength.Length, 0);
  115. durLength = durLength.Trim();
  116. if(durLength == "") return 0;
  117. return (int)(Convert.ToDouble(durLength) / 1000f);
  118. }
  119. }

  120. //当前时间
  121.   public int CurrentPosition
  122. {
  123. get
  124. {
  125. durLength = "";
  126. durLength = durLength.PadLeft(128,Convert.ToChar(" ")) ;
  127. APIClass.mciSendString("status media position", durLength, durLength.Length, 0);
  128. mc.iPos = (int)(Convert.ToDouble(durLength) / 1000f);
  129. return mc.iPos;
  130. }
  131. }
  132. }

  133. public class APIClass
  134. {
  135. [DllImport("kernel32.dll", CharSet = CharSet.Auto)]
  136. public static extern int GetShortPathName (
  137. string lpszLongPath,
  138. string shortFile,
  139. int cchBuffer
  140. );

  141. [DllImport("winmm.dll", EntryPoint="mciSendString", CharSet = CharSet.Auto)]
  142. public static extern int mciSendString (
  143. string lpstrCommand,
  144. string lpstrReturnString,
  145. int uReturnLength,
  146. int hwndCallback
  147. );
  148. }
  149. }
复制代码



1 个回复

倒序浏览
请问怎么用呀?
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马