1. 播放系統事件聲音
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音樂文件
System.Media.SoundPlayer sp = new SoundPlayer();
sp.SoundLocation = @"D:\TestMusic.wav";
sp.PlayLooping();
3. 使用非托管的dll播放.wav音樂文件
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個形參,把1換為9,連續播放
}
}
4. 使用MCI Command String多媒體設備程序接口播放.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 ""D:\TestMusic.mp3"" alias temp_alias", null, 0, 0);
mciSendString("play temp_alias repeat", null, 0, 0);
}
5. 使用axWindowsMediaPlayer的COM組件播放.mp3或.avi音樂文件
1) 加載COM組件:ToolBox->Choose Items->COM Components->Window Media Player。
2) 把Windows Media Player控件拖到窗體中,把axWindowsMediaPlay1的URL屬性設置為.mp3或.avi音樂文件的路徑,運行程序即可。
3) 使用Windows Media Player循環播放音樂文件:
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();
}
趕緊動手設計一個簡易的音樂播放器吧!只要堅持每天一點點地改進你的音樂播放器,不斷地增加新的功能,相信總有一天它會成為最適合你的音樂播放器。