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

各位大虾!帮帮我怎么在窗体程序中安插音乐!
小弟初学c#,想给自己的程序配上背景音乐!不知道要怎么做!背景音乐是可以选择的!这应该怎么写代码啊!

------解决方案--------------------
using System.Media;

private void button1_Click(object sender, EventArgs e)
{
//通过资源播放
SoundPlayer sndPing = new SoundPlayer();
sndPing.Stream = Properties.Resources.MySound;
sndPing.Play();
//功过文件播放
SoundPlayer vSoundPlayer = new SoundPlayer();
vSoundPlayer.Stream = new FileStream(@ "C:\WINDOWS\Media\chord.wav ",
FileMode.Open, FileAccess.Read);
vSoundPlayer.Play();
}

------解决方案--------------------
//可以使用api播放...

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);
}