我是靠谱客的博主 标致大山,最近开发中收集的这篇文章主要介绍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 
 

最后

以上就是标致大山为你收集整理的WinCE系统播放wav声音文件的实现方法 的全部内容,希望文章能够帮你解决WinCE系统播放wav声音文件的实现方法 所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(85)

评论列表共有 0 条评论

立即
投稿
返回
顶部