如下的内容是关于C#播放背景音乐的几种方法的内容,希望能对大伙有所帮助。
System.Media.SystemSounds.Asterisk.Play();
System.Media.SystemSounds.Beep.Play();
System.Media.SystemSounds.Exclamation.Play();
System.Media.SystemSounds.Hand.Play();
System.Media.SystemSounds.Question.Play();
2.使用System.Media.SoundPlayer播放wav,mp3 System.Media.SoundPlayersp=newSoundPlayer(); sp.SoundLocation=@"恋爱ing.wav"; sp.PlayLooping();注:使用这种方法,总是提示出该文件不是波形文件,很郁闷,还是没有找出原因!!使用VS2008的话,直接添加System.Media命名空间即可!很简单!3.使用MCICommandString多媒体设备程序接口播放mp3,avi等
using System.Runtime.InteropServices;
public static uint SND_ASYNC = 0x0001;
public static uint SND_FILENAME = 0x00020000;
[DllImport("winmm.dll")]
public static extern uint mciSendString(string lpstrCommand,
string lpstrReturnString, uint uReturnLength, uint hWndCallback);
public void Play()
{
mciSendString(@"close temp_alias", null, 0, 0);
mciSendString(@"open ""恋爱ing.mp3"" alias temp_alias", null, 0, 0);
mciSendString("play temp_alias repeat", null, 0, 0);
}
private void axWindowsMediaPlayer1_PlayStateChange(object sender, AxWMPLib._WMPOCXEvents_PlayStateChangeEvent e)
{
if (axWindowsMediaPlayer1.playState == WMPLib.WMPPlayState.wmppsMediaEnded)
{
Thread thread = new Thread(new ThreadStart(PlayThread));
thread.Start();
}
}
private void PlayThread()
{
axWindowsMediaPlayer1.URL = @"E:MusicSomeOne.avi";
axWindowsMediaPlayer1.Ctlcontrols.play();
}
MCICommandString和WindowsMediaPlayer都有非常丰富的功能接口,这里不能一一介绍,可以参考MSDN中的具体描述.