C # Sound Control

Source: Internet
Author: User
Control

Cannot use control to prevent delay when playing
Or can provide memory methods for storing sound data
The goal is to be able to play the sound very accurately without any delay in sound

[DllImport ("Winmm.dll")]
public static extern long PlaySound (string Name,long module,long flag);
[DllImport ("Winmm.dll")]
private static extern long mciSendString (string lpstrcommand,string lpstrreturnstring,long length,long hwndcallback);
private string M_musicname= "";
private void Playmusic ()
{
M_musicname= "\" "+tool.readinfo (" promptmusicfile ") +" "";
if (m_musicname.length==0)
Return
Try
{
mciSendString (@ "Close" + M_musicname, "", 0,0);
mciSendString (@ "open" + M_musicname, "", 0,0);
mciSendString (@ "Play" + M_musicname, "", 0,0);
}
Catch
{
}

}

private void Stopmusic ()
{
Try
{
mciSendString (@ "Close" + M_musicname, "", 0,0);
}
catch{}
}

Playback in-memory WAV files can be this way:

API definition
Private Const int snd_async = 0x1;
Private Const int snd_memory = 0x4;

[DllImport ("Winmm.dll")]
private static extern int Sndplaysounda (byte[] lpszsoundname, int uflags);

Add Blip1.wav to the project and set it as an embedded resource
Now read it into memory standby
Type T=this. GetType ();
System.Reflection.Assembly a=t.assembly;
System.IO.Stream Stream=a.getmanifestresourcestream (t.namespace+ ". Blip1.wav");
Byte[] Ba=new Byte[stream. Length];
Stream. Read (ba,0, BA. Length);
Stream. Close ();

Play Cache
Sndplaysounda (BA, snd_memory);



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.