Click here to Skip to main content
13,148,510 members (67,130 online)
Click here to Skip to main content
Add your own
alternative version

Stats

14K views
1.2K downloads
9 bookmarked
Posted 4 Aug 2016

PVS.AVPlayer - MCI Sound Recorder

, 21 Aug 2017
Rate this:
Please Sign up or sign in to vote.
A brief guide for creating a sound recorder with the PVS.AVPlayer library

Version 0.72: player update, taskbar marquee indicators, input devices error handling, extended sample rates (8000 to 192000 Hz), 24/32 bits recording and others. For more information please see the 'About PVS.AVPlayer versions.txt' download.

Library size: 130 KB
Platform : Microsoft Windows - WinForms Any CPU
Requirements: Microsoft .NET Framework 2.0 or higher
License : CPOL - Free to Use

Version 0.72, Copyright © 2017 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

  1. Adding PVS.AVPlayer to your project
  2. Creating a Sound Recorder
  3. Sound Recorder Settings
  4. Starting a Recording
  5. Stopping and Saving a Recording
  6. Input Level and Position Information
  7. Playing a Recording

1. Adding PVS.AVPlayer to Your Project

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;

2. Creating a Sound Recorder

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; // use the system's default input device

            recorder1.Events.RecorderInputLevelChanged += InputLevelChanged;
            if (recorder1.LastError) ... // if LastError is true, there's no input device

            recorder1.Events.RecorderPositionChanged += PositionChanged;
            recorder1.Events.RecorderSaveRequest += SaveRequest;
        }

        private void InputLevelChanged (object sender, InputLevelEventArgs e)
        {
             // ... the sound input (volume) level has changed.
        }

        private void PositionChanged (object sender, RecorderPosEventArgs e)
        {
             // ... the recording position has changed.
        }

        private void SaveRequest (object sender, EventArgs e)
        {
            // ... a recording has ended and can be saved to a file.
        }
    }
}

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.

3. Sound Recorder Settings

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;  // set the second device from the list as input device
if (recorder1.LastError) ...      // if LastError is true, setting the input device has failed

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;  // use the system's default input device (if any)

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.

4. Starting a Recording

Once you have created a recorder and its settings are set correctly, you can start recording with:

recorder1.Record(); // start a new recording (same as recorder1.Start();)

At any time during a recording, you can pause and resume the recording with:

recorder1.Pause();  // temporarily stop recording
recorder1.Resume(); // continue a paused recording

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'.

5. Stopping and Saving a 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); // or show SaveFileDialog
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.

6. Input Level and Position Information

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:

Sound Input Level Information

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(); // example: show the values in labels
     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.

Position Information

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(); // example: show the value in a label
}

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.

7. Playing a Recording

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.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Peter Vegter
United States United States
No Biography provided

You may also be interested in...

Comments and Discussions

 
Questioninput level metering Pin
Member 1208593129-Nov-16 4:05
memberMember 1208593129-Nov-16 4:05 
AnswerRe: input level metering Pin
Peter Vegter29-Nov-16 8:02
memberPeter Vegter29-Nov-16 8:02 
GeneralRe: input level metering Pin
Member 1208593130-Nov-16 4:32
memberMember 1208593130-Nov-16 4:32 
GeneralRe: input level metering Pin
Peter Vegter30-Nov-16 4:44
memberPeter Vegter30-Nov-16 4:44 
GeneralRe: input level metering Pin
Member 1208593130-Nov-16 5:23
memberMember 1208593130-Nov-16 5:23 
GeneralRe: input level metering Pin
Peter Vegter30-Nov-16 5:37
memberPeter Vegter30-Nov-16 5:37 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.170924.2 | Last Updated 21 Aug 2017
Article Copyright 2016 by Peter Vegter
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid