首页 > Linux操作系统 > Linux操作系统 > 播放嵌入资源的声音文件
摘要:本文阐述了在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");
然后,我们就可以调用PlaySound方法来播放了:
PlayNativeRef.PlaySound(
ms.GetBuffer(),
IntPtr.Zero,
SoundFlags.Synchronous | SoundFlags.Memory); 
注意这里的SoundFlag要记得把Memory加上,表示第一个参数是指向内存中的声音文件镜像。播放的时候从内存中加载资源。
不过有一点要注意的,嵌入的声音文件不宜太多,否则会让程序集变得很庞大。
代码在这里下载
来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/14766526/viewspace-561432/ ,如需转载,请注明出处,否则将追究法律责任。