C#使用HWQPlayer類播放wav文件
類的代碼:
using System.IO;
using System.Runtime.InteropServices;
namespace HoverTreeSound.HewenqiFrame
{
internal class HWQPlayer
{
[DllImport("winmm.dll")]
private static extern int sndPlaySoundA(byte[] lpszSoundName, int uFlags);
private const int SND_MEMORY = 0x4;
private const int SND_ASYNC = 0x1;
byte[] StreamToBytes(Stream stream)
{
byte[] bytes = new byte[stream.Length];
stream.Read(bytes, 0, bytes.Length);
// 設置當前流的位置為流的開始 by 何問起
stream.Seek(0, SeekOrigin.Begin);
return bytes;
}
public void PlayWav(Stream stream)
{
sndPlaySoundA(StreamToBytes(stream), SND_MEMORY);
}
}
}
調用代碼:
new HWQPlayer().PlayWav(Properties.Resources.hewenqi);
調用的地方需要引用命名空間:
using HoverTreeSound.HewenqiFrame;
示例下載:http://hovertree.com/h/bjaf/4aaa1b2s.htm
界面: