播放MP3的類(C#)

來源:互聯網
上載者:User
  1. 具體源碼如下:<轉載>      
  2.   一、使用API函數mciSendString構成的媒體播放類。      
  3. 程式碼      
  4. using System;      
  5. using System.Runtime.InteropServices;       
  6. using System.Text;       
  7. using System.IO ;       
  8. namespace clsMCIPlay      
  9. {      
  10. /// <SUMMARY>   
  11. /// clsMci 的摘要說明。   
  12. /// </SUMMARY>   
  13. public class clsMCI      
  14.  {      
  15. public clsMCI()      
  16.   {      
  17. //   
  18. // TODO: 在此處添加建構函式邏輯   
  19. //   
  20.   }      
  21. //定義API函數使用的字串變數    
  22.   [MarshalAs(UnmanagedType.ByValTStr,SizeConst=260)]      
  23. private string Name = "" ;      
  24.   [MarshalAs(UnmanagedType.ByValTStr,SizeConst=128)]      
  25. private string durLength = "" ;      
  26.   [MarshalAs(UnmanagedType.LPTStr,SizeConst=128)]      
  27. private string TemStr ="";      
  28. int ilong;      
  29. //定義播放狀態枚舉變數   
  30. public enum State      
  31.   {      
  32.    mPlaying = 1,      
  33.    mPuase = 2,      
  34.    mStop = 3      
  35.   };      
  36. //結構變數   
  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 state;      
  46.   };      
  47. public structMCI mc =new structMCI() ;      
  48. //取得播放檔案屬性   
  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.GetShortPathName(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("出錯錯誤!");       
  76.     }      
  77.    }      
  78.   }      
  79. //播放   
  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. //停止   
  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. //總時間   
  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. //目前時間   
  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 GetShortPathName (      
  140. string lpszLongPath,      
  141. string shortFile,      
  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. }  
相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.