使用C#播放MP3/WAV文件的四種方式


https://www.cnblogs.com/xibei666/p/4586767.html

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

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM