柔城

SOSOFT articles

C#播放MP3源代码

代码如下:

  1 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 
 20 //定义API函数使用的字符串变量 
 21   [MarshalAs(UnmanagedType.ByValTStr,SizeConst=260)]
 22 private string Name = "" ;
 23 [MarshalAs(UnmanagedType.ByValTStr,SizeConst=128)]
 24 private string durLength = "" ;
 25 [MarshalAs(UnmanagedType.LPTStr,SizeConst=128)]
 26 private string TemStr ="";
 27 int ilong;
 28 //定义播放状态枚举变量
 29   public enum State
 30 {
 31 mPlaying = 1,
 32 mPuase = 2,
 33 mStop = 3
 34 };
 35 //结构变量
 36   public struct structMCI 
 37 {
 38 public bool bMut;
 39 public int iDur;
 40 public int iPos;
 41 public int iVol;
 42 public int iBal;
 43 public string iName;
 44 public State state;
 45 };
 46 
 47 public structMCI mc =new structMCI() ;
 48 
 49 //取得播放文件属性
 50   public string FileName
 51 {
 52 get
 53 {
 54 return mc.iName;
 55 }
 56 set
 57 {
 58 //ASCIIEncoding asc = new ASCIIEncoding(); 
 59     try
 60 {
 61 TemStr =""; 
 62 TemStr = TemStr.PadLeft(127,Convert.ToChar(" "));
 63 Name = Name.PadLeft(260,Convert.ToChar(" ")) ;
 64 mc.iName = value; 
 65 ilong = APIClass.GetShortPathName(mc.iName,Name, Name.Length);
 66 Name = GetCurrPath(Name);
 67 //Name = "open " + Convert.ToChar(34) + Name + Convert.ToChar(34) + " alias media";
 68      Name = "open " + Convert.ToChar(34) + Name + Convert.ToChar(34) + " alias media";
 69 ilong = APIClass.mciSendString("close all", TemStr, TemStr.Length , 0);
 70 ilong = APIClass.mciSendString( Name, TemStr, TemStr.Length, 0);
 71 ilong = APIClass.mciSendString("set media time format milliseconds", TemStr, TemStr.Length , 0);
 72 mc.state = State.mStop; 
 73 }
 74 catch
 75 {
 76 }
 77 }
 78 }
 79 //播放  http://sosoft.cnblogs.com/
 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 
 97 public void Puase()
 98 {
 99 TemStr = "";
100 TemStr = TemStr.PadLeft(128,Convert.ToChar(" "));
101 ilong = APIClass.mciSendString("pause media", TemStr, TemStr.Length, 0);
102 mc.state = State.mPuase ; 
103 }
104 private string GetCurrPath(string name)
105 {
106 if(name.Length <1) return ""; 
107 name = name.Trim();
108 name = name.Substring(0,name.Length-1);
109 return name;
110 }
111 //总时间
112   public int Duration
113 {
114 get
115 {
116 durLength = "";
117 durLength = durLength.PadLeft(128,Convert.ToChar(" ")) ;
118 APIClass.mciSendString("status media length", durLength, durLength.Length, 0);
119 durLength = durLength.Trim();
120 if(durLength == "") return 0;
121 return (int)(Convert.ToDouble(durLength) / 1000f); 
122 }
123 }
124 
125 //当前时间
126   public int CurrentPosition
127 {
128 get
129 {
130 durLength = "";
131 durLength = durLength.PadLeft(128,Convert.ToChar(" ")) ;
132 APIClass.mciSendString("status media position", durLength, durLength.Length, 0);
133 mc.iPos = (int)(Convert.ToDouble(durLength) / 1000f);
134 return mc.iPos;
135 }
136 }
137 }
138 
139 public class APIClass
140 {
141 [DllImport("kernel32.dll", CharSet = CharSet.Auto)]
142 public static extern int GetShortPathName (
143 string lpszLongPath,
144 string shortFile,
145 int cchBuffer
146 );
147 
148 [DllImport("winmm.dll", EntryPoint="mciSendString", CharSet = CharSet.Auto)]
149 public static extern int mciSendString (
150 string lpstrCommand,
151 string lpstrReturnString,
152 int uReturnLength,
153 int hwndCallback
154 );
155 }
156 }
157 
158 
159 
160 
161 
162 clsMCI cm = new clsMCI();
163             cm.FileName = "keleyi.mp3";//文件名
164             cm.play();

 

posted on 2014-07-01 23:07  柔城  阅读(4924)  评论(1编辑  收藏  举报

导航