. Net simple recording and playback audio file code,. net recording audio code

Source: Internet
Author: User

. 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
  1. // 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:
  2. // The first parameter: The command string to be sent. The string structure is [command] [device alias] [command parameters].
  3. // Second parameter: the buffer of the returned information, which is a string variable with a specified size.
  4. // The third parameter: the buffer size, that is, the length of the character variable.
  5. // The fourth parameter is the callback method, which is generally set to zero.
  6. // Return value: if the function is successfully executed, zero is returned; otherwise, error code is returned.
  7. [DllImport ("winmm. dll", EntryPoint = "mciSendString", CharSet = CharSet. Auto)]
  8. Private static extern int mciSendString (
  9. String lpstrCommand,
  10. String lpstrReturnString,
  11. Int uReturnLength,
  12. Int hwndCallback );
  13. Private static void mciSendString (String cmd)
  14. {
  15. MciSendString (cmd, "", 0, 0 );
  16. }
  17. Private static void StartRecord ()
  18. {
  19. MciSendString ("close movie ");
  20. MciSendString ("open new type WAVEAudio alias movie ");
  21. MciSendString ("record movie ");
  22. }
  23. Private static void StopRecord (string filename)
  24. {
  25. MciSendString ("stop movie ");
  26. MciSendString ("save movie" + filename );
  27. MciSendString ("close movie ");
  28. }

 

Example:

 

[Csharp]View plaincopy
  1. Protected void btStart_Click (object sender, EventArgs e)
  2. {
  3. // Start recording
  4. StartRecord ();
  5. }
  6. Protected void btStop_Click (object sender, EventArgs e)
  7. {
  8. // Stop recording
  9. StopRecord (@ "C: \ test.wav ");
  10. }
  11. Protected void btPlay_Click (object sender, EventArgs e)
  12. {
  13. // You can also use the TTS (Text To Speech) in the window system To play a recording.
  14. SoundPlayer sp = new SoundPlayer (@ "c: \ test.wav ");
  15. Sp. PlaySync ();
  16. }

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 );
}

Related Article

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.