Mr. Royal
Mr. Royal

Reputation: 25

Record audio from a media player

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

Answers (2)

Berkay Yaylacı
Berkay Yaylacı

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

Maciej Jureczko
Maciej Jureczko

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.

  1. Import the namespace.

    using System.Runtime.InteropServices;

  2. 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);

  3. Start recording

    record("open new Type waveaudio Alias recsound", "", 0, 0);
    record("record recsound", "", 0, 0);

  4. Stop recording and save to file

    record("save recsound d:\myRecordedAudioFile.wav", "", 0, 0);
    record("close recsound", "", 0, 0);

Upvotes: 1

Related Questions