Use C # vocal (finishing)

xiaoxiao2021-03-05  29

Due to the elements that have no sound in the C #, the API function of the system is required to achieve this, which is only included in this document, and does not include a non-Window operating system.

Below is the code:

Using System.Runtime.InteropServices

// Call WinAPI in C #

[DLLIMPORT ("Winmm.dll", entrypoint = "plays")]]

Public Static Extern Int Plays (String Lpxzname, Int HMODUL, INT DWFLAGS);

/ / How to use Playsound ()

INT SND_ASYNC = 0x1;

INT SND_FILENAME = 0x20000;

INT SND_LOOP = 0x8;

// Run the Plays function

Playsound ("audio file name .wav", 0, SND_ASYNC | SND_FILENAME | SND_LOOP);

If the audio file is too large, there is another way to solve the problem of system resource tightness. For example, a method of dividing a file and a buffer.

as follows:

// Declare some call of API functions

[Dllimport ("Winmm.dll")] Public Static Extern long Plays (String Name, Long Module, Long Flag); [DLLIMM.DLL "] Private Static Extern long mcIndstring (String LPSTRCOMMAND,

String LPSTRRRRNSTRING, Long Length, long hwndcallback;

Private string m_musicname = ""; private void playmusic () {m_musicname = "/" " TOOL.ReadInfo (" promptmusicfile ") " / ""; // promptmusicfile is the target file IF (m_musicname.length == 0) Return Try {mciSendstring (@ "close" m_musicname, "file path and name", 0, 0); mcIndstring (@ "open" m_musicname, "" ", 0, 0); mcIndstring (@" play " m_musicname, "", 0, 0);} catch {}}

Private void stopmusic () {Try {mcIndstring (@ "close" m_musicname, "", 0, 0);} catch {}}

The following code is a method instance of establishing a buffer mechanism in memory:

// API Definition Private const INT SND_ASYNC = 0x1; private const INT SND_MEMORY = 0x4;

[DLLIMM.DLL "] Private static extern int sndplaySounda (byte [] lpszsoundname, int uflags); // Add blip1.wav Add Identification and set to embedded resources // now read it into memory ready to use 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 () ;. STREAM.CLOSE ();

// Play the cache SNDPLAYSOUNDA (Ba, SND_MEMORY);

The following is a description of Playsound and McISendstring:

In the description winmm.dll PlaySound: Platforms: Win 32s, Win 95/98, Win NT PlaySound plays a waveform sound through the speakers This sound could be a .wav file, a system event sound (such as the system startup sound. ), or a sound resource stored in an application. Note that when the function needs to play an application resource or a RAM-loaded sound, Visual Basic users must use the alternate declare of the function in order to pass the numeric identifier of the sound instead of a string the function returns 0 if an error occured, or a non-zero value if successful description mciSendString of:... mciGetErrorString obtains a textual description of an error raised by another Media Control Interface (MCI) function Typically these errors are not the fault of the program. Rather, they are caused by "problems" with the device (for example, the MIDI driver is currently being used by another program, so your program's attempt to open it failed). The messages retrieved by this Function Are Sufficient to Tell The User What caused the error.

PS: A piece of code that is recorded, and the timing page refresh and executes some operations can be refreshed with Window.setInterval () in JavaScript, or refreshed with Meta Refresh, such as ()

Implementation of Window.setInterval ():