1. 播放系統(tǒng)事件聲音
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音樂(lè)文件
System.Media.SoundPlayer sp = new SoundPlayer();
sp.SoundLocation = @"D:\TestMusic.wav";
sp.PlayLooping();
3. 使用非托管的dll播放.wav音樂(lè)文件
using System.Runtime.InteropServices;
class PlayMusic
{
[DllImport("winmm.dll")]
public static extern bool PlaySound(String Filename,int Mod,int Flags);
public void Main()
{
PlaySound(@"D:\TestMusic.wav",0,1); //第3個(gè)形參,把1換為9,連續(xù)播放
}
}
4. 使用MCI Command String多媒體設(shè)備程序接口播放.mp3,.avi等音樂(lè)文件
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 ""D:\TestMusic.mp3"" alias temp_alias", null, 0, 0);
mciSendString("play temp_alias repeat", null, 0, 0);
}
5. 使用axWindowsMediaPlayer的COM組件播放.mp3或.avi音樂(lè)文件
1) 加載COM組件:ToolBox->Choose Items->COM Components->Window Media Player。
2) 把Windows Media Player控件拖到窗體中,把axWindowsMediaPlay1的URL屬性設(shè)置為.mp3或.avi音樂(lè)文件的路徑,運(yùn)行程序即可。
3) 使用Windows Media Player循環(huán)播放音樂(lè)文件:
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 = @"D:\TestMusic.avi";
axWindowsMediaPlayer1.Ctlcontrols.play();
}
趕緊動(dòng)手設(shè)計(jì)一個(gè)簡(jiǎn)易的音樂(lè)播放器吧!只要堅(jiān)持每天一點(diǎn)點(diǎn)地改進(jìn)你的音樂(lè)播放器,不斷地增加新的功能,相信總有一天它會(huì)成為最適合你的音樂(lè)播放器。