日期:2014-05-19  浏览次数:20965 次

想给游戏添加背景音乐
我写了个俄罗斯方块的小游戏,想给游戏添加背景音乐不知道如何实现?

------解决方案--------------------
using System.Runtime.InteropServices;

public static uint SND_ASYNC = 0x0001; // play asynchronously
public static uint SND_FILENAME = 0x00020000; // name is file name
[DllImport( "winmm.dll ")]
public static extern uint mciSendString(string lpstrCommand,
string lpstrReturnString, uint uReturnLength, uint hWndCallback);

private void button1_Click(object sender, EventArgs e)
{
mciSendString(@ "close temp_alias ", null, 0, 0);
mciSendString(@ "open " "E:\音乐\周杰伦-东风破.mp3 " " alias temp_alias ",
null, 0, 0);
mciSendString( "play temp_alias ", null, 0, 0);
}

------解决方案--------------------

[System.Runtime.InteropServices.DllImport( "winmm.DLL ", EntryPoint = "PlaySound ", SetLastError = true)]
private static extern bool PlaySound(string szSound, System.IntPtr hMod, PlaySoundFlags flags);

[System.Flags]
public enum PlaySoundFlags : int
{
SND_SYNC = 0x0000,
SND_ASYNC = 0x0001,
SND_NODEFAULT = 0x0002,
SND_LOOP = 0x0008,
SND_NOSTOP = 0x0010,
SND_NOWAIT = 0x00002000,
SND_FILENAME = 0x00020000,
SND_RESOURCE = 0x00040004
}

private void button1_Click (object sender, System.EventArgs e)
{
PlaySound (文件路径, new System.IntPtr(), PlaySoundFlags.SND_SYNC);
}

这个是MSDN里面的..
------解决方案--------------------
打开“xxx.mp3”媒体资源,并将打开后的资源别名设置为“temp_alias”
后面的指令就可以通过“temp_alias”来操作