Click here to Skip to main content
13,349,539 members (51,284 online)
Click here to Skip to main content

Stats

556.4K views
25.9K downloads
255 bookmarked
Posted 14 Sep 2010

PVS.AVPlayer - MCI Audio and Video Library

, 7 Dec 2017
Windows Media Control Interface (MCI) library with many added features
PVS.AVPlayer
PVS.AVPlayer .NET 2.0
PVS.AVPlayer .NET 3.0
PVS.AVPlayer .NET 3.5
PVS.AVPlayer .NET 4.0
PVS.AVPlayer .NET 4.5
PVS.AVPlayer .NET 4.5.1
PVS.AVPlayer .NET 4.5.2
PVS.AVPlayer .NET 4.6
PVS.AVPlayer.dll
PVS.AVPlayer.dll
PVS.AVPlayer.dll
PVS.AVPlayer.dll
PVS.AVPlayer.dll
PVS.AVPlayer.dll
PVS.AVPlayer.dll
PVS.AVPlayer .NET 4.6.1
PVS.AVPlayer.dll
PVS.AVPlayer .NET 4.6.2
PVS.AVPlayer.dll
PVS.AVPlayer.dll
PVS.AVPlayer .NET 4.7.1
PVS.AVPlayer.dll
PVS.AVPlayer .NET 4.7
PVS.AVPlayer.dll
PVS.AVPlayer All Source Code
AVPlayerExample
AVPlayerExample
AVPlayerExample.csproj.user
bin
Debug
Release
Dialogs
Display Overlays
obj
Debug
Release
x86
Debug
Release
Properties
Resources
Crystal Italic1.ttf
WingDings3a.ttf
Voice Recorder
FolderView
FolderView
bin
Debug
Release
FolderView.csproj.user
obj
Release
x86
Debug
Release
Properties
Resources
Crystal Italic1.ttf
PVS.AVPlayer
AVPlayerExample.csproj.user
PVS.AVPlayer.dll
Custom Items
Native Methods
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
Custom Items
FolderView.csproj.user
Crystal Italic1.ttf
media7a.ico
media7b.ico
Media8a.ico
Media8b.ico
Subtitles Overlay
Various
How To (C#)
PVSAVPlayerHowTo
bin
PVS.AVPlayer.dll
Release
Properties
How To (VB.NET)
PVSAVPlayerHowToVB
bin
PVS.AVPlayer.dll
Release
Form1.Designer.v_b
Form1.v_b
My Project
app.manifest
Application.Designer.v_b
Application.myapp
AssemblyInfo.v_b
Resources.Designer.v_b
Settings.Designer.v_b
Overlay.Designer.v_b
Overlay.v_b
PVSAVPlayerHowTo.vbproj.user
PVS.AVPlayer Examples
AVPlayerExample.ex_
FolderView.ex_
AVPlayerExample.ex_
FolderView.ex_
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;
        private bool                _userMuted;
        private bool                _disposed;

        private string              _theMovie;
        private TimeSpan            _position;

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

        public bool                 _lastError;

        #endregion

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

        #region Main

        public PlayerWindow(MainWindow baseWindow, string theMovie, TimeSpan position)
        {
            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;
            _position   = position;
            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;
        }

        private void PlayerWindow_Shown(object sender, EventArgs e)
        {
            _player.Events.MediaPositionChanged += Player_MediaPositionChanged;
            _player.Play(_theMovie);
            _lastError = _player.LastError;

            if (!_lastError)
            {
                _player.Position.FromStart = _position;
                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
        private void PlayerWindow_Activated(object sender, EventArgs e)
        {
            if (!_userMuted) _player.Audio.Enabled = true;
        }

        // Mute audio when form deactivated
        private void PlayerWindow_Deactivate(object sender, EventArgs e)
        {
            if (_player != null)
            {
                Application.DoEvents();
                if (_player.Overlay.Window != null)
                {
                    if (_player.Overlay.Window.Handle != SafeNativeMethods.GetForegroundWindow()) _player.Audio.Enabled = false;
                }
                else _player.Audio.Enabled = false;
            }
        }

        // Enable audio when overlay activated
        private void SubtitlesOverlay_Activated(object sender, EventArgs e)
        {
            if (!_userMuted) _player.Audio.Enabled = true;
        }

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

        // 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;
                }
            }
        }

        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;
        }

        #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 StretchAllMenuItem_Click(object sender, EventArgs e)
        {
            _baseWindow.StretchAllWindows();
        }

        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";
        }

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

        // DisplayMode ZoomAndCenter
        private void ZoomAndCenterMenuItem_Click(object sender, EventArgs e)
        {
            ZoomMenuFromMain();
        }
        internal void ZoomMenuFromMain()
        {
            _player.Display.Mode = DisplayMode.ZoomAndCenter;
            zoomAndCenterMenuItem.Checked = true;
            stretchMenuItem.Checked = false;
        }

        // 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 | Terms of Use | Mobile
Web01 | 2.8.180111.1 | Last Updated 7 Dec 2017
Article Copyright 2010 by Peter Vegter
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid