C#播放配景音樂的辦法小結。本站提示廣大學習愛好者:(C#播放配景音樂的辦法小結)文章只能為提供參考,不一定能成為您想要的結果。以下是C#播放配景音樂的辦法小結正文
本文實例總結了C#播放配景音樂的辦法。分享給年夜家供年夜家參考。詳細剖析以下:
最經在寫winform法式,個中有效到播放配景音樂
特此搜集了一些網上的教程:
1、挪用非托管的dll
using System.Runtime.InteropServices;
//DllImport定名空間的援用
class test //提醒音
{
[DllImport("winmm.dll")]
public static extern bool PlaySound(String Filename,int Mod,int Flags);
public void Main()
{
PlaySound(@"d:/qm.wav",0,1);
//把1調換成9,可持續播放
}
}
2、播放體系自帶聲響
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();
3、應用System.Media.SoundPlayer播放wav
System.Media.SoundPlayer sp = new SoundPlayer(); sp.SoundLocation = @"D:\10sec.wav"; sp.PlayLooping();
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 ""E:\Music\青花瓷.mp3"" alias temp_alias",null,0,0);
mciSendString("play temp_alias repeat", null, 0, 0);
}
關於mciSendString的具體參數解釋,請拜見MSDN,或是 http://blog.csdn.net/psongchao/archive/2007/01/19/1487788.aspx
5、應用axWindowsMediaPlayer的COM組件來播放
a.加載COM組件:ToolBox->Choose Items->COM Components->Windows Media Player:
b.把Windows Media Player控件拖放到Winform窗體中,把axWindowsMediaPlayer1中URL屬性設置為MP3或是AVI的文件途徑,F5運轉。
若何應用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 = @"E:\Music\SomeOne.avi";
axWindowsMediaPlayer1.Ctlcontrols.play();
}
願望本文所述對年夜家的C#法式設計有所贊助。