c#用mcisendstring API播放mp3

 
  1. using System;   
  2. using System.Collections.Generic;   
  3. using System.Text;   
  4. using System.Runtime.InteropServices;   
  5. using System.IO;   
  6. namespace WindowsApplication2   
  7. {   
  8.     class Media   
  9.     {   
  10.         [DllImport("winmm.dll", EntryPoint = "mciSendString", CharSet = CharSet.Auto)]   
  11.         private static extern int mciSendString(   
  12.             string lpstrCommand,   
  13.             string lpstrReturnString,   
  14.             int uReturnLength,   
  15.             int hwndCallback   
  16.             );   
  17.   
  18.         [DllImport("winmm.dll", EntryPoint = "mciGetDeviceID", CharSet = CharSet.Auto)]   
  19.         private static extern int mciGetDeviceID(string lpstrName);   
  20.         [DllImport("kernel32.dll", CharSet = CharSet.Auto)]   
  21.         public static extern int GetShortPathName(   
  22.          string lpszLongPath,   
  23.          string shortFile,   
  24.          int cchBuffer   
  25.         );   
  26.   
  27.         public enum PlayTypeName : byte  
  28.         {   
  29.             File = 1,   
  30.             CDAudio = 2,   
  31.             VCD = 3,   
  32.             RealPlay = 4   
  33.         }   
  34.   
  35.         public enum AudioSource : byte  
  36.         {   
  37.             H = 0,   
  38.             L = 1,   
  39.             R = 2   
  40.         }   
  41.   
  42.         public enum Playstate : byte  
  43.         {   
  44.             Stopped = 1,   
  45.             Playing = 2,   
  46.             Pause = 3   
  47.         }   
  48.   
  49.         public enum PlayStyle : byte  
  50.         {   
  51.             顺序 = 1,   
  52.             随机 = 2,   
  53.             循环 = 3   
  54.         }   
  55.   
  56.         public PlayTypeName PlayType;   
  57.   
  58.         public int Temp; //零时变量 工程中将使用   
  59.   
  60.          public String SongName;  //储存当前真正播放的歌曲名称   
  61.   
  62.          public string PreSongName;   
  63.   
  64.         public string NextSongName;   
  65.   
  66.         public int SongIndex; //储存当前播放的歌曲列表的位置   
  67.   
  68.          public int totalSong;   
  69.   
  70.         public PlayStyle PlayerStyle;//播放模式   
  71.   
  72.          public int Valume;//音量大小   
  73.   
  74.          public AudioSource audiosource;   
  75.   
  76.         public bool IsSlowly;//播放速度          
  77.   
  78.          /// <summary>   
  79.         /// 获取DeviceID   
  80.         /// </summary>   
  81.         /// <returns>返回设备类型</returns>   
  82.   
  83.         public int GetDeviceID()   
  84.         {   
  85.             return mciGetDeviceID("NOWMUSIC");   
  86.         }   
  87.   
  88.         /// <summary>   
  89.         /// 根据文件名,确定设备   
  90.          /// </summary>   
  91.         /// <param name="ff">文件名</param>   
  92.         /// <returns></returns>   
  93.   
  94.         public string GetDriverID(string ff)   
  95.         {   
  96.             string result = "";   
  97.             ff = ff.ToUpper().Trim();   
  98.             switch (ff.Substring(ff.Length - 3))   
  99.             {   
  100.                 case "MID":   
  101.                     result = "Sequencer";   
  102.                     break;   
  103.   
  104.                 case "RMI":   
  105.                     result = "Sequencer";   
  106.                     break;   
  107.   
  108.                 case "IDI":   
  109.                     result = "Sequencer";   
  110.                     break;   
  111.   
  112.                 case "WAV":   
  113.                     result = "Waveaudio";   
  114.                     break;   
  115.   
  116.                 case "ASX":   
  117.                     result = "MPEGVideo2";   
  118.                     break;   
  119.   
  120.                 case "IVF":   
  121.                     result = "MPEGVideo2";   
  122.                     break;   
  123.   
  124.                 case "LSF":   
  125.                     result = "MPEGVideo2";   
  126.                     break;   
  127.   
  128.                 case "LSX":   
  129.                     result = "MPEGVideo2";   
  130.                     break;   
  131.   
  132.                 case "P2V":   
  133.                     result = "MPEGVideo2";   
  134.                     break;   
  135.   
  136.                 case "WAX":   
  137.                     result = "MPEGVideo2";   
  138.                     break;   
  139.   
  140.                 case "WVX":   
  141.                     result = "MPEGVideo2";   
  142.                     break;   
  143.   
  144.                 case ".WM":   
  145.                     result = "MPEGVideo2";   
  146.                     break;   
  147.   
  148.                 case "WMX":   
  149.                     result = "MPEGVideo2";   
  150.                     break;   
  151.   
  152.                 case "WMP":   
  153.                     result = "MPEGVideo2";   
  154.                     break;   
  155.   
  156.                 case ".RM":   
  157.                     result = "RealPlay";   
  158.                     break;   
  159.   
  160.                 case "RAM":   
  161.                     result = "RealPlay";   
  162.                     break;   
  163.   
  164.                 case ".RA":   
  165.                     result = "RealPlay";   
  166.                     break;   
  167.   
  168.                 case "MVB":   
  169.                     result = "RealPlay";   
  170.                     break;   
  171.   
  172.                 default:   
  173.                     result = "MPEGVideo";   
  174.                     break;   
  175.             }   
  176.             return result;   
  177.         }   
  178.   
  179.         /// <summary>   
  180.         /// 打开MCI设备,   
  181.          /// </summary>   
  182.         /// <param name="FileName">要打开的文件名</param>   
  183.         /// <param name="Handle">mci设备的窗口句柄</param>   
  184.         /// <returns>传值代表成功与否</returns>   
  185.   
  186.         public bool OpenMusic(string FileName, IntPtr Handle)   
  187.         {   
  188.             bool result = false;               
  189.             string MciCommand;               
  190.             int RefInt;   
  191.   
  192.             CloseMusic();   
  193.   
  194.             ShortPathName = "";   
  195.             ShortPathName = ShortPathName.PadLeft(260, Convert.ToChar(" "));   
  196.             RefInt=GetShortPathName(FileName, ShortPathName, ShortPathName .Length );   
  197.             ShortPathName = GetCurrPath(ShortPathName);   
  198.             string DriverID = GetDriverID(ShortPathName);   
  199.             if (DriverID == "RealPlay")   
  200.                 return false;               
  201.   
  202.             MciCommand = string.Format("open {0} type {1} alias NOWMUSIC ", ShortPathName, DriverID);//"open " & RefShortName & " type " & DriverID & " alias NOWMUSIC"   
  203.   
  204.             if (DriverID == "AVIVideo" || DriverID == "MPEGVideo" || DriverID == "MPEGVideo2")   
  205.             {   
  206.                 if (Handle != IntPtr.Zero)   
  207.                 {   
  208.                     MciCommand = MciCommand + string.Format(" parent {0} style child ", Handle);// " parent " & Hwnd & " style child"   
  209.                 }   
  210.                 else  
  211.                 {   
  212.                     MciCommand = MciCommand + " style overlapped ";   
  213.                 }   
  214.             }   
  215.   
  216.             TemStr  = "";   
  217.             TemStr  = TemStr.PadLeft(128, Convert.ToChar(" "));   
  218.             RefInt = mciSendString(MciCommand, null, 0, 0);   
  219.             mciSendString("set NOWMUSIC time format milliseconds"null, 0, 0);   
  220.   
  221.             if (RefInt == 0)   
  222.             {   
  223.                 result = true;   
  224.                 SongName = Path.GetFileNameWithoutExtension(FileName);   
  225.             }   
  226.             return result;   
  227.         }   
  228.   
  229.         /// <summary>   
  230.         /// 播放音乐   
  231.          /// </summary>   
  232.         /// <returns></returns>   
  233.         public bool PlayMusic()   
  234.         {   
  235.             bool result = false;   
  236.             int RefInt = mciSendString("play NOWMUSIC"null, 0, 0);   
  237.             if (RefInt == 0)   
  238.             {   
  239.                 result = true;   
  240.                 SetValume(Valume);//当前音量大小   
  241.                    //检测播放速度   
  242.                    if (IsSlowly)   
  243.                     SetSpeed(800);   
  244.                 else  
  245.                     SetSpeed(1200);   
  246.                 //检测声道   
  247.                    switch ((int)audiosource)   
  248.                 {   
  249.                     case 0:   
  250.                         SetAudioSource(AudioSource.H);   
  251.                         break;   
  252.   
  253.                     case 1:   
  254.                         SetAudioSource(AudioSource.L);   
  255.                         break;   
  256.   
  257.                     case 2:   
  258.                         SetAudioSource(AudioSource.R);   
  259.                         break;   
  260.                 }   
  261.             }   
  262.             return result;   
  263.         }   
  264.   
  265.         /// <summary>   
  266.         /// 设置声音大小   
  267.          /// </summary>   
  268.         /// <param name="Valume">音量大小</param>   
  269.         /// <returns></returns>   
  270.         public bool SetValume(int Valume)   
  271.         {   
  272.             bool result = false;   
  273.             string MciCommand = string.Format("setaudio NOWMUSIC volume to {0}", Valume);   
  274.             int RefInt = mciSendString(MciCommand, null, 0, 0);   
  275.             if (RefInt == 0)   
  276.             {   
  277.                 result = true;   
  278.             }   
  279.   
  280.             return result;   
  281.         }   
  282.   
  283.         /// <summary>   
  284.         /// 设置播放速度   
  285.          /// </summary>   
  286.         /// <param name="Speed"></param>   
  287.         /// <returns></returns>   
  288.         public bool SetSpeed(int Speed)   
  289.         {   
  290.   
  291.             bool result = false;   
  292.             string MciCommand = string.Format("set NOWMUSIC speed to {0}", Speed);   
  293.             int RefInt = mciSendString(MciCommand, null, 0, 0);   
  294.             if (RefInt == 0)   
  295.                 result = true;   
  296.             return result;   
  297.         }   
  298.   
  299.         /// <summary>   
  300.         /// 设置声道   
  301.          /// </summary>   
  302.         /// <param name="audioSource"></param>   
  303.         /// <returns></returns>   
  304.   
  305.         public bool SetAudioSource(AudioSource audioSource)   
  306.         {   
  307.             bool result = false;   
  308.             string strSource = "";   
  309.             switch ((int)audioSource)   
  310.             {   
  311.                 case 1: strSource = "left"break;   
  312.   
  313.                 case 2: strSource = "right"break;   
  314.   
  315.                 case 0: strSource = "stereo"break;   
  316.             }   
  317.   
  318.             int RefInt = mciSendString("setaudio  NOWMUSIC source to " + strSource, null, 0, 0);   
  319.             if (RefInt == 0)   
  320.                 result = true;   
  321.             return result;   
  322.         }   
  323.   
  324.         /// <summary>   
  325.         /// 设置静音 True为静音,FALSE为取消静音   
  326.          /// </summary>   
  327.         /// <param name="AudioOff"></param>   
  328.         /// <returns></returns>   
  329.         public bool SetAudioOnOff(bool AudioOff)   
  330.         {   
  331.   
  332.             bool resut = false;   
  333.   
  334.             string OnOff;   
  335.   
  336.             if (AudioOff)   
  337.   
  338.                 OnOff = "off";   
  339.   
  340.             else  
  341.   
  342.                 OnOff = "on";   
  343.   
  344.             int RefInt = mciSendString("setaudio NOWMUSIC " + OnOff, null, 0, 0);   
  345.   
  346.             if (RefInt == 0)   
  347.   
  348.                 resut = true;   
  349.   
  350.             return resut;   
  351.   
  352.         }   
  353.   
  354.         /// <summary>   
  355.   
  356.         /// 关闭媒体   
  357.   
  358.         /// </summary>   
  359.   
  360.         /// <returns></returns>   
  361.   
  362.         public bool CloseMusic()   
  363.   
  364.         {   
  365.   
  366.             int RefInt = mciSendString("close NOWMUSIC"null, 0, 0);   
  367.   
  368.             if (RefInt == 0)   
  369.   
  370.                 return true;   
  371.   
  372.             return false;   
  373.   
  374.         }   
  375.   
  376.         /// <summary>   
  377.   
  378.         /// 暂停播放   
  379.   
  380.         /// </summary>   
  381.   
  382.         /// <returns></returns>   
  383.   
  384.         public bool PauseMusic()   
  385.   
  386.         {   
  387.   
  388.             int RefInt = mciSendString("pause NOWMUSIC"null, 0, 0);   
  389.   
  390.             if (RefInt == 0)   
  391.   
  392.                 return true;   
  393.   
  394.             return false;   
  395.   
  396.         }   
  397.   
  398.         /// <summary>   
  399.   
  400.         /// 获得当前媒体的状态是不是在播放   
  401.   
  402.         /// </summary>   
  403.   
  404.         /// <returns></returns>   
  405.   
  406.         public Playstate IsPlaying()   
  407.   
  408.         {   
  409.   
  410.             Playstate isPlaying=Playstate .Stopped ;   
  411.   
  412.             durLength = "";   
  413.   
  414.             durLength = durLength.PadLeft(128, Convert.ToChar(" "));   
  415.   
  416.            int RefInt=  mciSendString("status NOWMUSIC mode", durLength, durLength.Length, 0);   
  417.   
  418.             durLength = durLength.Trim();   
  419.   
  420.             if (durLength.Substring(0, 7) == "playing" || durLength.Substring(0, 2) == "播放")   
  421.   
  422.                 isPlaying = Playstate.Playing;   
  423.   
  424.             else if (durLength.Substring(0, 7) == "stopped" || durLength.Substring(0, 2) == "停止")   
  425.   
  426.                 isPlaying = Playstate.Stopped;   
  427.   
  428.             else isPlaying = Playstate.Pause;   
  429.   
  430.   
  431.   
  432.             return isPlaying;   
  433.   
  434.         }   
  435.   
  436.         /// <summary>   
  437.   
  438.         /// 获取当前播放进度 毫秒   
  439.   
  440.         /// </summary>   
  441.   
  442.         /// <returns></returns>   
  443.   
  444.         public int GetMusicPos()   
  445.   
  446.         {   
  447.   
  448.             durLength = "";   
  449.   
  450.             durLength = durLength.PadLeft(128, Convert.ToChar(" "));   
  451.   
  452.             mciSendString("status NOWMUSIC position", durLength ,durLength .Length , 0);   
  453.   
  454.             durLength = durLength.Trim();   
  455.   
  456.             if (string.IsNullOrEmpty(durLength))   
  457.   
  458.                 return 0;   
  459.   
  460.             else  
  461.   
  462.                 return (int)(Convert.ToDouble(durLength) );   
  463.   
  464.         }   
  465.   
  466.         /// <summary>   
  467.   
  468.         /// 获取当前播放进度 格式 00:00:00   
  469.   
  470.         /// </summary>   
  471.   
  472.         /// <returns></returns>   
  473.   
  474.         public string GetMusicPosString()   
  475.   
  476.         {   
  477.   
  478.             durLength = "";   
  479.   
  480.             durLength = durLength.PadLeft(128, Convert.ToChar(" "));   
  481.   
  482.             mciSendString("status NOWMUSIC position", durLength, durLength.Length, 0);   
  483.   
  484.             durLength = durLength.Trim();   
  485.   
  486.             if (string.IsNullOrEmpty(durLength))   
  487.   
  488.                 return "00:00:00";   
  489.   
  490.             else  
  491.   
  492.             {   
  493.   
  494.                 int s = Convert.ToInt32(durLength)/1000;   
  495.   
  496.                 int h = s /3600;   
  497.   
  498.                 int m = (s - (h * 3600)) / 60;   
  499.   
  500.                 s = s - (h * 3600 + m * 60);   
  501.   
  502.                 return string.Format("{0:D2}:{1:D2}:{2:D2}", h, m, s);   
  503.   
  504.             }   
  505.   
  506.         }   
  507.   
  508.         /// <summary>   
  509.   
  510.         /// 获取媒体的长度   
  511.   
  512.         /// </summary>   
  513.   
  514.         /// <returns></returns>   
  515.   
  516.         public int GetMusicLength()   
  517.   
  518.         {   
  519.   
  520.             durLength = "";   
  521.   
  522.             durLength = durLength.PadLeft(128, Convert.ToChar(" "));   
  523.   
  524.             mciSendString("status NOWMUSIC length", durLength, durLength.Length, 0);   
  525.   
  526.             durLength = durLength.Trim();   
  527.   
  528.             if (string.IsNullOrEmpty(durLength))   
  529.   
  530.                 return 0;   
  531.   
  532.             else  
  533.   
  534.                 return Convert.ToInt32(durLength);   
  535.   
  536.         }   
  537.   
  538.         /// <summary>   
  539.   
  540.         /// 获取媒体的长度 00:00:00   
  541.   
  542.         /// </summary>   
  543.   
  544.         /// <returns></returns>   
  545.   
  546.         public string GetMusicLengthString()   
  547.   
  548.         {   
  549.   
  550.             durLength = "";   
  551.   
  552.             durLength = durLength.PadLeft(128, Convert.ToChar(" "));   
  553.   
  554.             mciSendString("status NOWMUSIC length", durLength, durLength.Length, 0);   
  555.   
  556.             durLength = durLength.Trim();   
  557.   
  558.             if (string.IsNullOrEmpty(durLength))   
  559.   
  560.                 return "00:00:00";   
  561.   
  562.             else  
  563.   
  564.             {   
  565.   
  566.                 int s = Convert.ToInt32(durLength) / 1000;   
  567.   
  568.                 int h = s / 3600;   
  569.   
  570.                 int m = (s - (h * 3600)) / 60;   
  571.   
  572.                 s = s - (h * 3600 + m * 60);   
  573.   
  574.                 return string.Format("{0:D2}:{1:D2}:{2:D2}", h, m, s);   
  575.   
  576.             }   
  577.   
  578.   
  579.   
  580.         }   
  581.   
  582.   
  583.   
  584.         public bool SetMusicPos(int Position)   
  585.   
  586.         {   
  587.   
  588.             string MciCommand = string.Format("seek NOWMUSIC to {0}", Position );   
  589.   
  590.             int RefInt = mciSendString( MciCommand , null, 0, 0);   
  591.   
  592.             if (RefInt == 0)   
  593.   
  594.                 return true;   
  595.   
  596.             else  
  597.   
  598.                 return false;   
  599.   
  600.         }   
  601.   
  602.         private string GetCurrPath(string name)   
  603.   
  604.         {   
  605.   
  606.             if (name.Length < 1) return "";   
  607.   
  608.             name = name.Trim();   
  609.   
  610.             name = name.Substring(0, name.Length - 1);   
  611.   
  612.             return name;   
  613.   
  614.         }   
  615.   
  616.   
  617.   
  618.         [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 260)]   
  619.   
  620.         private string ShortPathName = "";   
  621.   
  622.         [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 128)]   
  623.   
  624.         private string durLength = "";   
  625.   
  626.         [MarshalAs(UnmanagedType.LPTStr, SizeConst = 128)]   
  627.   
  628.         private string TemStr = "";   
  629.   
  630.     }   
  631.   
  632. }  
  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值