Click here to Skip to main content
14,207,050 members
Click here to Skip to main content
Add your own
alternative version

Stats

760.9K views
31.2K downloads
292 bookmarked
Posted 14 Sep 2010
Licenced CPOL

PVS.AVPlayer - Audio and Video Player Library

, 14 Jun 2019
Microsoft Media Foundation (MF) based easy-to-use 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 .NET 4.8
PVS.AVPlayer.dll
PVS.AVPlayer.XML
PVS.AVPlayer Licenses
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.png
Dial Normal 2.png
Dial Red 2.png
Dial Red.png
Kaizen-4.png
Media Normal.ico
Media Paused.ico
Media Paused1.ico
Media Playing.ico
PVSLogo.png
PVSLogoOutline.png
VU Meter.png
WingDings3a.ttf
Various
PVS.AVPlayer Licenses
About Dialog
PVS.AVPlayer.dll
PVS.AVPlayer.XML
Custom Items
FolderView.csproj.user
Media8b.ico
Debug
Bob.png
Crystal Italic1.ttf
Media Paused.ico
media7a.ico
media7b.ico
Media8a.ico
Media8b.ico
PVSLogoOutline.png
Subtitles Overlay
Various
PVS.AVPlayer Licenses
How To (C#)
PVS.AVPlayer Licenses
PVSAVPlayerHowTo
bin
Debug
PVS.AVPlayer.dll
PVS.AVPlayer.XML
Release
obj
Debug
Release
Properties
How To (VB.NET)
PVS.AVPlayer Licenses
PVSAVPlayerHowToVB
bin
Debug
PVS.AVPlayer.dll
PVS.AVPlayer.XML
Release
My Project
Application.myapp
obj
Debug
Release
PVSAVPlayerHowTo.vbproj.user
PVS.AVPlayer Licenses
PVS.AVPlayer Examples
AVPlayerExample.ex_
FolderView.ex_
AVPlayerExample.exe
FolderView.exe
PVS.AVPlayer Licenses
PVS.AVPlayer.dll
#region Usings

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

#endregion

namespace AVPlayerExample
{
    public partial class VoicePlayer : Form
    {
        /*
        
        VoicePlayer - the playback part of the Voice Recorder,
        using a PVS.AVPlayer Player.
        
        */

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

        #region Fields

        // Form
        private Form1       _baseForm;
        private Font        _crystalFont20;

        // Player
        private Player      _voicePlayer1;

        private bool        _countDown; // countdown counter
        internal bool       UseLastRecording = true;
        internal string     PlayFile = string.Empty; // file to play - selected (with 'Open') or last recording

        private bool        _disposed;

        #endregion

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

        #region Main

        public VoicePlayer(Form1 theForm)
        {
            InitializeComponent();

            _baseForm = theForm;
            Owner = _baseForm;

            _crystalFont20 = new Font(_baseForm.FontCollection.Families[0], 20, FontStyle.Italic);

            playCounterLabel.Font = _crystalFont20;
            playCounterLabel.UseCompatibleTextRendering = true;

            // create an 'invisible display' (plays video sound only)
            Panel voiceDisplay = new Panel {Bounds = new Rectangle(-20, -20, 16, 16)};
            Controls.Add(voiceDisplay);

            _voicePlayer1 = new Player()
            {
                AudioVolumeSlider = volumeSlider,
                AudioBalanceSlider = balanceSlider,
                PositionSlider = positionSlider,
                Display = voiceDisplay
            };

            _voicePlayer1.MediaPositionChanged += voicePlayer1_MediaPositionChanged;
            _voicePlayer1.MediaStarted += voicePlayer1_MediaPlayingChanged;
            _voicePlayer1.MediaEnded += voicePlayer1_MediaPlayingChanged;
            _voicePlayer1.MediaStopped += voicePlayer1_MediaPlayingChanged;
        }

        void voicePlayer1_MediaPositionChanged(object sender, EventArgs e)
        {
            playCounterLabel.Text = _countDown ? _voicePlayer1.GetMediaLength(MediaLength.ToEnd).ToString().Substring(0, 8) : _voicePlayer1.Position.ToString().Substring(0, 8);
        }

        // Update interface with media playing / non playing
        void voicePlayer1_MediaPlayingChanged(object sender, EventArgs e)
        {
            playLight.LightOn = _voicePlayer1.Playing;
        }

        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 (_voicePlayer1.Paused) ButtonFlash.Remove(pauseButton);

                    _voicePlayer1.Dispose(); _voicePlayer1 = null;
                    _crystalFont20.Dispose(); _crystalFont20 = null;

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

        #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);
                base.WndProc(ref msg);
                //this.Cursor = Cursors.Default;
            }
        }

        #endregion

        // ******************************** Play Media

        #region Play Media

        private void PlayMedia()
        {
            if (PlayFile == string.Empty) SelectMedia();

            if (PlayFile != string.Empty)
            {
                _voicePlayer1.Play(PlayFile);
                if (_voicePlayer1.LastError)
                {
                    ShowErrorMessage();
                    PlayFile = string.Empty;
                }
            }
        }

        internal void ClosePlayer()
        {
            _voicePlayer1.Stop();
            if (_voicePlayer1.Paused) pauseButton.PerformClick();
            _voicePlayer1.Repeat = repeatMenuItem.Checked = false;
            UseLastRecording = lastRecordingMenuItem.Checked = true;
            PlayFile = string.Empty;
        }

        #endregion


        // ******************************** Main Menu

        #region Main Menu

        private void openMenuItem_Click(object sender, EventArgs e)
        {
            if (SelectMedia()) PlayMedia();
        }

        private void lastRecordingMenuItem_Click(object sender, EventArgs e)
        {
            UseLastRecording = lastRecordingMenuItem.Checked = !UseLastRecording;
        }

        private void countdownTimerMenuItem_Click(object sender, EventArgs e)
        {
            _countDown = countdownTimerMenuItem.Checked = !_countDown;
        }

        private void repeatMenuItem_Click(object sender, EventArgs e)
        {
            _voicePlayer1.Repeat = repeatMenuItem.Checked = !_voicePlayer1.Repeat;
        }

        private void hideMenuItem_Click(object sender, EventArgs e)
        {
            _baseForm.showPlayerMenuItem_Click(this, EventArgs.Empty);
        }

        private void closeMenuItem_Click(object sender, EventArgs e)
        {
            ClosePlayer();
            _baseForm.showPlayerMenuItem_Click(this, EventArgs.Empty);
        }

        #endregion

        // ******************************** Volume and Balance Menus

        #region Volume and Balance Menus

        private void muteMenuItem_Click(object sender, EventArgs e)
        {
            _voicePlayer1.AudioVolume = 0;
        }

        private void averageVolumeMenuItem_Click(object sender, EventArgs e)
        {
            _voicePlayer1.AudioVolume = 500;
        }

        private void maximumVolumeMenuItem_Click(object sender, EventArgs e)
        {
            _voicePlayer1.AudioVolume = 1000;
        }

        private void leftBalanceMenuItem_Click(object sender, EventArgs e)
        {
            _voicePlayer1.AudioBalance = 0;
        }

        private void centerBalanceMenuItem_Click(object sender, EventArgs e)
        {
            _voicePlayer1.AudioBalance = 500;
        }

        private void rightBalanceMenuItem_Click(object sender, EventArgs e)
        {
            _voicePlayer1.AudioBalance = 1000;
        }

        #endregion

        // ******************************** Play, Pause and Stop Buttons

        #region Play, Pause and Stop Buttons

        private void playButton_Click(object sender, EventArgs e)
        {
            PlayMedia();
        }

        private void pauseButton_Click(object sender, EventArgs e)
        {
            _voicePlayer1.Paused = !_voicePlayer1.Paused;
            if (_voicePlayer1.Paused) ButtonFlash.Add(pauseButton, pauseButton.ForeColor, Color.Black);
            else ButtonFlash.Remove(pauseButton);
        }

        private void stopButton_Click(object sender, EventArgs e)
        {
            _voicePlayer1.Stop();
        }

        #endregion

        // ******************************** Select Media OpenFileDialog

        #region Select Media OpenFileDialog

        private bool SelectMedia()
        {
            bool result = false;

            OpenFileDialog dlg = new OpenFileDialog
            {
                Title = "Open Voice Recording",
                Filter = "Voice Recordings (*.wav)|*.wav|All Files (*.*)|*.*",
                CheckFileExists = true
            };

            if (dlg.ShowDialog(this) == DialogResult.OK)
            {
                // TODO check for .ppl for playlist mode
                PlayFile = dlg.FileName;
                result = true;
            }
            dlg.Dispose();
            return result;
        }

        #endregion

        // ******************************** Error Message Dialog

        #region Error Message Dialog

        private void ShowErrorMessage()
        {
            // 346 = no display
            ErrorDialog errorDialog = _voicePlayer1.LastErrorCode == 346 ? new ErrorDialog(Form1.APPLICATION_NAME, "VOICE PLAYER\r\n\r\nThe selected mediafile contains video/images, but the Voice Player doesn't have a display.\r\n\r\n" + _voicePlayer1.LastErrorString, false) : new ErrorDialog(Form1.APPLICATION_NAME, "VOICE PLAYER\r\n\r\n" + _voicePlayer1.LastErrorString, 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

Permalink | Advertise | Privacy | Cookies | Terms of Use | Mobile
Web06 | 2.8.190612.1 | Last Updated 14 Jun 2019
Article Copyright 2010 by Peter Vegter
Everything else Copyright © CodeProject, 1999-2019
Layout: fixed | fluid