"Original" + "Reference" hands-on experiment: use C # To play a sound in PPC

Source: Internet
Author: User

Today, the project requires a sound to remind the user that a certain state of the program has been activated. Net CF does not have such a good dongdongniao ~, As a result, ghost and Baike finally came up with another ZZK. They found that MSDN and the town articles both mentioned how to play the sound under PPC (there are also many articles about the same method, however, some netizens reported that the video could not be played successfully, so I filtered it out here.) in the articles mentioned in msdn, many of my friends reported that the video could not be played and I did not confirm it here, I directly used the town code and successfully played the sound. Therefore, I wrote this article to make a backup and share it with you ~

First, we create a WM5 ppc project named SoundPlay under. net cf 2.0. Of course, I chose C # As the language #.

Then the interface is designed as follows:

LblFileName -- Text property: file address

BtnOpen -- Text properties: browse files

BtnPlay -- Text attribute: Playing Sound

Create a new class: Sound, the code is similar to that of the town, but I have made a few changes, so I also posted it for your convenience:

 

Sound
Using System;
Using System. Runtime. InteropServices;
Using System. IO;
Using System. Collections. Generic;
Using System. Text;

Namespace SoundPlay
{
Public class Sound
{
Private byte [] m_soundBytes;
Private enum Flags
{
SND_SYNC = 0x0000,/* play synchronously (default )*/
SND_ASYNC = 0x0001,/* play asynchronously */
SND_NODEFAULT = 0x0002,/* silence (! Default) if sound not found */
SND_MEMORY = 0x0004,/* pszSound points to a memory file */
SND_LOOP = 0x0008,/* loop the sound until next sndPlaySound */
SND_NOSTOP = 0x0010,/* don't stop any currently playing sound */
SND_NOWAIT = 0x00002000,/* don't wait if the driver is busy */
SND_ALIAS = 0x00010000,/* name is a registry alias */
SND_ALIAS_ID = 0x00110000,/* alias is a predefined ID */
SND_FILENAME = 0x00020000,/* name is file name */
SND_RESOURCE = 0x00040004/* name is resource name or atom */
}
[DllImport ("CoreDll. DLL", EntryPoint = "PlaySound", SetLastError = true)]
Private extern static int MobilePlaySound (string szSound, IntPtr hMod, int flags); // play an external sound file

[DllImport ("CoreDll. DLL", EntryPoint = "PlaySound", SetLastError = true)]
Private extern static int MoiblePlaySoundBytes (byte [] szSound, IntPtr hMod, int flags); // playback embedded sound resources

/// <Summary>
/// Construct the Sound object to play sound data from the specified stream.
/// </Summary>
Public Sound (Stream stream)
{
// Read the data from the stream
M_soundBytes = new byte [stream. Length];
Stream. Read (m_soundBytes, 0, (int) stream. Length );
}
/// <Summary>
/// From a Sound byte stream constructor Sound
/// </Summary>
/// <Param name = "snd"> </param>
Public Sound (byte [] snd)
{
M_soundBytes = snd;
}
/// <Summary>
/// Construct an empty Sound function for playing external files
/// </Summary>
Public Sound ()
{
}

/// <Summary>
/// Play the sound File Play method for external files
/// </Summary>
Public void Play (string filename)
{
MobilePlaySound (filename, IntPtr. Zero, (int) (Flags. SND_ASYNC | Flags. SND_FILENAME ));

}
/// <Summary>
/// Play The Stream Sound. This method should correspond to The Sound constructed from Byte and Stream.
/// Note that its Flags is set to SND_MEMORY, because our audio files have been converted to memory.
/// </Summary>
Public void Play ()
{
MoiblePlaySoundBytes (m_soundBytes, IntPtr. Zero, (int) (Flags. SND_ASYNC | Flags. SND_MEMORY ));
}
}
}

 

The button Event code on the main interface is as follows:

 

Button code
Private void btnOpen_Click (object sender, EventArgs e)
{
Using (OpenFileDialog ofd = new OpenFileDialog ())
{
If (ofd. ShowDialog () = DialogResult. OK)
{
LblFileName. Text = ofd. FileName;
}
}
}

Private void btnPlay_Click (object sender, EventArgs e)
{
Sound sound = new Sound ();
Sound. Play (lblFileName. Text );
}

 

After testing, both methods can generate the selected .wav audio file.

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.