How to play WAV audio files in the wince System

Source: Internet
Author: User
  1. Question: How to play WAV audio files in the wince System
  2. Mailing site: dianheting BBS (Tue Apr 15 12:55:40 2008), this site (bbs.cumt.edu.cn)
  3. This example runs successfully in the wince5.0 +. Net cf2.0 environment.
  4. The following namespace must be referenced in this example:
  5. System
  6. System. Io
  7. System. Reflection
  8. System. runtime. interopservices
  9. System. Windows. Forms
  10. This example shows how to use a platform call to play two WAV Files: one is an embedded resource and the other is the content.
  11. This example defines a sound class, which provides the following native code functions through wince's coredll. dll:
  12. 1. Platform call method declaration using the file name or stream playback sound.
  13. 2. Enumeration of BITs used to pass Parameters in platform call method calls.
  14. 3. The play method is used to call the correct platform call method to play a single file or embedded resource.
  15. Step 1: add the sound class to the project.
  16. Public class sound
  17. {
  18. Private byte [] m_soundbytes;
  19. Private string m_filename;
  20. Private Enum flags {
  21. Snd_sync = 0x0000,/* play synchronously (default )*/
  22. Snd_async = 0x0001,/* Play asynchronously */
  23. Snd_nodefault = 0x0002,/* silence (! Default) if sound not found */
  24. Snd_memory = 0x0004,/* pszsound points to a memory file */
  25. Snd_loop = 0x0008,/* loop the sound until next sndplaysound */
  26. Snd_nostop = 0x0010,/* don't stop any currently playing sound */
  27. Snd_nowait = 0x00002000,/* Don't wait if the driver is busy */
  28. Snd_alias = 0x00010000,/* name is a registry alias */
  29. Snd_alias_id = 0x00110000,/* alias is a predefined ID */
  30. Snd_filename = 0x00020000,/* name is file name */
  31. Snd_resource = 0x00040004/* Name Is Resource Name or atom */
  32. }
  33. [Dllimport ("coredll. dll", entrypoint = "playsound", setlasterror = true)]
  34. Private extern static int wce_playsound (string szsound, intptr hmod, int flags );
  35. [Dllimport ("coredll. dll", entrypoint = "playsound", setlasterror = true)]
  36. Private extern static int wce_playsoundbytes (byte [] szsound, intptr hmod, int flags );
  37. /// <Summary>
  38. /// Construct the sound object to play sound data from the specified file.
  39. /// </Summary>
  40. Public sound (string filename ){
  41. M_filename = filename;
  42. }
  43. /// <Summary>
  44. /// Construct the sound object to play sound data from the specified stream.
  45. /// </Summary>
  46. Public sound (Stream stream ){
  47. // Read the data from the stream
  48. M_soundbytes = new byte [stream. Length];
  49. Stream. Read (m_soundbytes, 0, (INT) stream. Length );
  50. }
  51. /// <Summary>
  52. /// Play the sound
  53. /// </Summary>
  54. Public void play (){
  55. // If a file name has been registered, call wce_playsound,
  56. // Otherwise call wce_playsoundbytes
  57. If (m_filename! = NULL)
  58. Wce_playsound (m_filename, intptr. Zero, (INT) (flags. snd_async | flags. snd_filename ));
  59. Else
  60. Wce_playsoundbytes (m_soundbytes, intptr. Zero, (INT) (flags. snd_async | flags. snd_memory ));
  61. }
  62. }
  63. Step 2: add methods for creating sound class instances and playing files, for example, in the Click Event of a button.
  64. Private void btnembedded_click (Object sender, system. eventargs e ){
  65. Sound sound = new sound (assembly. getexecutingassembly (). getmanifestresourcestream ("soundsample.chimes.wav "));
  66. Sound. Play ();
  67. }
  68. Private void btnfile_click (Object sender, system. eventargs e ){
  69. Sound sound = new sound (@ "program files/soundsample/dingdang.wav ");
  70. Sound. Play ();
  71. }
  72. Compile code
  73. OK

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.