Click here to Skip to main content
13,830,143 members
Click here to Skip to main content

Stats

730.3K views
29.9K downloads
281 bookmarked
Posted 14 Sep 2010
Licenced CPOL

PVS.AVPlayer - MCI Audio and Video Library

, 7 Aug 2018
Windows Media Control Interface (MCI) library with many added features
PVS.AVPlayer
PVS.AVPlayer .NET 2.0
PVS.AVPlayer.XML
PVS.AVPlayer .NET 3.0
PVS.AVPlayer.XML
PVS.AVPlayer .NET 3.5
PVS.AVPlayer.XML
PVS.AVPlayer .NET 4.0
PVS.AVPlayer.XML
PVS.AVPlayer .NET 4.5
PVS.AVPlayer .NET 4.5.1
PVS.AVPlayer.XML
PVS.AVPlayer .NET 4.5.2
PVS.AVPlayer.XML
PVS.AVPlayer.XML
PVS.AVPlayer .NET 4.6
PVS.AVPlayer.XML
PVS.AVPlayer.dll
PVS.AVPlayer.XML
PVS.AVPlayer.dll
PVS.AVPlayer.XML
PVS.AVPlayer.dll
PVS.AVPlayer.XML
PVS.AVPlayer.dll
PVS.AVPlayer.XML
PVS.AVPlayer.dll
PVS.AVPlayer.XML
PVS.AVPlayer.dll
PVS.AVPlayer.XML
PVS.AVPlayer.dll
PVS.AVPlayer.XML
PVS.AVPlayer .NET 4.6.1
PVS.AVPlayer.dll
PVS.AVPlayer.XML
PVS.AVPlayer .NET 4.6.2
PVS.AVPlayer.dll
PVS.AVPlayer.XML
PVS.AVPlayer.dll
PVS.AVPlayer.XML
PVS.AVPlayer .NET 4.7
PVS.AVPlayer .NET 4.7.1
PVS.AVPlayer.dll
PVS.AVPlayer.XML
PVS.AVPlayer .NET 4.7.2
PVS.AVPlayer.dll
PVS.AVPlayer.XML
PVS.AVPlayer.dll
PVS.AVPlayer.XML
PVS.AVPlayer All Source Code
AVPlayerExample
AVPlayerExample
AVPlayerExample.csproj.user
bin
Debug
PVS.AVPlayer.XML
Release
Dialogs
Display Overlays
obj
Debug
Release
x86
Debug
Release
Properties
Resources
Crystal Italic1.ttf
WingDings3a.ttf
Voice Recorder
FolderView
FolderView
bin
Debug
PVS.AVPlayer.XML
Release
FolderView.csproj.user
obj
Release
x86
Debug
Release
Properties
Resources
Crystal Italic1.ttf
PVS.AVPlayer
AVPlayerExample.csproj.user
PVS.AVPlayer.dll
PVS.AVPlayer.XML
Custom Items
Native Methods
Bob.png
Crystal Italic1.ttf
Dial Green 2.png
Dial Green 4.png
Dial Green.png
Dial Red 2.png
Dial Red.png
media7.ico
media7a.ico
Media8.ico
Media8a.ico
VU Meter.png
WingDings3a.ttf
Sound Recorder
Various
About Dialog
PVS.AVPlayer.dll
PVS.AVPlayer.XML
Custom Items
FolderView.csproj.user
Debug
Bob.png
Crystal Italic1.ttf
media7a.ico
media7b.ico
Media8a.ico
Media8b.ico
Subtitles Overlay
Various
How To (C#)
PVSAVPlayerHowTo
bin
Debug
PVS.AVPlayer.dll
PVS.AVPlayer.XML
Release
obj
Debug
Release
Properties
How To (VB.NET)
PVSAVPlayerHowToVB
bin
Debug
PVS.AVPlayer.dll
PVS.AVPlayer.XML
Release
My Project
Application.myapp
obj
Debug
Release
PVSAVPlayerHowTo.vbproj.user
PVS.AVPlayer Examples
AVPlayerExample.ex_
FolderView.ex_
AVPlayerExample.exe
FolderView.exe
PVS.AVPlayer.dll
#region Usings

using System;
using System.Drawing;
using System.Windows.Forms;
using PVS.AVPlayer;

#endregion

namespace AVPlayerExample
{
    public partial class VoiceRecorder : Form
    {
        /*
        
        Voice Recorder - using the PVS.AVPlayer Recorder Class to
        record sound (microphone input) with the MCI WaveAudio Device.
        
        */

        // ******************************** Fields

        #region Fields

        // Constants
        private const int   NO_ERROR = 0;
        private const int   WARNING_FILESIZE = 1;
        private const int   WARNING_MICROPHONE = 328;

        // Recorder
        Recorder            _myRecorder1;

        // Various
        private Form1       _baseForm;
        private VoicePlayer _voicePlayer; // to pass on filename of last recording
        private Font        _crystalFont20;

        private bool        _recFlash;      // record button flashing state
        private bool        _pauseFlash;    // pause button flashing state

        // show large file warning once
        private bool        _recWarning = true;

        private bool        _disposed;

        #endregion

        // ******************************** Main

        #region Main

        public VoiceRecorder(Form1 theForm, VoicePlayer thePlayer)
        {
            InitializeComponent();

            // Create recorder with eventhandlers:
            _myRecorder1 = new Recorder();
            // Save recording when stopped (and length > 0):
            _myRecorder1.RecorderSaveRequest += myRecorder1_RecorderSaveRequest;
            // Update time counter display:
            _myRecorder1.RecorderPositionChanged += myRecorder1_RecorderPositionChanged;
            // Flashing record button:
            _myRecorder1.RecorderStarted += myRecorder1_RecorderStartStop;
            _myRecorder1.RecorderStopped += myRecorder1_RecorderStartStop;
            // Flashing pause button:
            _myRecorder1.RecorderPaused += myRecorder1_RecorderPauseResume;
            _myRecorder1.RecorderResumed += myRecorder1_RecorderPauseResume;

            // Form handling
            _baseForm = theForm;
            Owner = _baseForm;
            _voicePlayer = thePlayer;

            // Set font
            _crystalFont20 = new Font(_baseForm.FontCollection.Families[0], 20, FontStyle.Italic);
            recCounterLabel.Font = _crystalFont20;
            recCounterLabel.UseCompatibleTextRendering = true;
        }

        protected override bool ShowWithoutActivation
        {
            get { return true; }
        }

        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
        protected override void Dispose(bool disposing)
        {
            if (!_disposed)
            {
                if (disposing)
                {
                    if (_pauseFlash) ButtonFlash.Remove(pauseButton);
                    if (_recFlash) ButtonFlash.Remove(recordButton);

                    if (_myRecorder1 != null)
                    {
                        _myRecorder1.Dispose();
                        _myRecorder1 = null;
                    }
                    _crystalFont20.Dispose(); _crystalFont20 = null;

                    if (components != null) components.Dispose();
                }
                base.Dispose(disposing);
                _disposed = true;
            }
        }

        #endregion

        // ******************************** Recorder Event Handling

        #region Recorder Event Handling

        // Start/Stop flashing record button
        void myRecorder1_RecorderStartStop(object sender, EventArgs e)
        {
            if (_myRecorder1.Recording)
            {
                if (!_recFlash) // no need for this var?
                {
                    ButtonFlash.Add(recordButton, recordButton.ForeColor, Color.Black);
                    _recFlash = true;
                }
            }
            else
            {
                if (_recFlash)
                {
                    ButtonFlash.Remove(recordButton);
                    _recFlash = false;
                }
            }
        }

        // Start/Stop flashing pause button
        void myRecorder1_RecorderPauseResume(object sender, EventArgs e)
        {
            if (_myRecorder1.Paused)
            {
                if (!_pauseFlash)
                {
                    ButtonFlash.Add(pauseButton, pauseButton.ForeColor, Color.Black);
                    _pauseFlash = true;
                }
            }
            else
            {
                if (_pauseFlash)
                {
                    ButtonFlash.Remove(pauseButton);
                    _pauseFlash = false;
                }
            }
        }

        // Update time counter display
        void myRecorder1_RecorderPositionChanged(object sender, EventArgs e)
        {
            recCounterLabel.Text = _myRecorder1.Position.ToString().Substring(0, 8);
        }

        // Save recording (event fires when recording has stopped and recording length > 0)
        void myRecorder1_RecorderSaveRequest(object sender, EventArgs e)
        {
            SaveFileDialog saveFileDialog1 = new SaveFileDialog
            {
                Title = "Save Voice Recording",
                FileName = "VoiceRecording.wav",
                DefaultExt = ".wav",
                AddExtension = true
            };

            if (saveFileDialog1.ShowDialog(this) == DialogResult.OK)
            {
                // pass on filename to voicePlayer
                if (_myRecorder1.Save(saveFileDialog1.FileName) == NO_ERROR && _voicePlayer.UseLastRecording)
                    _voicePlayer.PlayFile = saveFileDialog1.FileName;
            }
            saveFileDialog1.Dispose();
        }

        #endregion

        // ******************************** Drag Borderless Form

        #region Drag Borderless Form

        private const int WM_NCLBUTTONDOWN = 0xA1;
        private const int HT_CAPTION = 0x2;
        private void Control_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                //this.Cursor = Cursors.SizeAll;
                ((Control)sender).Capture = false;
                Message msg = Message.Create(Handle, WM_NCLBUTTONDOWN, (IntPtr)HT_CAPTION, IntPtr.Zero);
                WndProc(ref msg);
                //this.Cursor = Cursors.Default;
            }
        }

        #endregion

        // ******************************** Menu

        #region Menu

        private void inputMenuItem_Click(object sender, EventArgs e)
        {
            _myRecorder1.ShowAudioInputPanel(this);
        }

        // Channels menu
        private void monoMenuItem_Click(object sender, EventArgs e)
        {
            monoMenuItem.Checked = true;
            stereoMenuItem.Checked = false;
            _myRecorder1.Channels = Channels.Mono;
        }

        private void stereoMenuItem_Click(object sender, EventArgs e)
        {
            monoMenuItem.Checked = false;
            stereoMenuItem.Checked = true;
            _myRecorder1.Channels = Channels.Stereo;
        }

        // Bits menu
        private void bits8MenuItem_Click(object sender, EventArgs e)
        {
            bits8MenuItem.Checked = true;
            bits16MenuItem.Checked = false;
            _myRecorder1.Bits = Bits.Bits8;
        }

        private void bits16MenuItem_Click(object sender, EventArgs e)
        {
            bits8MenuItem.Checked = false;
            bits16MenuItem.Checked = true;
            _myRecorder1.Bits = Bits.Bits16;
        }

        // Sample Rate menu
        private void hz11025MenuItem_Click(object sender, EventArgs e)
        {
            hz11025MenuItem.Checked = true;
            hz22050MenuItem.Checked = false;
            hz44100MenuItem.Checked = false;
            _myRecorder1.SampleRate = SampleRate.Samples11025;
        }

        private void hz22050MenuItem_Click(object sender, EventArgs e)
        {
            hz11025MenuItem.Checked = false;
            hz22050MenuItem.Checked = true;
            hz44100MenuItem.Checked = false;
            _myRecorder1.SampleRate = SampleRate.Samples22050;
        }

        private void hz44100MenuItem_Click(object sender, EventArgs e)
        {
            hz11025MenuItem.Checked = false;
            hz22050MenuItem.Checked = false;
            hz44100MenuItem.Checked = true;
            _myRecorder1.SampleRate = SampleRate.Samples44100;

            if (_recWarning)
            {
                ShowErrorMessage(WARNING_FILESIZE);
                _recWarning = false;
            }
        }

        // Hide menu
        private void hideMenuItem_Click(object sender, EventArgs e)
        {
            _baseForm.showRecorderMenuItem_Click(this, EventArgs.Empty);
        }

        // Close menu
        private void closeMenuItem_Click(object sender, EventArgs e)
        {
            _myRecorder1.Stop();
            _myRecorder1.Paused = false;
            _baseForm.showRecorderMenuItem_Click(this, EventArgs.Empty);
        }

        #endregion

        // ******************************** Record, Pause and Stop Buttons

        #region Record, Pause and Stop Buttons

        // record button
        private void recordButton_Click(object sender, EventArgs e)
        {
            if (_myRecorder1.Recording) _myRecorder1.Stop();
            else
            {
                _myRecorder1.Record();
                if (_myRecorder1.LastError) ShowErrorMessage(_myRecorder1.LastErrorCode);
            }
        }

        // pause button
        private void pauseButton_Click(object sender, EventArgs e)
        {
            _myRecorder1.Paused = !_myRecorder1.Paused;
        }

        // stop button
        private void stopButton_Click(object sender, EventArgs e)
        {
            _myRecorder1.Stop();
        }

        #endregion

        // ******************************** Close Recorder (Stop and Reset Pause (from Main Form))

        #region Close Recorder

        internal void CloseRecorder()
        {
            _myRecorder1.Stop();
            _myRecorder1.Paused = false;
        }

        #endregion

        // ******************************** Error and Warning Message Dialog

        #region Error and Warning Message Dialog

        private void ShowErrorMessage(int errCode)
        {
            string errorText = errCode == WARNING_FILESIZE ? "Please Note:\r\nHigh quality recording may result in very large file sizes." : _myRecorder1.GetErrorString(errCode);
            if (errCode == WARNING_MICROPHONE) errorText += "\r\n\r\nPlease select an input device using the Voice Recorder's 'Microphone' menu item.";

            ErrorDialog errorDialog = new ErrorDialog(Form1.APPLICATION_NAME, "VOICE RECORDER\r\n\r\n" + errorText, false);
            errorDialog.checkBox1.Hide();
            errorDialog.checkBox2.Hide();

            _baseForm.CenterDialog(this, errorDialog);
            errorDialog.ShowDialog();

            errorDialog.Dispose();
        }

        #endregion
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

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

Permalink | Advertise | Privacy | Cookies | Terms of Use | Mobile
Web04 | 2.8.190114.1 | Last Updated 7 Aug 2018
Article Copyright 2010 by Peter Vegter
Everything else Copyright © CodeProject, 1999-2019
Layout: fixed | fluid