黄季冬=>fox23

Freesc Huang & Smart Devices
数据加载中……
[CF.Skills]播放嵌入资源的声音文件
[CF.Skills]播放嵌入资源的声音文件

摘要:本文阐述了在Windows Mobile中如何播放潜入资源的声音文件
Keywords
PlaySound, Windows Mobile, Embedded Resources, p/invoke

要在Windows Mobile上播放嵌入资源(Embedded Resource)的声音文件,该怎么办呢?显然是要用到反射的,我查了一下MSDN,还好GetManifestResourceStream对移动设备是可用的。

首先我们需要一个类,通过P/invoke来帮助我们实现播放声音的功能:

   [Flags]
    
enum SoundFlags
    
{
        Alias 
= 0x00010000,
        Filename 
= 0x00020000,
        Synchronous 
= 0x00000000,
        Asynchronous 
= 0x00000001,
        Memory 
= 0x00000004,
        Loop 
= 0x00000008,
        NoStop 
= 0x00000010
    }


    
class PlayNativeRef
    
{
        [DllImport(
"CoreDll.DLL", EntryPoint = "PlaySound", SetLastError = true)]
        
public extern static int PlaySound(byte[] szSound, IntPtr hMod, SoundFlags flags);
    }

调用的时候,我们先要将该声音资源从程序集加载到内存中:

 MemoryStream ms =(MemoryStream) Assembly.GetExecutingAssembly().GetManifestResourceStream("PlayEmbeddedAudio.Resources.yuyinlangdu.wav");

Tips这里的资源名字,一定不要弄错,如果你不确信的话,可以通过GetManifestResourceNames方法来查看资源的名称。

然后,我们就可以调用PlaySound方法来播放了:

                PlayNativeRef.PlaySound(
                     ms.GetBuffer(),
                     IntPtr.Zero,
                     SoundFlags.Synchronous 
| SoundFlags.Memory);   


注意这里的SoundFlag要记得把Memory加上,表示第一个参数是指向内存中的声音文件镜像。播放的时候从内存中加载资源。

不过有一点要注意的,嵌入的声音文件不宜太多,否则会让程序集变得很庞大。

代码在这里下载

Enjoy it!

黄季冬<fox23>

2008/5/18

posted on 2008-05-18 02:30 Freesc Huang 阅读(676) 评论(2)  编辑 收藏 网摘 所属分类: Compact Framework

评论

#1楼 2008-07-08 14:28 roc196[未注册用户]

对于.NET CF 2.0,这是比较安全的~~~
以前1.0的时候要播文件~~~
    回复  引用    



发表评论

昵称: [登录] [注册]

主页:

邮箱:(仅博主可见)

评论内容:

  登录  注册

[使用Ctrl+Enter键快速提交评论]

0 1201740




相关文章:

相关链接: