我最近正在学习如何在一个自己编写的windows应用程序中加入背景音乐或者是一个小型的音频播放器,参考了网上的很多帮助资料,可是现在却越来越混乱了,想问一下:1. 我用的是VS.NET2003,开发语言是C#。在这种环境下一定要安装象DirectX SDK这种第三方控件才能播放声音吗?VS.NET2003中没有相关的API吗,我在帮助文档中看到有个“sndPlaySound”的函数,它不能用来播放声音吗?2. 我如果只希望在应用程序中加入背景音乐,格式用midi是否较好(体积小)? 3. 如果我希望在应用程序中加入一个小音频播放器(就是那种只有播放,停止,下一首等几个常用按钮的播放器,不用新开复杂的界面),是该用ActiveX控件中的MediaPlayer还是要自己编写完整的代码?希望了解这部分功能实现的高手指点一下,谢谢!
private static extern long mciSendString(
string lpstrCommand,
string lpstrReturnString,
long uReturnLength,
long hwndCallback
);
调 用这个API
using System.Threading;
using System.Runtime.InteropServices;
namespace Sound
{
class Class1
{
[DllImport("user32.dll")]
public static extern int MessageBeep(uint n);
[DllImport("kernel32.dll")]
private static extern int Beep(int dwFreq ,int dwDuration) ;
[DllImport("winmm.dll")]
public static extern long mciSendString(string lpstrCommand,string lpstrReturnString,long length,long hwndcallback);
[DllImport("winmm.dll")]
public static extern long PlaySound(String fileName,long a,long b);
[DllImport("winmm.dll", EntryPoint="PlaySound")]
public static extern bool PlaySound(ref Byte snd, IntPtr hmod, uint fdwSound);
[DllImport("winmm.dll", EntryPoint="PlaySound")]
public static extern int PlaySound(string lpszName,int hModule,int dwFlags); public enum PlayingFlags :uint
{
SND_SYNC = 0x00,
SND_ASYNC = 0x01,
SND_NODEFAULT = 0x02,
SND_MEMORY = 0x04,
SND_ALIAS = 0x010000,
SND_FILENAME = 0x020000,
SND_RESOURCE = 0x040004,
SND_ALIAS_ID = 0x0110000,
SND_ALIAS_START = 0,
SND_LOOP = 0x08,
SND_NOSTOP = 0x010,
SND_VALID = 0x01F,
SND_NOWAIT = 0x02000,
SND_PURGE = 0x40
}
internal class Helpers
{
[Flags]
public enum PlaySoundFlags : int
{
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("winmm")]
public static extern bool PlaySound(string szSound, IntPtr hMod, PlaySoundFlags flags );
} [STAThread]
static void Main(string[] args)
{
//Play_Beep_01();
//Play_Beep_03();
//Play_Beep_02(); //Play_wav_01();
Play_wav_02();
Play_wav_03();
Console.WriteLine("Finished!");
Console.ReadLine();
} static void Play_Beep_01()
{
for (int i=0;i<3;i++)
{
MessageBeep(0xFFFFFFFF);
Thread.Sleep(1000);
}
} static void Play_Beep_02()
{
int a=0X7FF;
int b=100;
Beep(a,b);
}
static void Play_wav_01()
{
try
{
mciSendString("chimes.wav", null, 0, 0);
mciSendString("chord.wav", "ccc", 0, 0);
mciSendString("ding.wav", null, 0, 0);
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}
} static void Play_wav_02()
{
PlaySound("1.wav",0,0);
} static void Play_Beep_03()
{
byte[] bb = new byte[2];
bb[0] = 7;
char b = BitConverter.ToChar(bb,0);
for(int i = 0;i<4;i++)
{
Console.Write(b);
Thread.Sleep(1000);
}
} static void Play_wav_03()
{
for (int i=0;i<10;i++)
{
Helpers.PlaySound("Utopia Restore Down.WAV", IntPtr.Zero, Helpers.PlaySoundFlags.SND_FILENAME | Helpers.PlaySoundFlags.SND_ASYNC);
Thread.Sleep(100);
}
}
}
}