. Net simple recording and playback audio file code,. net recording audio code
Features of this Code: DirectX is not required and applicable to C/S and B/S.
Method:
[Csharp]View plaincopy
- // MciSendStrin. It is an API command used to play multimedia files. It can play MPEG, AVI, WAV, MP3, and so on. The following describes how to use it:
- // The first parameter: The command string to be sent. The string structure is [command] [device alias] [command parameters].
- // Second parameter: the buffer of the returned information, which is a string variable with a specified size.
- // The third parameter: the buffer size, that is, the length of the character variable.
- // The fourth parameter is the callback method, which is generally set to zero.
- // Return value: if the function is successfully executed, zero is returned; otherwise, error code is returned.
- [DllImport ("winmm. dll", EntryPoint = "mciSendString", CharSet = CharSet. Auto)]
- Private static extern int mciSendString (
- String lpstrCommand,
- String lpstrReturnString,
- Int uReturnLength,
- Int hwndCallback );
- Private static void mciSendString (String cmd)
- {
- MciSendString (cmd, "", 0, 0 );
- }
- Private static void StartRecord ()
- {
- MciSendString ("close movie ");
- MciSendString ("open new type WAVEAudio alias movie ");
- MciSendString ("record movie ");
- }
- Private static void StopRecord (string filename)
- {
- MciSendString ("stop movie ");
- MciSendString ("save movie" + filename );
- MciSendString ("close movie ");
- }
Example:
[Csharp]View plaincopy
- Protected void btStart_Click (object sender, EventArgs e)
- {
- // Start recording
- StartRecord ();
- }
- Protected void btStop_Click (object sender, EventArgs e)
- {
- // Stop recording
- StopRecord (@ "C: \ test.wav ");
- }
- Protected void btPlay_Click (object sender, EventArgs e)
- {
- // You can also use the TTS (Text To Speech) in the window system To play a recording.
- SoundPlayer sp = new SoundPlayer (@ "c: \ test.wav ");
- Sp. PlaySync ();
- }
How can I record the online audio files?
MP3 audio recorders are easy to use and provide sound quality.
Net how to obtain the playback time of the audio file
The following code first references the com component shell32.dll:
Shell32.Shell shell = new Shell32.Shell ();
// File path
Shell32.Folder folder = shell. NameSpace (folder path );
// File name
Shell32.FolderItem folderitem = folder. ParseName (file name. Extension );
If (Environment. OSVersion. Version. Major> = 6)
{
Return folder. GetDetailsOf (folderitem, 27 );
}
Else
{
Return folder. GetDetailsOf (folderitem, 21 );
}