今天,项目中要求用声音来提示用户程序的某个状态被激活了,发现,.Net CF下没有System.Media这种好东东鸟~,于是乎,股沟、百毒最后再来个ZZK,发现MSDN和小镇的文章都有提及在PPC下如何播放声音(还有很多文章提到的方法差不多,但是都有网友反映不能成功播放,所以我在这里就过滤掉了),msdn中说道的文章,好多朋友反映是播放不出来,在此我没有去考证,我直接使用了小镇的代码,并且成功的播放了声音,所以,写下此篇随笔,一来做个备份,二来和大家分享一下~
今天,项目中要求用声音来提示用户程序的某个状态被激活了,发现,.Net CF下没有System.Media这种好东东鸟~,于是乎,股沟、百毒最后再来个ZZK,发现MSDN和小镇的文章都有提及在PPC下如何播放声音(还有很多文章提到的方法差不多,但是都有网友反映不能成功播放,所以我在这里就过滤掉了),msdn中说道的文章,好多朋友反映是播放不出来,在此我没有去考证,我直接使用了小镇的代码,并且成功的播放了声音,所以,写下此篇随笔,一来做个备份,二来和大家分享一下~
首先,我们建立一个.net cf 2.0下,名称为SoundPlay的WM5 ppc项目,当然语言我选择的是C#。
然后界面设计如下:其中:
lblFileName——Text属性:文件地址
btnOpen——Text属性:浏览文件
btnPlay——Text属性:播放声音
新建一个类:Sound,代码和小镇的差不多,不过我做过一小点修改,所以也贴出来,方便大家查阅:
Sound
using System;
using System.Runtime.InteropServices;
using System.IO;
using System.Collections.Generic;
using System.Text;
namespace SoundPlay
{
public class Sound
{
private byte[] m_soundBytes;
private enum Flags
{
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("CoreDll.DLL", EntryPoint = "PlaySound", SetLastError = true)]
private extern static int MobilePlaySound(string szSound, IntPtr hMod, int flags);//播放外部声音文件
[DllImport("CoreDll.DLL", EntryPoint = "PlaySound", SetLastError = true)]
private extern static int MoiblePlaySoundBytes(byte[] szSound, IntPtr hMod, int flags);//播放嵌入声音资源
/// <summary>
/// Construct the Sound object to play sound data from the specified stream.
/// </summary>
public Sound(Stream stream)
{
// read the data from the stream
m_soundBytes = new byte[stream.Length];
stream.Read(m_soundBytes, 0, (int)stream.Length);
}
/// <summary>
/// 从一个声音byte流构造函数Sound
/// </summary>
/// <param name="snd"></param>
public Sound(byte[] snd)
{
m_soundBytes = snd;
}
/// <summary>
/// 构造一个空Sound函数,此函数用于播放外部文件
/// </summary>
public Sound()
{
}
/// <summary>
/// Play the sound File播放外部文件的Play方法
/// </summary>
public void Play(string filename)
{
MobilePlaySound(filename, IntPtr.Zero, (int)(Flags.SND_ASYNC | Flags.SND_FILENAME));
}
/// <summary>
/// Play The Stream Sound,此方法应该对应从Byte和Stream构造的Sound
/// 注意,它的Flags设置为SND_MEMORY,因为我们的声音文件已经转换到内存中了
/// </summary>
public void Play()
{
MoiblePlaySoundBytes(m_soundBytes, IntPtr.Zero, (int)(Flags.SND_ASYNC | Flags.SND_MEMORY));
}
}
}
然后,主界面上的按钮事件代码如下:
按钮代码
private void btnOpen_Click(object sender, EventArgs e)
{
using (OpenFileDialog ofd = new OpenFileDialog())
{
if (ofd.ShowDialog() == DialogResult.OK)
{
lblFileName.Text = ofd.FileName;
}
}
}
private void btnPlay_Click(object sender, EventArgs e)
{
Sound sound = new Sound();
sound.Play(lblFileName.Text);
}
够清楚了吧,经过测试,两种方式(通过流或者指定文件路径)均能播放出选中的.wav声音文件。