欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

C#实现简单播放mp3的方法

程序员文章站 2023-12-12 14:51:22
本文实例讲述了c#实现简单播放mp3的方法。分享给大家供大家参考。具体实现方法如下: /// /// 测试 ///

本文实例讲述了c#实现简单播放mp3的方法。分享给大家供大家参考。具体实现方法如下:

/// <summary>
/// 测试
/// </summary>
class testdemo
{
 public void test()
 {
  clsmci cm = new clsmci();
  cm.filename = "alarm.mp3";
  cm.play();
 }
}
/// <summary>
/// clsmci 的摘要说明。
/// </summary>
public class clsmci
{
 public clsmci()
 {
 }
 //定义api函数使用的字符串变量 
 [marshalas(unmanagedtype.byvaltstr,sizeconst=260)]
 private string name = "" ;
 [marshalas(unmanagedtype.byvaltstr,sizeconst=128)]
 private string durlength = "" ;
 [marshalas(unmanagedtype.lptstr,sizeconst=128)]
 private string temstr ="";
 int ilong;
 //定义播放状态枚举变量
 public enum state
 {
  mplaying = 1,
  mpuase = 2,
  mstop = 3
 };
 //结构变量
 public struct structmci 
 {
  public bool bmut;
  public int idur;
  public int ipos;
  public int ivol;
  public int ibal;
  public string iname;
  public state state;
 };
 public structmci mc =new structmci() ;
 //取得播放文件属性
 public string filename
 {
  get
  {
  return mc.iname;
  }
  set
  {
    try
    {
     temstr =""; 
     temstr = temstr.padleft(127,convert.tochar(" "));
     name = name.padleft(260,convert.tochar(" ")) ;
     mc.iname = value; 
     ilong = apiclass.getshortpathname(mc.iname,name, name.length);
     name = getcurrpath(name);
     name = "open " + convert.tochar(34) + name + convert.tochar(34) + " alias media";
     ilong = apiclass.mcisendstring("close all", temstr, temstr.length , 0);
     ilong = apiclass.mcisendstring( name, temstr, temstr.length, 0);
     ilong = apiclass.mcisendstring("set media time format milliseconds", temstr, temstr.length , 0);
     mc.state = state.mstop; 
    }
    catch
    {
    }
  }
 }
 //播放
 public void play()
 {
  temstr = "";
  temstr = temstr.padleft(127,convert.tochar(" "));
  apiclass.mcisendstring("play media", temstr, temstr.length , 0);
  mc.state = state.mplaying ;
 }
 //停止
 public void stopt()
 {
  temstr = "";
  temstr = temstr.padleft(128,convert.tochar(" "));
  ilong = apiclass.mcisendstring("close media", temstr, 128, 0);
  ilong = apiclass.mcisendstring("close all", temstr, 128, 0);
  mc.state = state.mstop ; 
 }
 public void puase()
 {
  temstr = "";
  temstr = temstr.padleft(128,convert.tochar(" "));
  ilong = apiclass.mcisendstring("pause media", temstr, temstr.length, 0);
  mc.state = state.mpuase ; 
 }
 private string getcurrpath(string name)
 {
  if(name.length <1) return ""; 
  name = name.trim();
  name = name.substring(0,name.length-1);
  return name;
 }
 //总时间
 public int duration
 {
  get
  {
   durlength = "";
   durlength = durlength.padleft(128,convert.tochar(" ")) ;
   apiclass.mcisendstring("status media length", durlength, durlength.length, 0);
   durlength = durlength.trim();
   if(durlength == "") return 0;
   return (int)(convert.todouble(durlength) / 1000f); 
  }
 }
 //当前时间
 public int currentposition
 {
  get
  {
   durlength = "";
   durlength = durlength.padleft(128,convert.tochar(" ")) ;
   apiclass.mcisendstring("status media position", durlength, durlength.length, 0);
   mc.ipos = (int)(convert.todouble(durlength) / 1000f);
   return mc.ipos;
  }
 }
}
public class apiclass
{
  [dllimport("kernel32.dll", charset = charset.auto)]
  public static extern int getshortpathname (
   string lpszlongpath,
   string shortfile,
   int cchbuffer
);
[dllimport("winmm.dll", entrypoint="mcisendstring", charset = charset.auto)]
public static extern int mcisendstring (
   string lpstrcommand,
   string lpstrreturnstring,
   int ureturnlength,
   int hwndcallback
  );
}

希望本文所述对大家的c#程序设计有所帮助。

上一篇:

下一篇: