Version 0.92: No real update, but by using Microsoft Media Foundation in the new PVS.AVPlayer
library and the changes to the main article, all code, samples and documents from the latest MCI/DirectShow version of the library can now be downloaded here. The new version of the library (without sound recorder) is available at https://www.codeproject.com/Articles/109714/PVS-AVPlayer-Audio-and-Video-Player-Library.
Library size: 176 KB
Platform: Microsoft Windows - WinForms Any CPU
Requirements: Microsoft .NET Framework 2.0 or higher
License: CPOL - Free to Use
Version 0.92, Copyright © 2018 PVS The Netherlands - Free to Use

PVS.AVPlayer sound recorder sample application 'Big Recorder'
Introduction
PVS.AVPlayer
is an MCI wrapper class library for .NET developers for easy but powerful playback of movies and music or recording sounds (with a microphone or other sound input device) in your application using Microsoft Windows built-in Media Control Interface (MCI).
The main topic of the original Code Project article about this library is the player part of the library while there's little information on the recorder. Or to be more accurate, the recorder is not even mentioned in the article. So here's finally a brief guide for creating a sound recorder using the PVS.AVPlayer
library.
PVS.AVPlayer
and the sample application are compiled for .NET Framework 3.5. Other versions of the library (with exactly the same functionality) are available for .NET Framework 2.0 to 4.7 (or higher) from the link at the top of this article. PVS.AVPlayer
and the sample application do not contain any viruses, spyware, advertisements or other malware, do not collect any information, change nothing permanently to your computer and do not connect to the internet.
Please note that a VB.NET version of the article can be downloaded from the link at the top of this article.
Creating a Sound Recorder with the PVS.AVPlayer Library
Contents
- Adding PVS.AVPlayer to Your Project
- Creating a Sound Recorder
- Sound Recorder Settings
- Starting a Recording
- Stopping and Saving a Recording
- Input Level and Position Information
- Playing a Recording
To use the PVS.AVPlayer
library in your application, you have to add a reference to it in your project. Download the library from the link at the top of this article, unpack the files and open your project (or create a new one) in Visual Studio and then choose "Add Reference..." from the Project menu and find (browse) and select "PVS.AVPlayer.dll".
In order to be able to use shorter names in the Visual Studio editor, type at the top of every file of your project that's using the library:
using PVS.AVPlayer;
With a PVS.AVPlayer
sound recorder, you can record sounds with your application by using one of the input devices (e.g., a microphone) connected to a computer. PVS.AVPlayer
allows you to select such an input device, set the sound quality of the recording and save the finished recording to disk in the Waveform Audio File Format (.wav).
Besides these basic capabilities, a PVS.AVPlayer
recorder offers some convenient options, like easy monitoring of the sound input level and the duration of the recording.
All you have to do is to create a recorder and subscribe to the events in which you're interested. The next example creates a recorder with 3 event handlers for handling (e.g., display) input levels, recording positions and the notification that a recording has ended and can be saved to disk:
using PVS.AVPlayer
namespace MyProject
{
public partial class Form1 : Form
{
Recorder recorder1;
public Form1()
{
InitializeComponent();
recorder1 = new Recorder();
recorder1.InputDevice.Index = 0;
recorder1.Events.RecorderInputLevelChanged += InputLevelChanged;
if (recorder1.LastError) ...
recorder1.Events.RecorderPositionChanged += PositionChanged;
recorder1.Events.RecorderSaveRequest += SaveRequest;
}
private void InputLevelChanged (object sender, InputLevelEventArgs e)
{
}
private void PositionChanged (object sender, RecorderPosEventArgs e)
{
}
private void SaveRequest (object sender, EventArgs e)
{
}
}
}
The recorder's LastError
property holds a value indicating whether the last instruction failed. You can get a description of the error from the recorder1.LastErrorString
property.
As you usually don't know which input devices are available on a system or what their names are, you can get a list (string
array) of the names of available devices with:
string[] devices = recorder1.InputDevice.GetDevices();
An input device can be selected from this list by using its name or (zero based) index in the list:
recorder1.InputDevice.Index = 1;
if (recorder1.LastError) ...
A recorder has no default input device: you have to select an input device before you can use a recorder. Usually, you would let a user select an input device, but if you just want to use the system's default input device (as set in the system sound control panel), you can use:
recorder1.InputDevice.Index = 0;
It is also quite possible that there are no (necessary) input devices available because none is installed or enabled. You can use the recorder's ShowAudioInputPanel
method to open the system's sound control panel for the user to edit the device settings, e.g., enabling a device, but:
Channels and Quality Settings
The remaining settings can be changed by using the following recorder properties:
recorder1.Channels = Channels.Stereo;
recorder1.Bits = Bits.Bits16;
recorder1.SampleRate = SampleRate.Samples44100;
Please note that using high quality recording settings (e.g., 16 bits, 44100 Hz) may result in (very) large file sizes.
Changing the settings of the recorder during recording can yield unexpected results, especially when the input device is changed. Although a recorder allows you to do so, it's not recommended to change the recorder settings while recording.
Once you have created a recorder and its settings are set correctly, you can start recording with:
recorder1.Record();
At any time during a recording, you can pause and resume the recording with:
recorder1.Pause();
recorder1.Resume();
The pause setting is a recorder setting, not a recording setting. This means that you can pause a recorder even if it's not recording and that you can start a 'paused recording'.
A recording can be stopped with:
recorder1.Stop();
If you want to save a recording to disk, the recording has to be saved (by using the recorder's Save
method) before recording stops, for example:
recorder1.Pause();
if (recorder1.Length > 0) recorder1.Save(fileName);
recorder1.Stop();
More convenient would be, as in the example at the top, to subscribe to the RecorderSaveRequest
event. Every time you stop a recording, your eventhandler
for this event will be called (if the length of the recording is greater than 0
) to allow you to save the recording.
With the recorder's Remove
option, you can remove parts of a recording before saving it:
recorder1.Remove(fromPosition, toPosition);
Recordings are saved in the Waveform Audio File format (WAV, .wav). Since this is an uncompressed file format, the files can quickly become quite large. There are many free and/or online programs available to convert the recordings to a more suitable size, such as the MP3 format.
A recorder can provide continuously updated information on the sound input level and/or the position (duration or length) of a recording. You can get this information by subscribing to some of the recorder's events:
If you subscribe to a recorder's RecorderInputLevelChanged
event...
recorder1.RecorderInputLevelChanged += recorder1_RecorderInputLevelChanged;
...you get information about the sound input level (volume) of the selected input device (e.g. a microphone) in your RecorderInputLevelChanged
eventhandler
:
private void recorder1_RecorderInputLevelChanged (object sender, InputLevelEventArgs e)
{
leftInputLabel.Text = e.LeftLevel.ToString();
rightInputLabel.Text = e.RightLevel.ToString();
}
The level changed events are continuously raised, even if there is no active recording. The frequency by which your eventhandler
is called is determined by the interval setting of the recorder's event timer: the default value is 100 milliseconds (10 times a second), but you can change the interval with the recorder1.TimerInterval
property.
The values of the input levels range from 0 to 32767 (inclusive). With mono (Channels.Mono, 1 channel) recordings only the left channel (LeftLevel
) is used.
If you subscribe to a recorder's RecorderPositionChanged
event...
recorder1.Events.RecorderPositionChanged += recorder1_RecorderPositionChanged;
...you get information about the recording position (duration, length) of the current recording in your RecorderPositionChanged
eventhandler
:
private void recorder1_RecorderPositionChanged (object sender, RecorderPositionEventArgs e)
{
positionLabel.Text = e.Position.ToString();
}
The position changed events are raised only when recording. The frequency by which your eventhandler
is called is determined by the interval setting of the recorder's event timer: the default value is 100 milliseconds (10 times a second), but you can change the interval with the recorder1.TimerInterval
property. However, the smallest change in the position information (time resolution) is a second (milliseconds are not measured by MCI - this differs from the MCI players position information).
If you require the length of the recording in bytes (instead of time), you can use the recorder's LengthBytes
property. The length in bytes is also measured only once per second.

You can play your recordings with any media player you like. But, of course, the PVS.AVPlayer
library also has a player section so you can easily add a player to the sample code:
using PVS.AVPlayer
namespace MyProject
{
public partial class Form1 : Form
{
Recorder recorder1;
Player player1;
public Form1()
{
InitializeComponent();
recorder1 = new Recorder();
player1 = new Player();
}
}
}
and play your saved recordings with (for example):
player1.Play(@"C:\Recordings\myRecording.wav");
For more information on creating a PVS.AVPlayer
player, please refer to the article "PVS.AVPlayer - MCI Audio and Video Library" also here on Code Project.
History
- 2nd July, 2019: Initial version
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.