C#中聲音播放的三種方式
C#中聲音的播放主要有三種方法:
1.使用API函式。
2.使用SoundPlayer類播放。
3.使用DirectX進行播放。
一、使用API函式進行播放。
windows作業系統中的winmm.dll檔案中封裝了聲音處理的函式。在C#中我們可以通過平臺呼叫的方式使用這裡邊的API函式來播放聲音。
下邊的類中使用了PlaySound、sndPlaySound、mciSendString 3個API函式來播放聲音。
其中mciSendString還可以播放mp3格式的聲音。
using System; using System.Collections.Generic; using System.Linq;using System.Text; using System.Runtime.InteropServices; namespace HuaXu { public static class WavPlayer { [DllImport("winmm.dll", SetLastError = true)] static extern bool PlaySound(string pszSound, UIntPtr hmod, uint fdwSound); [DllImport("winmm.dll", SetLastError = true)] static externlong mciSendString(string strCommand, StringBuilder strReturn,int iReturnLength, IntPtr hwndCallback); [DllImport("winmm.dll")] private static extern long sndPlaySound(string lpszSoundName, long uFlags); [Flags] public enum SoundFlags { /// <summary>play synchronously (default)</summary> SND_SYNC = 0x0000, /// <summary>play asynchronously</summary> SND_ASYNC = 0x0001, /// <summary>silence (!default) if sound not found</summary> SND_NODEFAULT = 0x0002, /// <summary>pszSound points to a memory file</summary> SND_MEMORY = 0x0004, /// <summary>loop the sound until next sndPlaySound</summary> SND_LOOP = 0x0008, /// <summary>don’t stop any currently playing sound</summary> SND_NOSTOP = 0x0010, /// <summary>Stop Playing Wave</summary> SND_PURGE = 0x40, /// <summary>don’t wait if the driver is busy</summary> SND_NOWAIT = 0x00002000, /// <summary>name is a registry alias</summary> SND_ALIAS = 0x00010000, /// <summary>alias is a predefined id</summary> SND_ALIAS_ID = 0x00110000, /// <summary>name is file name</summary> SND_FILENAME = 0x00020000, /// <summary>name is resource name or atom</summary> SND_RESOURCE = 0x00040004 } public static void Play(string strFileName) { PlaySound(strFileName, UIntPtr.Zero, (uint)(SoundFlags.SND_FILENAME | SoundFlags.SND_SYNC | SoundFlags.SND_NOSTOP)); } public static void mciPlay(string strFileName) { string playCommand = "open " + strFileName + " type WAVEAudio alias MyWav"; mciSendString(playCommand, null, 0, IntPtr.Zero); mciSendString("play MyWav", null, 0, IntPtr.Zero); } public static void sndPlay(string strFileName) { sndPlaySound(strFileName, (long)SoundFlags.SND_SYNC); } } }
其中SoundFlags列舉定義了各種播放的方式。
二、使用SoundPlayer播放聲音。
System.Media名稱空間下的類SoundPlayer 可以讓我們很方便的播放wav波形聲音檔案。SoundPlayer類其實就是對winmm.dll檔案中API函式的封裝。
SoundPlayer類的使用很簡單。如下:
SoundPlayer player = new SoundPlayer(); player.SoundLocation = "音樂檔名"; player.Load(); player.Play();
其中Play方法是非同步方法,會在另一個執行緒中播放。如果我們有時候需要等聲音播放完畢之後再進行下一步操作。即聲音播放需要阻塞當前執行緒。就可以使用PlaySync()
方法。
SoundPlayer類的缺點:只能播放wav檔案;在winxp下播放檔案比較大或位率比較高的情況,PlaySync同步播放會有播放不完全的問題。
這個問題的產生是由於winmm.dll的版本問題引起的。在xp下winmm.dll的版本是5。在win7下是6。win7下就沒有問題。如果要解決在
xp下播放不完全的問題。可以使用xp下的錄音機開啟聲音檔案,把聲音檔案另存為7kbit/s的位率格式,但這樣聲音效果就很差了。
三、使用DirectX進行播放。
首先要安裝DirectX SDK。安裝好之後,在C:\Windows\Microsoft.NET\DirectX for Managed Code\1.0.2902.0目錄下有在.net下可以使用的dll檔案。
首先引用新增引用Microsoft.DirectX.AudioVideoPlayback
using Microsoft.DirectX.AudioVideoPlayback;
然後例項化Audo類的物件,就可以播放包括mp3格式的音樂檔案了。
Audio audio = new Audio("hello.mp3"); audio.play();
C#中聲音的播放主要有三種方法:
1.使用API函式。
2.使用SoundPlayer類播放。
3.使用DirectX進行播放。
一、使用API函式進行播放。
windows作業系統中的winmm.dll檔案中封裝了聲音處理的函式。在C#中我們可以通過平臺呼叫的方式使用這裡邊的API函式來播放聲音。
下邊的類中使用了PlaySound、sndPlaySound、mciSendString 3個API函式來播放聲音。
其中mciSendString還可以播放mp3格式的聲音。
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Runtime.InteropServices; namespace HuaXu { public static class WavPlayer { [DllImport("winmm.dll", SetLastError = true)] static extern bool PlaySound(string pszSound, UIntPtr hmod, uint fdwSound); [DllImport("winmm.dll", SetLastError = true)] static extern long mciSendString(string strCommand, StringBuilder strReturn,int iReturnLength, IntPtr hwndCallback); [DllImport("winmm.dll")] private static extern long sndPlaySound(string lpszSoundName, long uFlags); [Flags] public enum SoundFlags { /// <summary>play synchronously (default)</summary> SND_SYNC = 0x0000, /// <summary>play asynchronously</summary> SND_ASYNC = 0x0001, /// <summary>silence (!default) if sound not found</summary> SND_NODEFAULT = 0x0002, /// <summary>pszSound points to a memory file</summary> SND_MEMORY = 0x0004, /// <summary>loop the sound until next sndPlaySound</summary> SND_LOOP = 0x0008, /// <summary>don’t stop any currently playing sound</summary> SND_NOSTOP = 0x0010, /// <summary>Stop Playing Wave</summary> SND_PURGE = 0x40, /// <summary>don’t wait if the driver is busy</summary> SND_NOWAIT = 0x00002000, /// <summary>name is a registry alias</summary> SND_ALIAS = 0x00010000, /// <summary>alias is a predefined id</summary> SND_ALIAS_ID = 0x00110000, /// <summary>name is file name</summary> SND_FILENAME = 0x00020000, /// <summary>name is resource name or atom</summary> SND_RESOURCE = 0x00040004 } public static void Play(string strFileName) { PlaySound(strFileName, UIntPtr.Zero, (uint)(SoundFlags.SND_FILENAME | SoundFlags.SND_SYNC | SoundFlags.SND_NOSTOP)); } public static void mciPlay(string strFileName) { string playCommand = "open " + strFileName + " type WAVEAudio alias MyWav"; mciSendString(playCommand, null, 0, IntPtr.Zero); mciSendString("play MyWav", null, 0, IntPtr.Zero); } public static void sndPlay(string strFileName) { sndPlaySound(strFileName, (long)SoundFlags.SND_SYNC); } } }
其中SoundFlags列舉定義了各種播放的方式。
二、使用SoundPlayer播放聲音。
System.Media名稱空間下的類SoundPlayer 可以讓我們很方便的播放wav波形聲音檔案。SoundPlayer類其實就是對winmm.dll檔案中API函式的封裝。
SoundPlayer類的使用很簡單。如下:
SoundPlayer player = new SoundPlayer(); player.SoundLocation = "音樂檔名"; player.Load(); player.Play();
其中Play方法是非同步方法,會在另一個執行緒中播放。如果我們有時候需要等聲音播放完畢之後再進行下一步操作。即聲音播放需要阻塞當前執行緒。就可以使用PlaySync()
方法。
SoundPlayer類的缺點:只能播放wav檔案;在winxp下播放檔案比較大或位率比較高的情況,PlaySync同步播放會有播放不完全的問題。
這個問題的產生是由於winmm.dll的版本問題引起的。在xp下winmm.dll的版本是5。在win7下是6。win7下就沒有問題。如果要解決在
xp下播放不完全的問題。可以使用xp下的錄音機開啟聲音檔案,把聲音檔案另存為7kbit/s的位率格式,但這樣聲音效果就很差了。
三、使用DirectX進行播放。
首先要安裝DirectX SDK。安裝好之後,在C:\Windows\Microsoft.NET\DirectX for Managed Code\1.0.2902.0目錄下有在.net下可以使用的dll檔案。
首先引用新增引用Microsoft.DirectX.AudioVideoPlayback
using Microsoft.DirectX.AudioVideoPlayback;
然後例項化Audo類的物件,就可以播放包括mp3格式的音樂檔案了。
Audio audio = new Audio("hello.mp3"); audio.play();
出處:https://www.cnblogs.com/chenjiahong/articles/2716552.html