Click here to Skip to main content
13,663,506 members
Click here to Skip to main content

Stats

680K views
29.6K downloads
279 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 PVS.AVPlayer;
using System;
using System.Drawing;
using System.Windows.Forms;

#endregion

namespace FolderView
{
    public partial class PlayerWindow : Form
    {
        /*
            A simple Player Window.
        */


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

        #region Fields

        MainWindow                  _baseWindow;

        internal Player             _player;
        internal bool               _userMuted;
        internal bool               _audioHold;
        private bool                _disposed;

        private string              _theMovie;
        private TimeSpan            _startPosition;

        private SubtitlesOverlay    _subtitlesOverlay;
        private bool                _subtitlesOn;
        private bool                _subtitlesMenuOn;

        public bool                 _lastError;

        #endregion

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

        #region Main

        public PlayerWindow(MainWindow baseWindow, string theMovie, TimeSpan startPosition)
        {
            InitializeComponent();
            Icon = Properties.Resources.Media8a;

            _baseWindow = baseWindow;

            ((ToolStripDropDownMenu)allWindowsMenuItem.DropDown).ShowImageMargin = false;
            ((ToolStripDropDownMenu)displayModeAllMenuItem.DropDown).ShowImageMargin = false;

            positionLabel1.Font                         = Globals.CrystalFont16;
            positionLabel1.UseCompatibleTextRendering   = true;
            positionLabel2.Font                         = Globals.CrystalFont16;
            positionLabel2.UseCompatibleTextRendering   = true;

            _theMovie       = theMovie;
            _startPosition  = startPosition;
            Text            = theMovie;

            _player = new Player();

            _player.Display.Window              = panel1;
            _player.Display.DragEnabled         = true;

            _player.Repeat                      = true;

            _player.Sliders.Position            = customSlider21;
            _player.Sliders.PositionMode        = PositionSliderMode.Track;

            _player.TaskbarProgress.Add(this);
            _player.TaskbarProgress.Mode        = TaskbarProgressMode.Track;

            _player.Events.MediaPeakLevelChanged += Player_MediaPeakLevelChanged;

            _player.CursorHide.Add(this);
        }

        private void PlayerWindow_Shown(object sender, EventArgs e)
        {
            if (_baseWindow._audioHold) _player.Audio.Enabled = false;

            _player.Events.MediaPositionChanged += Player_MediaPositionChanged;
            _player.Play(_theMovie, _startPosition, TimeSpan.Zero);
            _lastError = _player.LastError;

            if (!_lastError)
            {
                // reset the (repeat) startposition to the beginning of file
                // (if not played from the beginning of the file)
                _player.Media.StartPosition = TimeSpan.Zero;

                this.KeyPreview = true;
                this.KeyDown += PlayerWindow_KeyDown;

                string path = Globals.Subtitles.Find(_player.Media.GetName(MediaName.FileNameWithoutExtension) + ".srt", _player.Media.GetName(MediaName.DirectoryName), 3);
                if (!string.IsNullOrEmpty(path))
                {
                    _subtitlesOverlay = new SubtitlesOverlay(_player);
                    _player.Overlay.Hold = true;

                    showSubtitlesMenuItem.Enabled = showSubtitleOptionsMenuItem.Enabled = true;
                    showSubtitlesMenuItem.PerformClick();
                }
            }
        }

        // Enable audio when form activated and mute other PlayerWindows
        private void PlayerWindow_Activated(object sender, EventArgs e)
        {
            if (!_baseWindow._audioHold) PlayerWindow_SetAudio();
        }

        // Enable audio when overlay activated
        private void SubtitlesOverlay_Activated(object sender, EventArgs e)
        {
            if (!_baseWindow._audioHold) PlayerWindow_SetAudio();
        }

        // Mute audio when overlay deactivated (and other form activated)
        private void SubtitlesOverlay_Deactivate(object sender, EventArgs e)
        {
            Application.DoEvents();
            if (this.Handle != SafeNativeMethods.GetForegroundWindow())
            {
                if (!_baseWindow._audioHold) _player.Audio.Enabled = false;
            }
        }

        private void PlayerWindow_SetAudio()
        {
            foreach (Form form in Application.OpenForms)
            {
                Application.DoEvents();
                if (form != null && (form.GetType() == typeof(PlayerWindow)) && form.Handle != this.Handle)
                {
                    if (((PlayerWindow)form)._player != null)
                    {
                        ((PlayerWindow)form)._player.Audio.Enabled = false;
                    }
                }
            }
            if (!_userMuted) _player.Audio.Enabled = true;
        }

        // Esc key cancels fullscreen mode
        private void PlayerWindow_KeyDown(object sender, KeyEventArgs e)
        {
            e.Handled = true;
            if (e.KeyCode == Keys.Q && e.Control)
            {
                Close();
            }
            else
            {
                switch (e.KeyCode)
                {
                    case Keys.Escape:
                        if (_player.FullScreen) fullScreenMenuItem.PerformClick();
                        else Close();
                        break;

                    // FullScreen
                    case Keys.F:
                    case Keys.F11:
                        fullScreenMenuItem.PerformClick();
                        break;

                    // Pause
                    case Keys.P:
                    case Keys.Space:
                        pauseMenuItem.PerformClick();
                        break;

                    // Mute
                    case Keys.M:
                        muteMenuItem.PerformClick();
                        break;

                    // Subtitles
                    case Keys.S:
                        showSubtitlesMenuItem.PerformClick();
                        break;

                    // Properties
                    case Keys.I:
                        propertiesMenuItem.PerformClick();
                        break;

                    // Close
                    case Keys.C:
                        Close();
                        break;

                    default:
                        e.Handled = false;
                        break;
                }
            }
        }

        private void PlayerWindow_FormClosed(object sender, FormClosedEventArgs e)
        {
            if (_audioHold)
            {
                _audioHold = false;
                _baseWindow._audioHold = false;
                _baseWindow._audioHoldWindow = null;
            }

            if (e.CloseReason == CloseReason.UserClosing)
            {
                foreach (Form form in Application.OpenForms)
                {
                    Application.DoEvents();
                    if (form != null && (form.GetType() == typeof(PlayerWindow)) && form.Handle != this.Handle)
                    {
                        if (((PlayerWindow)form)._player != null)
                        {
                            form.Activate();
                        }
                    }
                }
            }
        }

        private void Player_MediaPeakLevelChanged(object sender, PeakLevelEventArgs e)
        {
            // update shared peak meters on main form - in combination with window (de-)activation handling
            // no need for any checking, disabling etc., the player takes care of everything

            // replaced original code to combine with the players on the main form with:
            _baseWindow.PaintPeakMeters(e.ChannelsValues[0], e.ChannelsValues[1]);
            // original code moved to PaintPeakMeters method
        }

        protected override void Dispose(bool disposing)
        {
            if (!_disposed)
            {
                if (disposing)
                {
                    if (_subtitlesOverlay != null)
                    {
                        if (_subtitlesOn)
                        {
                            _player.Overlay.Window.Activated -= SubtitlesOverlay_Activated;
                            _player.Overlay.Window.Deactivate -= SubtitlesOverlay_Deactivate;
                        }
                        _player.Overlay.Window = null;

                        _subtitlesOverlay.Dispose();
                        _subtitlesOverlay = null;
                    }
                    //_player.Events.MediaPositionChanged -= myPlayer1_MediaPositionChanged;
                    _player.Events.MediaPositionChanged -= Player_MediaPositionChanged;
                    _player.Dispose(); _player = null;

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

        #endregion

        // ******************************** Player Position Counters / InfoLabel Scroll Eventhandlers

        #region Player Position Counters Eventhandler

        // The playback position of a mediafile has changed - update position info labels
        private void Player_MediaPositionChanged(object sender, PositionEventArgs e)
        {
            positionLabel1.Text = e.FromStart.ToString().Substring(0, 8); // hh:mm:ss
            positionLabel2.Text = e.ToEnd.ToString().Substring(0, 8);
        }

        // Show infolabel
        private void CustomSlider21_Scroll(object sender, EventArgs e)
        {
            if (ModifierKeys == Keys.Shift) // don't show with shift key pressed
            {
                _baseWindow.PlayerWindowLabel.Hide(true);
            }
            else
            {
                // Get the position slider's x-coordinate of the current position (= thumb location)
                // (AlignmentOffset has been set to 0, -3)
                Point infoLabelLocation = SliderValue.ToPoint(customSlider21, customSlider21.Value);

                // Show the infolabel
                TimeSpan time = _player.Position.FromStart;
                //_baseWindow.PlayerWindowLabel.Show(
                //    string.Format("{0:00;00}:{1:00;00}:{2:00;00}.{3:000;000}", time.Hours, time.Minutes, time.Seconds, time.Milliseconds),
                //    customSlider21, infoLabelLocation);
                _baseWindow.PlayerWindowLabel.Show(
                    string.Format("{0:00;00}:{1:00;00}:{2:00;00}", time.Hours, time.Minutes, time.Seconds),
                    customSlider21, infoLabelLocation);
            }
        }

        #endregion

        // ******************************** Contextmenu Handler

        #region Contextmenu Handler

        private void PlayerWindowMenu_Opening(object sender, System.ComponentModel.CancelEventArgs e)
        {
            int count = 0;
            foreach (Form window in Application.OpenForms)
            {
                if (window.GetType() == typeof(PlayerWindow))
                {
                    if (++count > 1) break;
                }
            }
            allWindowsMenuItem.Enabled = count > 1;

            if (_audioHold) audioHoldMenuItem.Text = "Audio Release";
            else audioHoldMenuItem.Text = "Audio Hold";

            audioReleaseAllMenuItem.Enabled = _baseWindow._audioHold;
        }


        #region All Windows

        private void PauseAllMenuItem_Click(object sender, EventArgs e)
        {
            _baseWindow.PauseAllWindows();
        }

        private void ResumeAllMenuItem_Click(object sender, EventArgs e)
        {
            _baseWindow.ResumeAllWindows();
        }

        private void MuteAllMenuItem_Click(object sender, EventArgs e)
        {
            _baseWindow.MuteAllWindows();
        }

        private void MuteOffAllMenuItem_Click(object sender, EventArgs e)
        {
            _baseWindow.MuteOffAllWindows();
        }

        private void AudioReleaseAllMenuItem_Click(object sender, EventArgs e)
        {
            if (_audioHold)
            {
                audioHoldMenuItem.Text = "Audio Hold";
                _audioHold = false;
            }
            else if (_baseWindow._audioHold)
            {
                _baseWindow._audioHoldWindow._audioHold = false;
                _baseWindow._audioHoldWindow._player.Audio.Enabled = false;

                if (!_userMuted) _player.Audio.Enabled = true;
            }

            _baseWindow._audioHold = false;
            _baseWindow._audioHoldWindow = null;
        }

        private void StretchAllMenuItem_Click(object sender, EventArgs e)
        {
            _baseWindow.StretchAllWindows();
        }

        private void CoverAllMenuItem_Click(object sender, EventArgs e)
        {
            _baseWindow.CoverAllWindows();
        }

        private void ZoomAllMenuItem_Click(object sender, EventArgs e)
        {
            _baseWindow.ZoomAllWindows();
        }

        private void FullScreenOffAllMenuItem_Click(object sender, EventArgs e)
        {
            _baseWindow.FullScreenOffAllWindows();
        }

        private void HideSubtitlesAllMenuItem_Click(object sender, EventArgs e)
        {
            _baseWindow.HideSubtitlesAllWindows();
        }

        private void ShowSubtitlesAllMenuItem_Click(object sender, EventArgs e)
        {
            _baseWindow.ShowSubtitlesAllWindows();
        }

        private void CloseAllMenuItem_Click(object sender, EventArgs e)
        {
            _baseWindow.CloseAllWindows();
        }

        #endregion

        // Pause / Resume
        private void PauseMenuItem_Click(object sender, EventArgs e)
        {
            if (_player.Paused)
            {
                _player.Paused = false;
                pauseMenuItem.Text = "Pause";
            }
            else
            {
                _player.Paused = true;
                pauseMenuItem.Text = "Resume";
            }
        }
        internal void PauseMenuFromMain()
        {
            if (!_player.Paused)
            {
                _player.Paused = true;
                pauseMenuItem.Text = "Resume";
            }
        }
        internal void ResumeMenuFromMain()
        {
            if (_player.Paused)
            {
                _player.Paused = false;
                pauseMenuItem.Text = "Pause";
            }
        }

        // Mute On / Off
        private void MuteMenuItem_Click(object sender, EventArgs e)
        {
            if (_player.Audio.Enabled)
            {
                _player.Audio.Enabled = false;
                _userMuted = true;
                muteMenuItem.Text = "Mute Off";
            }
            else
            {
                _player.Audio.Enabled = true;
                _userMuted = false;
                muteMenuItem.Text = "Mute";
            }
        }
        internal void MuteMenuFromMain()
        {
            _player.Audio.Enabled = false;
            _userMuted = true;
            muteMenuItem.Text = "Mute Off";
        }
        internal void MuteOffMenuFromMain()
        {
            if (this == ActiveForm) _player.Audio.Enabled = true;
            _userMuted = false;
            muteMenuItem.Text = "Mute";
        }

        // Audio Hold / Release
        private void AudioHoldMenuItem_Click(object sender, EventArgs e)
        {
            if (_audioHold)
            {
                _audioHold = false;
                audioHoldMenuItem.Text = "Audio Hold";

                _baseWindow._audioHold = false;
                _baseWindow._audioHoldWindow = null;
            }
            else
            {
                _audioHold = true;
                audioHoldMenuItem.Text = "Audio Release";

                if (_baseWindow._audioHold)
                {
                    _baseWindow._audioHoldWindow._audioHold = false;
                    _baseWindow._audioHoldWindow._player.Audio.Enabled = false;
                }
                _baseWindow._audioHold = true;
                _baseWindow._audioHoldWindow = this;

                if (!_userMuted) _player.Audio.Enabled = true;
            }
        }

        // DisplayMode Stretch
        private void StretchMenuItem_Click(object sender, EventArgs e)
        {
            StretchMenuFromMain();
        }
        internal void StretchMenuFromMain()
        {
            _player.Display.Mode = DisplayMode.Stretch;
            stretchMenuItem.Checked = true;
            coverMenuItem.Checked = false;
            zoomMenuItem.Checked = false;
        }

        // DisplayMode Cover
        private void CoverMenuItem_Click(object sender, EventArgs e)
        {
            CoverMenuFromMain();
        }
        internal void CoverMenuFromMain()
        {
            _player.Display.Mode = DisplayMode.CoverCenter;
            stretchMenuItem.Checked = false;
            coverMenuItem.Checked = true;
            zoomMenuItem.Checked = false;
        }

        // DisplayMode Zoom
        private void ZoomMenuItem_Click(object sender, EventArgs e)
        {
            ZoomMenuFromMain();
        }
        internal void ZoomMenuFromMain()
        {
            _player.Display.Mode = DisplayMode.ZoomCenter;
            stretchMenuItem.Checked = false;
            coverMenuItem.Checked = false;
            zoomMenuItem.Checked = true;
        }

        // FullScreen On / Off
        private void FullScreenMenuItem_Click(object sender, EventArgs e)
        {
            if (_player.FullScreen)
            {
                _player.FullScreen = false;
                fullScreenMenuItem.Text = "FullScreen";
            }
            else
            {
                _player.FullScreen = true;
                fullScreenMenuItem.Text = "FullScreen Off";
            }
        }
        internal void FullScreenOffFromMain()
        {
            if (_player.FullScreen)
            {
                _player.FullScreen = false;
                fullScreenMenuItem.Text = "FullScreen";
            }
        }

        // Subtitles
        private void ShowSubtitlesMenuItem_Click(object sender, EventArgs e)
        {
            _subtitlesOn = !_subtitlesOn;
            if (_subtitlesOn)
            {
                if (_subtitlesOverlay != null)
                {
                    _player.Overlay.Window = _subtitlesOverlay;
                    _player.Overlay.Window.Activated += SubtitlesOverlay_Activated;
                    _player.Overlay.Window.Deactivate += SubtitlesOverlay_Deactivate;

                    showSubtitlesMenuItem.Text = "Hide Subtitles";
                    showSubtitleOptionsMenuItem.Enabled = true;
                }
                else
                {
                    _subtitlesOn = false;
                }
            }
            else
            {
                _player.Overlay.Window.Activated -= SubtitlesOverlay_Activated;
                _player.Overlay.Window.Deactivate -= SubtitlesOverlay_Deactivate;
                _player.Overlay.Window = null;

                showSubtitleOptionsMenuItem.Enabled = false;
                showSubtitlesMenuItem.Text = "Show Subtitles";
            }
        }
        internal void SubtitlesOffFromMain()
        {
            if (_subtitlesOn)
            {
                _player.Overlay.Window.Activated -= SubtitlesOverlay_Activated;
                _player.Overlay.Window.Deactivate -= SubtitlesOverlay_Deactivate;
                _player.Overlay.Window = null;

                showSubtitleOptionsMenuItem.Enabled = false;
                showSubtitlesMenuItem.Text = "Show Subtitles";

                _subtitlesOn = false;
            }

            if (_subtitlesMenuOn)
            {
                showSubtitleOptionsMenuItem.Text = "Show Subtitles Menu";
                _subtitlesOverlay.MenuEnabled = _player.Overlay.CanFocus = false;

                _subtitlesMenuOn = false;
            }
        }
        internal void SubtitlesOnFromMain()
        {
            if (!_subtitlesOn)
            {
                if (_subtitlesOverlay != null)
                {
                    _player.Overlay.Window = _subtitlesOverlay;
                    _player.Overlay.Window.Activated += SubtitlesOverlay_Activated;
                    _player.Overlay.Window.Deactivate += SubtitlesOverlay_Deactivate;

                    showSubtitlesMenuItem.Text = "Hide Subtitles";
                    showSubtitleOptionsMenuItem.Enabled = true;

                    _subtitlesOn = true;
                }
            }
        }

        // Subtitles Menu
        private void ShowSubtitleOptionsMenuItem_Click(object sender, EventArgs e)
        {
            _subtitlesMenuOn = !_subtitlesMenuOn;
            if (_subtitlesMenuOn)
            {
                showSubtitleOptionsMenuItem.Text = "Hide Subtitles Menu";
                _subtitlesOverlay.MenuEnabled = _player.Overlay.CanFocus = true;
            }
            else
            {
                showSubtitleOptionsMenuItem.Text = "Show Subtitles Menu";
                _subtitlesOverlay.MenuEnabled = _player.Overlay.CanFocus = false;
            }
        }

        // Properties
        private void PropertiesMenuItem_Click(object sender, EventArgs e)
        {
            try
            {
                SafeNativeMethods.SHELLEXECUTEINFO info = new SafeNativeMethods.SHELLEXECUTEINFO();
                info.cbSize = System.Runtime.InteropServices.Marshal.SizeOf(info);
                info.lpVerb = "properties";
                info.lpParameters = "details";
                info.lpFile = _player.Media.GetName(MediaName.FullPath);
                info.nShow = SafeNativeMethods.SW_SHOW;
                info.fMask = SafeNativeMethods.SEE_MASK_INVOKEIDLIST;
                SafeNativeMethods.ShellExecuteEx(ref info);
            }
            catch { /* ignore */ }
        }

        // Close
        private void CloseMenuItem_Click(object sender, EventArgs e)
        {
            Close();
        }

        #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
Web01-2016 | 2.8.180810.1 | Last Updated 7 Aug 2018
Article Copyright 2010 by Peter Vegter
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid