播放MP3的类(C#)

来源:互联网 发布:被两个男人同时 知乎 编辑:程序博客网 时间:2024/04/29 03:45
  1. using System.Runtime.InteropServices;
    public static uint SND_ASYNC = 0x0001; // play asynchronously
    public static uint SND_FILENAME = 0x00020000; // name is file name
    [DllImport("winmm.dll")]
    public static extern uint mciSendString(string lpstrCommand,
        string lpstrReturnString, uint uReturnLength, uint hWndCallback);


    private void
    button1_Click(object sender, EventArgs e)
    {
        mciSendString(@"close temp_alias", null, 0, 0);
        mciSendString(@"open ""E:/音乐/周杰伦-东风破.mp3"" alias temp_alias",
            null, 0, 0);
        mciSendString("play temp_alias", null, 0, 0);
    }

**********************************

  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. }  
原创粉丝点击