WinCE 下利用CoreDll.dll 播放聲音

來源:互聯網
上載者:User

此樣本示範如何使用平台叫用來播放兩個 WAV 檔案:一個為內嵌資源,另一個為內容。

若要在 Visual Studio 中實現作為嵌入資源的 WAV 檔案,請在“屬性”窗格中將其“產生操作”屬性設定為“內嵌資源”。

此樣本定義了 Sound 類,該類通過使用 Windows CE 中的 CoreDll.dll 提供下面的機器碼功能:

  • 使用檔案名稱或流播放聲音的平台叫用方法聲明。
  • 用於在平台叫用方法調用中傳遞參數的位值枚舉。
  • Play 方法,用於調用正確的平台叫用方法來播放單獨的檔案或內嵌資源。

此樣本使用名為 Chimes.wav 的音效檔。當包含該音效檔作為嵌入資源時,要通過如下方式為該檔案返回資源流:在對 GetManifestResourceStream 的調用中將程式集命名空間添加到原始檔案名的前面。

 

使用平台叫用播放聲音

1.     將 Sound 類添加到項目中。

public class Sound<br />{<br /> private byte[] m_soundBytes;<br /> private string m_fileName;</p><p> private enum Flags {<br /> SND_SYNC = 0x0000, /* play synchronously (default) */<br /> SND_ASYNC = 0x0001, /* play asynchronously */<br /> SND_NODEFAULT = 0x0002, /* silence (!default) if sound not found */<br /> SND_MEMORY = 0x0004, /* pszSound points to a memory file */<br /> SND_LOOP = 0x0008, /* loop the sound until next sndPlaySound */<br /> SND_NOSTOP = 0x0010, /* don't stop any currently playing sound */<br /> SND_NOWAIT = 0x00002000, /* don't wait if the driver is busy */<br /> SND_ALIAS = 0x00010000, /* name is a registry alias */<br /> SND_ALIAS_ID = 0x00110000, /* alias is a predefined ID */<br /> SND_FILENAME = 0x00020000, /* name is file name */<br /> SND_RESOURCE = 0x00040004 /* name is resource name or atom */<br /> }</p><p> [DllImport("CoreDll.DLL", EntryPoint="PlaySound", SetLastError=true)]<br /> private extern static int WCE_PlaySound(string szSound, IntPtr hMod, int flags);</p><p> [DllImport("CoreDll.DLL", EntryPoint="PlaySound", SetLastError=true)]<br /> private extern static int WCE_PlaySoundBytes (byte[] szSound, IntPtr hMod, int flags);</p><p> /// <summary><br /> /// Construct the Sound object to play sound data from the specified file.<br /> /// </summary><br /> public Sound (string fileName) {<br /> m_fileName = fileName;<br /> }</p><p> /// <summary><br /> /// Construct the Sound object to play sound data from the specified stream.<br /> /// </summary><br /> public Sound(Stream stream) {<br /> // read the data from the stream<br /> m_soundBytes = new byte [stream.Length];<br /> stream.Read(m_soundBytes, 0,(int)stream.Length);<br /> }</p><p> /// <summary><br /> /// Play the sound<br /> /// </summary><br /> public void Play () {<br /> // if a file name has been registered, call WCE_PlaySound,<br /> // otherwise call WCE_PlaySoundBytes<br /> if (m_fileName != null)<br /> WCE_PlaySound(m_fileName, IntPtr.Zero, (int) (Flags.SND_ASYNC | Flags.SND_FILENAME));<br /> else<br /> WCE_PlaySoundBytes (m_soundBytes, IntPtr.Zero, (int) (Flags.SND_ASYNC | Flags.SND_MEMORY));<br /> }<br />}  

 

2.     添加用於建立 Sound 類的執行個體和播放檔案的方法,例如,在某按鈕的 Click 事件中。

 

// To return a Stream object associated with an embedded<br />// resource, you must prepend the namespace to the original<br />// name of the file in the project.<br />private void btnEmbedded_Click(object sender, System.EventArgs e) {<br /> Sound sound = new Sound (Assembly.GetExecutingAssembly().GetManifestResourceStream("SoundSample.chimes.wav"));<br /> sound.Play();<br />}</p><p>private void btnFile_Click(object sender, System.EventArgs e) {<br /> Sound sound = new Sound ("Program Files//SoundSample//chord.wav");<br /> sound.Play();<br />}  

 

此樣本需要引用下面的命名空間:

  • System
  • System.IO
  • System.Reflection
  • System.Runtime.InteropServices
  • System.Windows.Forms

 

以上代碼是 MSDN 中提供的,我僅僅是經過測試而已。

在 VS2003 下,採用

 

private void btnFile_Click(object sender, System.EventArgs e)<br />{<br /> Sound sound = new Sound ("Program Files//SoundSample//chord.wav");<br /> sound.Play();<br />}</p><p>

 

這個調用方法,只要播發的音效檔的路徑是正確的,是可以播放的。

本人親自測試過,在我的開發板上是可以正常啟動並執行,但是模擬時有問題。

建議大家有硬體平台的直接就在硬體平台上測試。

 

 

原文地址:

http://www.cnblogs.com/WorkHard_PlayHarder/articles/1609019.html

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.