WinCE系统播放wav声音文件的实现方法

  1. 标  题: WinCE系统播放wav声音文件的实现方法 
  2. 发信站: 放鹤亭 BBS (Tue Apr 15 12:55:40 2008), 本站(bbs.cumt.edu.cn) 
  3. 此示例在WinCE5.0 + .Net CF2.0环境下运行成功 
  4. 此示例需要引用下面的命名空间: 
  5. System 
  6. System.IO 
  7. System.Reflection 
  8. System.Runtime.InteropServices 
  9. System.Windows.Forms 
  10. 此示例演示如何使用平台调用来播放两个WAV文件:一个为嵌入的资源,另一个为内容。 
  11. 此示例定义了一个Sound类,该类通过WinCE的CoreDll.dll提供下面的本机代码功能: 
  12. 1、使用文件名或流播放声音的平台调用方法声明。 
  13. 2、用于在平台调用方法调用中传递参数的位值枚举。 
  14. 3、Play方法,用于调用正确的平台调用方法来播放单独的文件或嵌入的资源。 
  15. 第一步:将 Sound 类添加到项目中。 
  16. public class Sound 
  17.     private byte[] m_soundBytes; 
  18.     private string m_fileName; 
  19.     private enum Flags { 
  20.         SND_SYNC = 0x0000,  /* play synchronously (default) */ 
  21.         SND_ASYNC = 0x0001,  /* play asynchronously */ 
  22.         SND_NODEFAULT = 0x0002,  /* silence (!default) if sound not found */ 
  23.         SND_MEMORY = 0x0004,  /* pszSound points to a memory file */ 
  24.         SND_LOOP = 0x0008,  /* loop the sound until next sndPlaySound */ 
  25.         SND_NOSTOP = 0x0010,  /* don't stop any currently playing sound */ 
  26.         SND_NOWAIT = 0x00002000, /* don't wait if the driver is busy */ 
  27.         SND_ALIAS = 0x00010000, /* name is a registry alias */ 
  28.         SND_ALIAS_ID = 0x00110000, /* alias is a predefined ID */ 
  29.         SND_FILENAME = 0x00020000, /* name is file name */ 
  30.         SND_RESOURCE = 0x00040004  /* name is resource name or atom */ 
  31.     } 
  32.     [DllImport("CoreDll.DLL", EntryPoint="PlaySound", SetLastError=true)] 
  33.     private extern static int WCE_PlaySound(string szSound, IntPtr hMod, int flags); 
  34.     [DllImport("CoreDll.DLL", EntryPoint="PlaySound", SetLastError=true)] 
  35.     private extern static int WCE_PlaySoundBytes (byte[] szSound, IntPtr hMod, int flags); 
  36.     /// <summary> 
  37.     /// Construct the Sound object to play sound data from the specified file. 
  38.     /// </summary> 
  39.     public Sound (string fileName) { 
  40.         m_fileName = fileName; 
  41.     } 
  42.     /// <summary> 
  43.     /// Construct the Sound object to play sound data from the specified stream. 
  44.     /// </summary> 
  45.     public Sound(Stream stream)    { 
  46.         // read the data from the stream 
  47.         m_soundBytes = new byte [stream.Length]; 
  48.         stream.Read(m_soundBytes, 0,(int)stream.Length); 
  49.     } 
  50.     /// <summary> 
  51.     /// Play the sound 
  52.     /// </summary> 
  53.     public void Play () { 
  54.         // if a file name has been registered, call WCE_PlaySound, 
  55.         //  otherwise call WCE_PlaySoundBytes 
  56.         if (m_fileName != null
  57.             WCE_PlaySound(m_fileName, IntPtr.Zero, (int) (Flags.SND_ASYNC | Flags.SND_FILENAME)); 
  58.         else 
  59.             WCE_PlaySoundBytes (m_soundBytes, IntPtr.Zero, (int) (Flags.SND_ASYNC | Flags.SND_MEMORY)); 
  60.     } 
  61. 第二步:添加用于创建 Sound 类的实例和播放文件的方法,例如,在某按钮的 Click 事件中。 
  62. private void btnEmbedded_Click(object sender, System.EventArgs e) { 
  63.     Sound sound = new Sound (Assembly.GetExecutingAssembly().GetManifestResourceStream("SoundSample.chimes.wav")); 
  64.     sound.Play(); 
  65. private void btnFile_Click(object sender, System.EventArgs e) { 
  66.     Sound sound = new Sound (@"Program Files/SoundSample/dingdang.wav"); 
  67.     sound.Play(); 
  68. 编译代码 
  69. OK 
 
  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值