前天买了块wince的控制板,询问厂家如何用C#在wince下如何播放声音,令人郁闷的是,几天过去了也没问出个结果。只好自己找资料,
现在贡献出来,希望能帮助其他的朋友少走弯路。
我介绍的方法适用WinCE6.0和以前的所有版本。
1.首先介绍如何播放*.wav, 这个方式微软提供了标准的播放类,但不适合播放大文件的*.wav, 只适合10MB以内的简单系统提示语音。
如下:
using System; using System.Runtime.InteropServices; using System.IO; namespace Dengfengsoft { public class Sound { private byte[] m_soundBytes; private string m_fileName; // private enum Flags { SND_SYNC = 0x0000, /* play synchronously (default) */ SND_ASYNC = 0x0001, /* plaSmartHomey 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 WCE_PlaySound(string szSound, IntPtr hMod, int flags); [DllImport("CoreDll.DLL", EntryPoint = "PlaySound", SetLastError = true)] private extern static int WCE_PlaySoundBytes(byte[] szSound, IntPtr hMod, int flags); /// <summary> /// Construct the Sound object to play sound data from the specified file. /// </summary> public Sound(string fileName) { m_fileName = fileName; } /// <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> /// Play the sound /// </summary> public void Play() { // if a file name has been registered, call WCE_PlaySound, // otherwise call WCE_PlaySoundBytes if (m_fileName != null) WCE_PlaySound(m_fileName, IntPtr.Zero, (int)(Flags.SND_ASYNC | Flags.SND_FILENAME)); else WCE_PlaySoundBytes(m_soundBytes, IntPtr.Zero, (int)(Flags.SND_ASYNC | Flags.SND_MEMORY)); } } }调用方法如下:
private void button1_Click(object sender, EventArgs e) { Sound soundFile = new Sound("\\Program Files\\music.wav"); soundFile.Play(); }
这个就比较难了,找了很久才找到的方法。
国外的前辈几年前给我们做了一个控件:fmodce.dll, 现在不是免费版了,但本文附件是早期的版本,所以...你懂的。
运行在winCE6.0,和以前的版本都没有问题!
MP3的播放类如下:
using System; using System.Linq; using System.Collections.Generic; using System.Text; using System.Runtime.InteropServices; namespace Dengfengsoft { public class PlayMP3 { public static IntPtr GetStream(string filename) // 获得音频文件的IntPtr { byte[] filenamebytes = System.Text.Encoding.Default.GetBytes(filename + null); GCHandle hfile = GCHandle.Alloc(filenamebytes, GCHandleType.Pinned); if (Environment.Version.Major == 1) return new IntPtr(hfile.AddrOfPinnedObject().ToInt32() + 4); else return hfile.AddrOfPinnedObject(); } [DllImport("fmodce.dll", EntryPoint = "FSOUND_Init", SetLastError = true, CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Winapi)] public static extern bool Init(int mixrate, int maxsoftwarechannels, int flags); // 初始化 [DllImport("fmodce.dll", EntryPoint = "FSOUND_Stream_GetLength", SetLastError = true, CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Winapi)] public static extern int GetLength(IntPtr fstream); // 获得流媒体的长度 [DllImport("fmodce.dll", EntryPoint = "FSOUND_Stream_GetPosition", SetLastError = true, CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Winapi)] public static extern UInt32 GetPosition(IntPtr fstream); // 获得流媒体当前播放位置 [DllImport("fmodce.dll", EntryPoint = "FSOUND_Stream_Open", SetLastError = true, CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Winapi)] public static extern IntPtr Open(IntPtr data, int mode, int offset, int length); // 打开音频文件 [DllImport("fmodce.dll", EntryPoint = "FSOUND_Stream_Play", SetLastError = true, CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Winapi)] public static extern int Play(int channel, IntPtr fstream); // 播放音频文件 [DllImport("fmodce.dll", EntryPoint = "FSOUND_Stream_SetPosition", SetLastError = true, CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Winapi)] public static extern bool SetPosition(IntPtr fstream, UInt32 position); // 定位音频文件播放位置 [DllImport("fmodce.dll", EntryPoint = "FSOUND_Stream_Stop", SetLastError = true, CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Winapi)] public static extern bool Stop(IntPtr fstream); // 停止播放 [DllImport("fmodce.dll", EntryPoint = "FSOUND_Close", SetLastError = true, CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Winapi)] public static extern void Close(); // 关闭音频文件 [DllImport("coredll.dll", SetLastError = true)] public static extern int SetSystemPowerState(string psState, int StateFlags, int Options); const int POWER_FORCE = 0x1000; const int POWER_STATE_ON = 0x10000; } }
〉〉播放MP3
private void button2_Click(object sender, EventArgs e) { try { //调用 string currentSoundTrack = "\\Program Files\\music.mp3"; PlayMP3.Init(44100, 16, 0); // 初始化为44.1kHz IntPtr soundStream = PlayMP3.GetStream(currentSoundTrack); soundHandle = PlayMP3.Open(soundStream, 16 | 32 | 256, 0, 0); PlayMP3.Play(0, soundHandle); } catch { MessageBox.Show("播放失败。"); } }
private void button3_Click(object sender, EventArgs e) { PlayMP3.Stop(soundHandle); }注意 soundHandle是全局变量
〉〉退出MP3播放器
private void button4_Click(object sender, EventArgs e) { PlayMP3.Close();//一般放在窗体退出的时候 }
本文两个例子的完整代码下载(vs2008)