播放MP3的类

来源:互联网 发布:简单源码 编辑:程序博客网 时间:2024/04/29 16:27
  1. 一、使用API函数mciSendString构成的媒体播放类。      
  2.      
  3. 程序代码      
  4.      
  5. using System;      
  6. using System.Runtime.InteropServices;       
  7. using System.Text;       
  8. using System.IO ;       
  9. namespace clsMCIPlay      
  10. {      
  11.  /// <SUMMARY>      
  12.  /// clsMci 的摘要说明。      
  13.  /// </SUMMARY>      
  14.  public class clsMCI      
  15.  {      
  16.   public clsMCI()      
  17.   {      
  18.    //      
  19.    // TODO: 在此处添加构造函数逻辑      
  20.    //      
  21.   }      
  22.      
  23.   //定义API函数使用的字符串变量       
  24.   [MarshalAs(UnmanagedType.ByValTStr,SizeConst=260)]      
  25.   private string Name = "" ;      
  26.   [MarshalAs(UnmanagedType.ByValTStr,SizeConst=128)]      
  27.   private string durLength = "" ;      
  28.   [MarshalAs(UnmanagedType.LPTStr,SizeConst=128)]      
  29.   private string TemStr ="";      
  30.   int ilong;      
  31.   //定义播放状态枚举变量      
  32.   public enum State      
  33.   {      
  34.    mPlaying = 1,      
  35.    mPuase = 2,      
  36.    mStop = 3      
  37.   };      
  38.   //结构变量      
  39.   public struct structMCI       
  40.   {      
  41.    public bool bMut;      
  42.    public int iDur;      
  43.    public int iPos;      
  44.    public int iVol;      
  45.    public int iBal;      
  46.    public string iName;      
  47.    public State state;      
  48.   };      
  49.      
  50.   public structMCI mc =new structMCI() ;      
  51.      
  52.   //取得播放文件属性      
  53.   public string FileName      
  54.   {      
  55.    get     
  56.    {      
  57.     return mc.iName;      
  58.    }      
  59.    set     
  60.    {      
  61.     //ASCIIEncoding asc = new ASCIIEncoding();       
  62.     try     
  63.     {      
  64.      TemStr ="";       
  65.      TemStr = TemStr.PadLeft(127,Convert.ToChar(" "));      
  66.      Name = Name.PadLeft(260,Convert.ToChar(" ")) ;      
  67.      mc.iName = value;       
  68.      ilong = APIClass.GetShortPathName(mc.iName,Name, Name.Length);      
  69.      Name = GetCurrPath(Name);      
  70.      //Name = "open " + Convert.ToChar(34) + Name + Convert.ToChar(34) + " alias media";      
  71.      Name = "open " + Convert.ToChar(34) + Name + Convert.ToChar(34) + " alias media";      
  72.      ilong = APIClass.mciSendString("close all", TemStr, TemStr.Length , 0);      
  73.      ilong = APIClass.mciSendString( Name, TemStr, TemStr.Length, 0);      
  74.      ilong = APIClass.mciSendString("set media time format milliseconds", TemStr, TemStr.Length , 0);      
  75.      mc.state = State.mStop;       
  76.     }      
  77.     catch     
  78.     {      
  79.      MessageBox.Show("出错错误!");       
  80.     }      
  81.    }      
  82.   }      
  83.   //播放      
  84.   public void play()      
  85.   {      
  86.    TemStr = "";      
  87.    TemStr = TemStr.PadLeft(127,Convert.ToChar(" "));      
  88.    APIClass.mciSendString("play media", TemStr, TemStr.Length , 0);      
  89.    mc.state = State.mPlaying ;      
  90.   }      
  91.   //停止      
  92.   public void StopT()      
  93.   {      
  94.    TemStr = "";      
  95.    TemStr = TemStr.PadLeft(128,Convert.ToChar(" "));      
  96.    ilong = APIClass.mciSendString("close media", TemStr, 128, 0);      
  97.    ilong = APIClass.mciSendString("close all", TemStr, 128, 0);      
  98.    mc.state = State.mStop ;       
  99.   }      
  100.      
  101.   public void Puase()      
  102.   {      
  103.    TemStr = "";      
  104.    TemStr = TemStr.PadLeft(128,Convert.ToChar(" "));      
  105.    ilong = APIClass.mciSendString("pause media", TemStr, TemStr.Length, 0);      
  106.    mc.state = State.mPuase ;       
  107.   }      
  108.   private string GetCurrPath(string name)      
  109.   {      
  110.    if(name.Length <1) return "";       
  111.    name = name.Trim();      
  112.    name = name.Substring(0,name.Length-1);      
  113.    return name;      
  114.   }      
  115.   //总时间      
  116.   public int Duration      
  117.   {      
  118.    get     
  119.    {      
  120.     durLength = "";      
  121.     durLength = durLength.PadLeft(128,Convert.ToChar(" ")) ;      
  122.     APIClass.mciSendString("status media length", durLength, durLength.Length, 0);      
  123.     durLength = durLength.Trim();      
  124.     if(durLength == ""return 0;      
  125.     return (int)(Convert.ToDouble(durLength) / 1000f);       
  126.    }      
  127.   }      
  128.      
  129.   //当前时间      
  130.   public int CurrentPosition      
  131.   {      
  132.    get     
  133.    {      
  134.     durLength = "";      
  135.     durLength = durLength.PadLeft(128,Convert.ToChar(" ")) ;      
  136.     APIClass.mciSendString("status media position", durLength, durLength.Length, 0);      
  137.     mc.iPos = (int)(Convert.ToDouble(durLength) / 1000f);      
  138.     return mc.iPos;      
  139.    }      
  140.   }      
  141.  }      
  142.      
  143.  public class APIClass      
  144.  {      
  145.   [DllImport("kernel32.dll", CharSet = CharSet.Auto)]      
  146.   public static extern int GetShortPathName (      
  147.    string lpszLongPath,      
  148.    string shortFile,      
  149.    int cchBuffer      
  150.   );      
  151.      
  152.   [DllImport("winmm.dll", EntryPoint="mciSendString", CharSet = CharSet.Auto)]      
  153.   public static extern int mciSendString (      
  154.    string lpstrCommand,      
  155.    string lpstrReturnString,      
  156.    int uReturnLength,      
  157.    int hwndCallback      
  158.   );      
  159.  }      
  160. }  
 
原创粉丝点击