Advertisement
Rythorian

Tutorial1

Nov 16th, 2024
32
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.36 KB | None | 0 0
  1. using NAudio.Wave;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.ComponentModel;
  5. using System.Data;
  6. using System.Drawing;
  7. using System.Linq;
  8. using System.Text;
  9. using System.Threading.Tasks;
  10. using System.Windows.Forms;
  11.  
  12. namespace Blits_Audio
  13. {
  14. public partial class BlitsAudio : Form
  15. {
  16.  
  17.  
  18. // Create class-level accessible variables to store the audio recorder and capturer instance
  19. private WaveFileWriter RecordedAudioWriter = null;
  20. private WasapiLoopbackCapture CaptureInstance = null;
  21.  
  22. public BlitsAudio()
  23. {
  24. InitializeComponent();
  25.  
  26. }
  27.  
  28. private void button1_Click(object sender, EventArgs e)
  29. {
  30.  
  31. string outputFilePath = $@"{Environment.GetFolderPath(Environment.SpecialFolder.Desktop)}\system_recorded_audio.wav";
  32. // string outputFilePath = @"C:\Users\domsd\Desktop\system_recorded_audio.wav";
  33.  
  34. // Redefine the capturer instance with a new instance of the LoopbackCapture class
  35. CaptureInstance = new WasapiLoopbackCapture();
  36.  
  37. // Redefine the audio writer instance with the given configuration
  38. RecordedAudioWriter = new WaveFileWriter(outputFilePath, CaptureInstance.WaveFormat);
  39.  
  40. // When the capturer receives audio, start writing the buffer into the mentioned file
  41. CaptureInstance.DataAvailable += (s, a) =>
  42. {
  43. RecordedAudioWriter.Write(a.Buffer, 0, a.BytesRecorded);
  44. };
  45.  
  46. // When the Capturer Stops
  47. CaptureInstance.RecordingStopped += (s, a) =>
  48. {
  49. RecordedAudioWriter.Dispose();
  50. RecordedAudioWriter = null;
  51. CaptureInstance.Dispose();
  52. };
  53.  
  54. // Enable "Stop button" and disable "Start Button"
  55. button1.Enabled = false;
  56. button2.Enabled = true;
  57.  
  58. // Start recording !
  59. CaptureInstance.StartRecording();
  60.  
  61. }
  62.  
  63. private void button2_Click(object sender, EventArgs e)
  64. {
  65.  
  66. // Stop recording !
  67. CaptureInstance.StopRecording();
  68. // Enable "Start button" and disable "Stop Button"
  69. button1.Enabled = true;
  70. button2.Enabled = false;
  71.  
  72.  
  73.  
  74.  
  75. }
  76.  
  77.  
  78. }
  79. }
  80.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement