播放背景音樂的幾種方法

C#中播放背景音樂幾種的方法


最經(jīng)在寫winform程序,其中有用到播放背景音樂

特此收集了一些網(wǎng)上的教程:

1、調(diào)用非托管的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,可連續(xù)播放

}

? ? ? ?}

2、播放系統(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();

3、使用System.Media.SoundPlayer播放wav

System.Media.SoundPlayer sp = new SoundPlayer();

  sp.SoundLocation = @"D:\10sec.wav";

  sp.PlayLooping();

4、使用MCI Command String多媒體設(shè)備程序接口播放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);

  }

關(guān)于mciSendString的詳細(xì)參數(shù)說明,請(qǐng)參見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窗體中,把a(bǔ)xWindowsMediaPlayer1中URL屬性設(shè)置為MP3或是AVI的文件路徑,F(xiàn)5運(yùn)行。

  如何使用Windows Media Player循環(huán)播放列表中的媒體文件?

  假設(shè)我們有一個(gè)播放列表,下面的代碼可以實(shí)現(xiàn)自動(dòng)循環(huán)播放

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

  }

?

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 本人初學(xué)Android,最近做了一個(gè)實(shí)現(xiàn)安卓簡單音樂播放功能的播放器,收獲不少,于是便記錄下來自己的思路與知識(shí)總結(jié)...
    落日柳風(fēng)閱讀 19,446評(píng)論 2 41
  • 1. Java基礎(chǔ)部分 基礎(chǔ)部分的順序:基本語法,類相關(guān)的語法,內(nèi)部類的語法,繼承相關(guān)的語法,異常的語法,線程的語...
    子非魚_t_閱讀 34,641評(píng)論 18 399
  • Spring Cloud為開發(fā)人員提供了快速構(gòu)建分布式系統(tǒng)中一些常見模式的工具(例如配置管理,服務(wù)發(fā)現(xiàn),斷路器,智...
    卡卡羅2017閱讀 136,537評(píng)論 19 139
  • ¥開啟¥ 【iAPP實(shí)現(xiàn)進(jìn)入界面執(zhí)行逐一顯】 〖2017-08-25 15:22:14〗 《//首先開一個(gè)線程,因...
    小菜c閱讀 7,317評(píng)論 0 17
  • 圖片轉(zhuǎn)視頻 為什么想將圖片轉(zhuǎn)視頻? 是這樣的,我打造的任性動(dòng)圖軟件,在編輯制作GIF動(dòng)圖方面,已經(jīng)基本完善?,F(xiàn)在想...
    古典小說閱讀 1,934評(píng)論 1 0

友情鏈接更多精彩內(nèi)容