C# Window Form播放音樂的4種方式

來源:互聯網
上載者:User

標籤:

C#播放背景音樂通常有四種方式:

  1.播放系統事件聲音

  2.使用System.Media.SoundPlayer播放wav------------------------僅僅是對波形音樂

  3.使用MCI Command String多媒體裝置程式介面播放mp3,avi等

  4.使用axWindowsMediaPlayer的COM組件來播放

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:/10sec.wav"; sp.PlayLooping();

3.使用MCI Command String多媒體裝置程式介面播放mp3,avi等

 

using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Runtime.InteropServices;namespace MyWenxinTool{    public class musicplay    {        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 static void PlayNmusinc(string path)        {            mciSendString(@"close temp_alias", null, 0, 0);            mciSendString(@"open """+path+@""" alias temp_alias", null, 0, 0);            mciSendString("play temp_alias repeat", null, 0, 0);        }        /// <summary>        /// 播放音樂檔案(重複)        /// </summary>        /// <param name="p_FileName">音樂檔案名稱</param>        public static void PlayMusic_Repeat(string p_FileName)        {            try            {                mciSendString(@"close temp_music", " ", 0, 0);                mciSendString(@"open " + p_FileName + " alias temp_music", " ", 0, 0);                mciSendString(@"play temp_music repeat", " ", 0, 0);            }            catch            { }        }        /// <summary>        /// 播放音樂檔案        /// </summary>        /// <param name="p_FileName">音樂檔案名稱</param>        public static void PlayMusic(string p_FileName)        {            try            {                mciSendString(@"close temp_music", " ", 0, 0);                //mciSendString(@"open " + p_FileName + " alias temp_music", " ", 0, 0);                mciSendString(@"open """ + p_FileName + @""" alias temp_music", null, 0, 0);                mciSendString(@"play temp_music", " ", 0, 0);            }            catch            { }        }        /// <summary>        /// 停止當前音樂播放        /// </summary>        /// <param name="p_FileName">音樂檔案名稱</param>        public static void StopMusic(string p_FileName)        {            try            {                mciSendString(@"close " + p_FileName, " ", 0, 0);            }            catch { }        }    }}

關於mciSendString的詳細參數說明,請參見MSDN,或是 http://blog.csdn.net/psongchao/archive/2007/01/19/1487788.aspx

4.使用axWindowsMediaPlayer的COM組件來播放

  a.載入COM組件:添加引用-->Com組件--> Windows Media Player如:

  b.把Windows Media Player控制項拖放到Winform表單中,把axWindowsMediaPlayer1中URL屬性設定為MP3或是AVI的檔案路徑,F5運行。

如何使用Windows Media Player迴圈播放清單中的媒體檔案?

假設我們有一個播放清單,下面的代碼可以實現自動迴圈播放

MediaPlayer.MediaPlayer mr = new MediaPlayer.MediaPlayer();mr.FileName = txpath.Text.Trim();mr.Play();

MCI Command String和Windows Media Player都有非常豐富的功能介面,這裡不能一一介紹,可以參考MSDN中的具體描述.

C# Window Form播放音樂的4種方式

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.