Reputation: 25
How do I record audio from my media player using c# winforms.
I'm trying to create an app that records audio from a player(vlc) and then saves it to my computer.
Any idea will be highly appreciated.
Upvotes: 1
Views: 477
Reputation: 4513
You can use NAudio, here is a quick sample
Two buttons Record and Stop,
public WaveIn _waveIn = null;
public WaveFileWriter fileToWrite = null;
private void btn_record_Click(object sender, EventArgs e) {
_waveIn = new WaveIn();
_waveIn.WaveFormat = new WaveFormat(44100, 1);
_waveIn.DataAvailable += _waveIn_DataAvailable; // event that keep listening mic
fileToWrite = new WaveFileWriter(@"C:\Users\userName\Documents\myFile.wav", _waveIn.WaveFormat);
_waveIn.StartRecording();
}
private void _waveIn_DataAvailable(object sender, WaveInEventArgs e) {
if (fileToWrite != null) {
fileToWrite.Write(e.Buffer, 0, e.BytesRecorded); // writes bytes to the wav file
fileToWrite.Flush();
}
}
private void btn_stop_Click(object sender, EventArgs e) {
_waveIn.StopRecording();
}
Hope helps,
Upvotes: 0
Reputation: 1598
What do you mean using your media player? If you simply want to do it using C# just use the winmm.dll library.
Import the namespace.
using System.Runtime.InteropServices;
Declare the interop function
[DllImport("winmm.dll",EntryPoint="mciSendStringA", ExactSpelling=true, CharSet=CharSet.Ansi, SetLastError=true)]
private static extern int record(string lpstrCommand, string lpstrReturnString, int uReturnLength, int hwndCallback);
Start recording
record("open new Type waveaudio Alias recsound", "", 0, 0);
record("record recsound", "", 0, 0);
Stop recording and save to file
record("save recsound d:\myRecordedAudioFile.wav", "", 0, 0);
record("close recsound", "", 0, 0);
Upvotes: 1