Click here to Skip to main content
13,830,043 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 PVS.AVPlayer;
using System;
using System.Drawing;
using System.Globalization;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
using System.Windows.Forms;

#endregion

namespace AVPlayerExample
{
    public partial class MainWindow
    {
        // ******************************** Preferences

        #region Preferences Fields

        [Serializable]
        public class Preferences
        {
            // Preferences Version
            public int              Version;

            // Window
            public bool             SaveWindow;
            public bool             Maximized;
            public Rectangle        Bounds;
            public bool             Fullscreen;
            public FullScreenMode   FullScreenMode;

            // DisplayMode
            public bool             SaveDisplayMode;
            public DisplayMode      DisplayMode;
            public Rectangle        VideoBounds;

            // Overlay
            public bool             SaveOverlay;
            public bool             AutoOverlay;
            public int              Overlay;
            public OverlayMode      OverlayMode;

            // Position Slider
            public bool             SaveSlider;
            public bool             SliderVisible;
            public bool             SliderProgress;
            public bool             SliderLiveUpdate;
            public int              SliderMouseWheel;

            // Repeat
            public bool             SaveRepeat;
            public int              Repeat; // 0 = off, 1 = one, 2 = all, 3 = shuffle

            // Speed
            public bool             SaveSpeed;
            public int              Speed;

            // Audio
            public bool             SaveAudio;
            public int              AudioVolume;
            public int              AudioBalance;

            // Folders
            public bool             SaveMediaFilesFolder;
            public string           MediaFilesFolder;
            public bool             SavePlaylistsFolder;
            public string           PlaylistsFolder;

            // Continue Playing Media
            public bool             ContinuePlay;
            public int              MediaToPlay;
            public double           Position;
            public double           StartPosition;
            public double           EndPosition;
            public bool             Paused;
            public int              RewindSecs;
            public bool             VideoPresent;

            // PlayList
            public bool             PlayListShowExtensions;
            public bool             PlayListChanged;
            public string           PlayListTitle;

            // Various
            public bool             AutoPlayStart;
            public bool             AutoPlayAdded;
            public bool             AutoPlayNext;
            public bool             OnErrorPlayNext; // non-persistent, same as AutoPlayNext but can for one time be changed in error dialog
            public bool             AutoHideCursor;
            public int              AutoHideCursorSeconds;
            public bool             OnErrorRemove;
            public bool             ShowErrorMessages;

            public bool             ClockShow;
            public bool             Clock24Hr;
            public Color            ClockColor;

            // Tooltips & Dialog MiniPlayers
            public bool             ShowTooltips;
            public bool             ShowMiniPlayers;

            // Output Level Meters
            public bool             MainLevelMeterShow;
            public Color            MainLevelMeterColor;
            public int              MainLevelMeterGain;
            public int              MainLevelMeterDecay;
            public Color            SoundLevelMeterColor;
            public Color            RecorderLevelMeterColor;

            // Slider Preview
            public bool             ShowSliderPreview;
            public int              SliderPreviewSize;
            public bool             SliderPreviewTimeOnly;

            public bool             ShowInfoLabels;
        }

        private const int           PREFERENCES_VERSION         = 9;
        private const int           DEFAULT_HIDE_CURSOR_SECONDS = 2;

        private string              _prefsFile                  = _appDataPath + PREFERENCES_NAME + ".inf"; // preferences file path
        internal static Preferences Prefs;

        #endregion

        private void LoadPreferences()
        {
            bool loadOk = false;
            if (Prefs == null) Prefs = new Preferences();

            if (File.Exists(_prefsFile))
            {
                //Use this for loading an xml-type file:
                //try
                //{
                //    using (StreamReader reader = new StreamReader(_prefsFile))
                //    {
                //        XmlSerializer xml = new XmlSerializer(typeof(Preferences));
                //        Prefs = (Preferences)xml.Deserialize(reader);
                //    }
                //    loadOk = true;
                //}
                //catch { /* ignore */ }

                // Use this for loading a binary-type file:
                try
                {
                    using (FileStream stream = File.Open(_prefsFile, FileMode.Open))
                    {
                        BinaryFormatter bin = new BinaryFormatter();
                        Prefs = (Preferences)bin.Deserialize(stream);
                    }
                    loadOk = true;
                }
                catch { /* ignore */ }
            }

            if (loadOk && Prefs.Version <= PREFERENCES_VERSION)
            {
                SetPreferences();
            }
            else SetDefaultPreferences();
        }

        private void SavePreferences()
        {
            GetPreferences(); // collect settings

            // Use this for saving an xml-type file:
            //try
            //{
            //    using (StreamWriter writer = new StreamWriter(_prefsFile))
            //    {
            //        XmlSerializer xml = new XmlSerializer(typeof(Preferences));
            //        xml.Serialize(writer, Prefs);
            //    }
            //}
            //catch { /* ignore */ }

            // Use this for saving a binary-type file:
            try
            {
                using (FileStream stream = File.Open(_prefsFile, FileMode.Create))
                {
                    BinaryFormatter bin = new BinaryFormatter();
                    bin.Serialize(stream, Prefs);
                }
            }
            catch { /* ignore */ }
        }


        private void SetPreferences()
        {
            // Versions handling
            if (Prefs.Version < 9)
            {
                Prefs.SliderMouseWheel = 0;
                if (Prefs.Version < 8)
                {
                    Prefs.ShowSliderPreview = true;
                    Prefs.SliderPreviewSize = 0;
                    Prefs.SliderPreviewTimeOnly = false;
                    Prefs.ShowInfoLabels = true;

                    if (Prefs.Version < 7)
                    {
                        // because of renaming pref items
                        Prefs.MainLevelMeterShow = true;
                        Prefs.MainLevelMeterColor = UIColors.MenuTextEnabledColor;
                        Prefs.MainLevelMeterGain = 1;
                        Prefs.MainLevelMeterDecay = 0;
                        Prefs.SoundLevelMeterColor = UIColors.MenuTextEnabledColor;
                        Prefs.RecorderLevelMeterColor = UIColors.MenuTextEnabledColor;

                        if (Prefs.Version < 6)
                        {
                            // you can change the peak level meters colors again (and it will be remembered),
                            // but it was intended to have the same color as the other (foreground) interface items
                            Prefs.MainLevelMeterColor = UIColors.MenuTextEnabledColor;
                            Prefs.SoundLevelMeterColor = UIColors.MenuTextEnabledColor;
                            Prefs.RecorderLevelMeterColor = UIColors.MenuTextEnabledColor;

                            Prefs.MainLevelMeterDecay = 0;

                            if (Prefs.Version < 5)
                            {
                                Prefs.MainLevelMeterShow = true;
                                Prefs.MainLevelMeterColor = UIColors.MenuTextEnabledColor;
                                Prefs.MainLevelMeterGain = 1;
                                Prefs.MainLevelMeterDecay = 0;
                                Prefs.SoundLevelMeterColor = UIColors.MenuTextEnabledColor;
                                Prefs.RecorderLevelMeterColor = UIColors.MenuTextEnabledColor;

                                if (Prefs.Version < 4)
                                {
                                    Prefs.OverlayMode = OverlayMode.Video;
                                    Prefs.AutoHideCursor = true;
                                    Prefs.AutoHideCursorSeconds = DEFAULT_HIDE_CURSOR_SECONDS;

                                    if (Prefs.Version < 3)
                                    {
                                        Prefs.AutoPlayAdded = true;
                                        Prefs.Clock24Hr = CultureInfo.CurrentUICulture.DateTimeFormat.ShortTimePattern.StartsWith("H");
                                        Prefs.ClockColor = UIColors.MenuTextEnabledColor;
                                    }
                                }
                            }
                        }
                    }
                }
            }

            // Window
            if (Prefs.SaveWindow || (Prefs.ContinuePlay && Prefs.MediaToPlay >= 0))
            {
                // check if bounds are on (fully) any current screen (if saved with multiple screens setup)
                Rectangle r = Prefs.Bounds;
                r.Inflate(-16, -16); // add a little (off-screen) margin
                for (int i = 0; i < Screen.AllScreens.Length; i++)
                {
                    if (Screen.AllScreens[i].Bounds.Contains(r))
                    {
                        StartPosition = FormStartPosition.Manual;
                        Bounds = Prefs.Bounds;
                        break;
                    }
                }
                // FullScreen and others are set in Form1_Shown eventhandler
            }

            // Audio
            if (Prefs.SaveAudio || (Prefs.ContinuePlay && Prefs.MediaToPlay >= 0))
            {
                Player1.Audio.Volume = Prefs.AudioVolume;
                Player1.Audio.Balance = Prefs.AudioBalance;
            }

            // Position Slider
            if (Prefs.SaveSlider || (Prefs.ContinuePlay && Prefs.MediaToPlay >= 0))
            {
                SetSliderVisibility(!Prefs.SliderVisible);
                SetSliderProgress(Prefs.SliderProgress);
                SetSliderLiveUpdate(Prefs.SliderLiveUpdate);
                SetSliderPreview(Prefs.ShowSliderPreview);
                SetSliderMouseWheel(Prefs.SliderMouseWheel, false);
            }
            else
            {
                Prefs.ShowSliderPreview = false;
                Prefs.ShowInfoLabels = true;
            }
            // the SetInfoLabels(Prefs.ShowInfoLabels) is called from InitInterface
            // for the reason described there (= after setting the player sliders).

            // DisplayMode
            if (Prefs.SaveDisplayMode || (Prefs.ContinuePlay && Prefs.MediaToPlay >= 0))
            {
                if (Prefs.DisplayMode == DisplayMode.Manual)
                {
                    if(StartPosition == FormStartPosition.Manual) Player1.Video.Bounds = Prefs.VideoBounds;
                    else Player1.Display.Mode = DisplayMode.ZoomCenter;
                }
                else Player1.Display.Mode = Prefs.DisplayMode;
            }

            // Speed
            if (Prefs.SaveSpeed || (Prefs.ContinuePlay && Prefs.MediaToPlay >= 0))
            {
                Player1.Speed = Prefs.Speed;
            }

            Prefs.ShowTooltips = false;

            // Overlay - set in Form1_Shown
            // Repeat - set in Form1_Shown
            // FullScreen and others - set in Form1_Shown
        }

        private void SetDefaultPreferences()
        {
            Prefs.Version                   = PREFERENCES_VERSION;

            Prefs.SaveWindow                = false;
            Prefs.Maximized                 = false;
            Prefs.Bounds                    = Bounds;
            Prefs.Fullscreen                = false;
            Prefs.FullScreenMode            = FullScreenMode.Display;

            Prefs.SaveAudio                 = false;
            Prefs.AudioVolume               = 1000;
            Prefs.AudioBalance              = 500;

            Prefs.SaveDisplayMode           = false;
            Prefs.DisplayMode               = DisplayMode.ZoomCenter;
            Prefs.VideoBounds               = Rectangle.Empty;

            Prefs.SaveSlider                = false;
            Prefs.SliderVisible             = true;
            Prefs.SliderProgress            = true;
            Prefs.SliderLiveUpdate          = true;
            Prefs.SliderMouseWheel          = 0;

            Prefs.SaveRepeat                = false;
            Prefs.Repeat                    = 0;

            Prefs.SaveOverlay               = false;
            Prefs.AutoOverlay               = true;
            Prefs.Overlay                   = -1;
            Prefs.OverlayMode               = OverlayMode.Video;

            Prefs.SaveSpeed                 = false;
            Prefs.Speed                     = 1000;

            Prefs.SaveMediaFilesFolder      = false;
            Prefs.MediaFilesFolder          = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
            Prefs.SavePlaylistsFolder       = false;
            Prefs.PlaylistsFolder           = Prefs.MediaFilesFolder;

            Prefs.ContinuePlay              = false;
            Prefs.MediaToPlay               = -1;
            Prefs.Position                  = 0;
            Prefs.StartPosition             = 0;
            Prefs.EndPosition               = 0;
            Prefs.Paused                    = false;
            Prefs.RewindSecs                = 10;
            Prefs.VideoPresent              = false;

            Prefs.PlayListShowExtensions    = true;
            Prefs.PlayListChanged           = false;
            Prefs.PlayListTitle             = "Untitled";

            Prefs.AutoPlayStart             = false;
            Prefs.AutoPlayAdded             = true;
            Prefs.AutoPlayNext              = true;
            //Prefs.OnErrorPlayNext = true;
            Prefs.AutoHideCursor            = true;
            Prefs.AutoHideCursorSeconds     = DEFAULT_HIDE_CURSOR_SECONDS;
            Prefs.OnErrorRemove             = false;
            Prefs.ShowErrorMessages         = true;

            Prefs.ClockShow                 = false;
            Prefs.Clock24Hr                 = CultureInfo.CurrentUICulture.DateTimeFormat.ShortTimePattern.StartsWith("H");
            Prefs.ClockColor                = UIColors.MenuTextEnabledColor;

            Prefs.ShowTooltips              = false;
            Prefs.ShowMiniPlayers           = false;

            Prefs.MainLevelMeterShow        = true;
            Prefs.MainLevelMeterColor       = UIColors.MenuTextEnabledColor;
            Prefs.MainLevelMeterGain        = 1;
            Prefs.MainLevelMeterDecay       = 0;
            Prefs.SoundLevelMeterColor      = UIColors.MenuTextEnabledColor;
            Prefs.RecorderLevelMeterColor   = UIColors.MenuTextEnabledColor;

            Prefs.ShowSliderPreview         = false;
            Prefs.SliderPreviewSize         = 0;
            Prefs.SliderPreviewTimeOnly     = false;

            Prefs.ShowInfoLabels            = true;
        }


        private void GetPreferences()
        {
            // Version
            Prefs.Version           = PREFERENCES_VERSION;

            // Window
            Prefs.Fullscreen        = Player1.FullScreen;
            Prefs.FullScreenMode    = Player1.FullScreenMode;
            Prefs.Maximized         = WindowState == FormWindowState.Maximized;
            Prefs.Bounds            = Player1.Display.RestoreBounds;

            // Audio
            Prefs.AudioVolume       = Player1.Audio.Volume;
            Prefs.AudioBalance      = Player1.Audio.Balance;

            // DisplayMode
            Prefs.DisplayMode       = Player1.Display.Mode;
            Prefs.VideoBounds       = Player1.Video.Bounds;

            // Repeat
            if (RepeatOne)          Prefs.Repeat = 1;
            else if (RepeatAll)     Prefs.Repeat = 2;
            else if (RepeatShuffle) Prefs.Repeat = 3;
            else                    Prefs.Repeat = 0;

            // Position Slider
            Prefs.SliderVisible     = sliderAlwaysVisibleMenuItem.Checked;
            Prefs.SliderProgress    = sliderShowsProgressMenuItem.Checked;
            Prefs.SliderLiveUpdate  = sliderSeekLiveUpdateMenuItem.Checked;
            // Prefs.SliderPreview etc. used and set by application.
            Prefs.SliderMouseWheel  = Player1.Sliders.PositionMouseWheel;

            // Display Overlay (stores the selected menu item - there are other ways to store the overlay)
            Prefs.Overlay           = -1;
            if (Player1.Overlay.Window != null)
            {
                for (int i = 3; i < displayOverlayMenu.Items.Count; i++) // find selected overlay menu item
                {
                    if (displayOverlayMenu.Items[i].GetType() == typeof(ToolStripMenuItem) && ((ToolStripMenuItem)displayOverlayMenu.Items[i]).Checked)
                    {
                        Prefs.Overlay = i;
                        break;
                    }
                }
            }
            Prefs.OverlayMode       = Player1.Overlay.Mode;

            // Speed
            Prefs.Speed             = Player1.Speed;

            // Continue Play
            if (Prefs.ContinuePlay && Player1.Playing)
            {
                // save current media (playlist entry)
                Prefs.MediaToPlay   = _mediaToPlay - 1;
                Prefs.Position      = Player1.Position.FromBegin.TotalMilliseconds;
                Prefs.StartPosition = Player1.Media.StartPosition.TotalMilliseconds;
                Prefs.EndPosition   = Player1.Media.StopPosition.TotalMilliseconds;
                Prefs.Paused        = Player1.Paused;
                Prefs.VideoPresent  = Player1.Video.Present;
            }
            else
            {
                Prefs.MediaToPlay   = -1;
            }

            // other options set elsewhere (Form1_Shown and some Prefs are used as 'normal' fields)
        }
    }
}

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