现在的位置: 首页 > 综合 > 正文

用 winmm 播放音乐

2012年11月30日 ⁄ 综合 ⁄ 共 2939字 ⁄ 字号 评论关闭

(一). 说明

      一个播放音乐的类,直接COPY就可以用了.
      可用于自己编写的游戏等一些需要播放音乐的地方. 只要给它正在的文件名
      就OK了.   具体看下面代码.

(二).示例图片

   

(三).代码      

using System;
//using System.Drawing;
using System.Collections;
using System.ComponentModel;
//using System.Windows.Forms;
//using System.Data;
using System.Runtime.InteropServices;

namespace 智能象棋游戏
{
 /// <summary>
 /// 用于播放音乐
 /// </summary> 
 internal class Helpers
 {
     [Flags]
     public enum PlaySoundFlags : int
     {
          SND_SYNC = 0x0000,  /* play synchronously (default) */ //同步
          SND_ASYNC = 0x0001,  /* play asynchronously */ //异步
          SND_NODEFAULT = 0x0002,  /* silence (!default) if sound not found */
          SND_MEMORY = 0x0004,  /* pszSound points to a memory file */
          SND_LOOP = 0x0008,  /* loop the sound until next sndPlaySound */
          SND_NOSTOP = 0x0010,  /* don't stop any currently playing sound */
          SND_NOWAIT = 0x00002000, /* don't wait if the driver is busy */
          SND_ALIAS = 0x00010000, /* name is a registry alias */
          SND_ALIAS_ID = 0x00110000, /* alias is a predefined ID */
          SND_FILENAME = 0x00020000, /* name is file name */
          SND_RESOURCE = 0x00040004  /* name is resource name or atom */
   }

   [DllImport("winmm")]
   public static extern bool PlaySound( string szSound, IntPtr hMod, PlaySoundFlags flags );
}

 public class Sound
 {
       public static void Play( string strFileName )
      {
   //Helpers.PlaySound( strFileName, IntPtr.Zero, Helpers.PlaySoundFlags.SND_FILENAME |    Helpers.PlaySoundFlags.SND_ASYNC );
     switch(strFileName)
     {
          case "start": strFileName=@"../../sound/start.WAV";       break;
          case "back": strFileName=@"../../sound/back.WAV";         break;
          case "fall": strFileName=@"../../sound/fall.WAV";         break;    
          case "huiqi": strFileName=@"../../sound/huiqi.WAV";       break;
          case "huiqiend": strFileName=@"../../sound/huiqiend.WAV"; break;
          case "jiangjun": strFileName=@"../../sound/jiangjun.WAV"; break;
          case "kill": strFileName=@"../../sound/kill.WAV";         break;
          case "win": strFileName=@"../../sound/win.WAV";           break;
          case "move": strFileName=@"../../sound/move.WAV";         break;
          case "hold": strFileName=@"../../sound/hold.WAV";         break;
          case "no": strFileName=@"../../sound/no.WAV";             break;
          case "popup": strFileName=@"../../sound/popup.WAV";       break;
          case "mayfall": strFileName=@"../../sound/mayfall.WAV";   break;
    }
   Helpers.PlaySound(strFileName, IntPtr.Zero, Helpers.PlaySoundFlags.SND_FILENAME | Helpers.PlaySoundFlags.SND_ASYNC);        //调用Dll方法,播放音乐
    }
  }  
}

(四).使用

    1.添加组件 (Interop.WMPLib) .  这个类库 VS.Net自带的,默认没有添加进来.

       方法:  展开项目文件夹,右击引用,添加引用,到Com组件栏,找到添加到项目中.

    2.新建一个*.cs文件,把这些代码拷贝进去,全部覆盖即可,并修改上例子中的播放文件名.

       就是这样的语句:  strFileName=@"../../sound/mayfall.WAV";     //一定要存在这个音乐文件,否则不能播放

    3.如果调用这个方法的地方与此类不在同一个命名空间. 要在用的地方打开类的命名空间.

        方法:  如这个示例类的命名空间应该这样写:    using 智能象棋游戏;

        最后就可以用了,用法极简单,直接调用此类的方法:  

              Sound.Play("move");   //其中参数自定义. 跟方法底层无关. 看一上类里面的swith语句就明白了.

 

(五).示例代码下载

    http://www.cnblogs.com/Files/ChengKing/播放声音.rar

   

             

   
   

抱歉!评论已关闭.