Click here to Skip to main content
13,768,211 members
Click here to Skip to main content

Stats

715.7K views
29.8K 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 "Imports"

Imports System.Text
Imports PVS.AVPlayer

#End Region

Public Class Form1

    ' PVS.AVPlayer 0.91 Example Application - How To...

    ' This small sample application shows how to use some of the more 'special' options
    ' of the player part of the PVS.AVPlayer library:

    '  1. add a display overlay
    '  2. display overlay always visible - from application start
    '  3. add one or more display clones
    '  4. add a position slider controlled by the player
    '  5. get continuous playback position information
    '  6. add audio sliders controlled by the player
    '  7. get changed audio value information
    '  8. get continuous playback audio output level information
    '  9. get mouse events on display, movie or overlay
    ' 10. get media finished playing information
    ' 11. get media subtitles
    ' 12. get media tag information (MP3 (ID3) and WMA (ASF) Tags)
    ' 13. play (or repeat) only a part of a media file
    ' 14. add a taskbar progress indicator
    ' 15. drag a form by dragging a player's display window
    ' 16. add an info label to the player's position slider
    ' 17. change the shape of the player's display window
    ' 18. using media signals
    ' 19. automatically hide the mouse cursor during media playback

    ' These options are initialized in the constructor (method Main: New()) of this class.

    ' Many options in de PVS.AVPlayer library are available even when no media is playing, they
    ' are 'Player settings' not 'Media settings', e.g.: Display settings (e.g. fullscreen),
    ' Audio settings (volume/balance), Start/EndPosition, Pause/Resume and others.

    ' If you have questions about using the PVS.AVPlayer library or this sample application, do not hesitate
    ' to ask a question in the PVS.AVPlayer article's comments on CodeProject:
    ' https://www.codeproject.com/Articles/109714/PVS-AVPlayer-MCI-Audio-And-Video-Library

    ' Peter Vegter
    ' August 2018, The Netherlands


    ' **** Class Fields ***************************************************************************

#Region "Class Fields"

    Private myPlayer As Player
    Private myOverlay As Overlay                ' in file Overlay.vb

    Private shapeStatus As Integer              ' shapes - 0:none, 1:oval, 2:none, 3:rounded, 4:none, 5:star

    Private myInfoLabel As InfoLabel
    Private myInfoLabelText As StringBuilder = New StringBuilder(64)

    ' used with drawing audio output levels
    ' levelUnit = size of 1 unit (of 32767) / 140 = width of Panel4 and Panel5
    Private levelUnit As Double = 140 ' / 32767.0 - changed in PVS.AVPlayer version 0.91
    Private leftLevel As Integer
    Private rightLevel As Integer

    Private myTagInfo As TagInfo                ' media (audio mp3/wma/asf) tag info

    Private myOpenFileDlg As OpenFileDialog     ' used with selection of media to play
    Private Const OPENMEDIA_FILTER As String =
            " Video Files (*.*)|*.asf;*.avi;*.bik;*.divx;*.dvx;*.f4v;*.flv;*.h264;*.hdmov;*.m2ts;*.mkv;*.mod;*.mov;*.mp2;*.mp4;*.mpeg;*.mpeg4;*.mpg;*.mpv;*.ogg;*.ogm;*.ogv;*.qt;*.rm;*.rms;*.rmvb;*.swf;*.ts;*.vfw;*.vob;*.webm;*.wmv;*.xvid|" +
            " Audio Files (*.*)|*.3gp;*.aa;*.aac;*.aiff;*.amr;*.ape;*.flac;*.m4a;*.mid;*.midi;*.mka;*.mp3;*.mpc;*.ogg;*.opus;*.ra;*.rm;*.wav;*.webm;*.wma;*.wv|" +
            " Image Files (*.*)|*.bmp;*.gif;*.img;*.jpeg;*.jpg;*.png;*.tga;*.tiff|" +
            " All Files|*.*"

    Private isInitializing As Boolean
    Private wasDisposed As Boolean
#End Region


    ' **** Main ***********************************************************************************

#Region "Main"

    Public Sub New()

        isInitializing = True
        InitializeComponent()               ' this call is required by the designer
        isInitializing = False

        myPlayer = New Player()             ' create a player
        myPlayer.Display.Window = Panel1    ' and set its display to Panel1
        myPlayer.Repeat = True              ' repeat media playback when finished

        myPlayer.SleepDisabled = True       ' prevent the computer from entering sleep mode

        ' create a file selector
        myOpenFileDlg = New OpenFileDialog() With
        {
            .Title = "Play Media",
            .Filter = OPENMEDIA_FILTER,
            .FilterIndex = 4                ' 4 = all files
        }

        ' You might want to add one or more of the following options to your player:

        ' **** 1. DISPLAY OVERLAY *****************************************************************

        ' A display overlay is a Form that allows you to display items on top of a movie.
        ' The sample overlay 'Overlay' is created with the designer - please see Overlay.vb
        myOverlay = New Overlay()            ' create (an instance of) the overlay
        myPlayer.Overlay.Window = myOverlay  ' and attach it to the player

        ' To remove a display overlay from the player set the overlay window to another overlay
        ' or to Nothing: myPlayer.Overlay.Window = Nothing



        ' **** 2. DISPLAY OVERLAY ALWAYS VISIBLE **************************************************

        ' A display overlay is usually only shown when media is playing, but if you want to always
        ' show the overlay you can use: myPlayer.Overlay.Hold = True

        ' The player's display has to be created and visible for this, so if you want
        ' to show the overlay right from the start of your application (without media playing) you
        ' have to put the 'Overlay.Hold' instruction in the Form1.Shown event handler (see below).



        ' **** 3. DISPLAY CLONES ******************************************************************

        ' For special purposes you can create one or more player display clones (display copies).
        ' Display clones require sufficient computing (cpu) power and may slow some computers:
        myPlayer.DisplayClones.AddRange(New Control() {Panel2, Panel3})

        ' cpu load can be reduced by lowering the framerate, quality or size of the clones:
        ' e.g. myPlayer.DisplayClones.FrameRate = 10

        ' You can also 'stretch' display clones (as with displaymode.stretch of the main display): 
        myPlayer.DisplayClones.SetLayout(Panel2, CloneLayout.Stretch)

        ' To remove display clones from the player use one of the clones remove methods, e.g.:
        ' myPlayer.DisplayClones.Remove(Panel2)



        ' **** 4. POSITION SLIDER *****************************************************************

        ' The player can control your media playback position slider (trackbar) with:
        myPlayer.Sliders.Position = TrackBar1

        ' The position slider controlled by the player is only for input by the user. The value of
        ' the slider should not be set from code. If you want to change the playback position (and
        ' the slider value), use the position methods of the player, for example:
        ' myPlayer.Position.FromBegin = TimeSpan.FromSeconds(30).

        ' To remove a slider from the player set the slider to Nothing:
        ' myPlayer.Sliders.Position = Nothing



        ' **** 5. POSITION TIME *******************************************************************

        ' If you want to display the elapsed and/or remaining media playback time (or use your
        ' 'own' position slider) you can get continuous media playback position information with:
        AddHandler myPlayer.Events.MediaPositionChanged, AddressOf MyPlayer_MediaPositionChanged
        ' please see the eventhandler below.

        ' To unsubscribe from the event you can use the RemoveHandler statement.



        ' **** 6. AUDIO SLIDERS *******************************************************************

        ' The player can also control your audio sliders (trackbars) with:
        myPlayer.Sliders.AudioVolume = TrackBar2   ' audio volume slider
        myPlayer.Sliders.AudioBalance = TrackBar3  ' audio balance slider

        ' The audio sliders controlled by the player are only for input by the user. The values of
        ' the sliders should not be set from code. If you want to change the audio properties (and
        ' the sliders values) of the player, use the audio methods of the player, for example:
        ' myPlayer.Audio.Volume = 500

        ' To remove a slider from the player set the slider to Nothing:
        ' myPlayer.Sliders.AudioVolume = Nothing



        ' **** 7. AUDIO VALUES ********************************************************************

        ' To get the changed values of the player's audio volume and balance use these events:
        AddHandler myPlayer.Events.MediaAudioVolumeChanged, AddressOf MyPlayer_MediaAudioVolumeChanged
        AddHandler myPlayer.Events.MediaAudioBalanceChanged, AddressOf MyPlayer_MediaAudioBalanceChanged
        ' please see the eventhandlers below.

        ' To unsubscribe from the event you can use the RemoveHandler statement.



        ' **** 8. AUDIO OUTPUT PEAK LEVELS ********************************************************

        ' From version 0.91, the use of the 'Stereo Mix' device has been replaced by the use of
        ' some functions from the Windows Core Audio Api (Windows Vista or higher only).

        ' Audio output levels can be used to display the peak levels of the system's default audio
        ' output device (eg speakers) in a numeric or graphical (eg level/vu meters) form or for
        ' other purposes.

        ' To get the audio peak values from the system default audio device you can use the
        ' MediaPeakLevelChanged event of the player:

        AddHandler myPlayer.Events.MediaPeakLevelChanged, AddressOf MyPlayer_MediaPeakLevelChanged
        ' see the eventhandler below.

        ' Check if there is an audio device or if this function is not supported (Windows Vista or
        ' higher) with: If myPlayer.LastError Then ...

        ' The values received in the eventhandler are between 0.0 and 1.0 (inclusive) or -1 when media
        ' playback has paused, stopped or ended - for every audio channel (usually 2 for stereo).

        ' To unsubscribe from the event you can use the RemoveHandler statement.



        ' **** 9. MOUSE EVENTS ON DISPLAY, MOVIE OR OVERLAY ***************************************

        ' Note: Because of the new drag option (see item #15 below) this option is now left unused.

        ' Normally you don't get mouse events from a (MCI) movie window, but with this you can:
        ' AddHandler myPlayer.Events.MediaMouseClick, AddressOf myPlayer_MediaMouseClick
        ' it combines mouse events for display, movie and overlay in one, so you always can get a
        ' mouse event (click, move, wheel etc.) on a player's display. Also see: myPlayer.PointTo

        ' For completeness, mouse click event handlers are added for the display clones which are
        ' 'normal' controls with 'normal' mouse events:
        AddHandler Panel2.MouseDown, AddressOf Clone_MouseClick ' Using mousedown for speed
        AddHandler Panel3.MouseDown, AddressOf Clone_MouseClick ' fast mouseclick = dubbelclick
        ' please see the eventhandlers below.



        ' **** 10. MEDIA FINISHED PLAYING *********************************************************

        ' You may want to know when media has finished playing to play other (next) media and/or
        ' stop certain processes (e.g. animation on a display overlay).
        ' To detect that media has finished playing just subscribe to the MediaEnded event:
        AddHandler myPlayer.Events.MediaEnded, AddressOf MyPlayer_MediaEnded

        ' You don't want new media started from the MediaEnded event before all processes have
        ' been notified that the previous media has finished playing, so there's another event:
        AddHandler myPlayer.Events.MediaEndedNotice, AddressOf MyPlayer_MediaEndedNotice
        ' You can use this event to just stop any active processes (and not start any new media).
        ' (With the MediaStarted event you can (re)start processes when new media starts playing.)

        ' To unsubscribe from the event you can use the RemoveHandler statement.



        ' **** 11. MEDIA SUBTITLES ****************************************************************

        ' You can get the SubRip (.srt) subtitles (if any) of the playing media by subscribing to
        ' the MediaSubtitleChanged event:
        AddHandler myPlayer.Events.MediaSubtitleChanged, AddressOf MyPlayer_MediaSubtitleChanged
        ' please see the eventhandler below.

        ' By default the subtitles file should be in the same folder and with the same name (but
        ' with the .srt extension) as the media file, or one of its containing folders. You can
        ' specify to search in any containing folders with (0 = media file 'base' folder):
        myPlayer.Subtitles.DirectoryDepth = 1 ' search base folder and containing folders 1 deep

        ' If the subtitles file is located somewhere else or it has another name, you can specify
        ' where to search with the myPlayer.Subtitles.Directory and/or myPlayer.Subtitles.FileName
        ' functions. The Folder depth search applies also to this location.

        ' There are some more convenient functions, e.g. to synchronize subtitles (TimeShift).

        ' To unsubscribe from the event you can use the RemoveHandler statement.



        ' **** 12. MEDIA TAG INFORMATION (MP3 (ID3) AND WMA (ASF) TAGS) ***************************

        ' You can get media tag information (MP3 (ID3) or WMA (ASF) Tags) with:
        ' TagInfo info = myPlayer.Media.GetTagInfo()

        ' Please see the methods PlayMedia, MyPlayer_MediaEnded, MyPlayer_MediaStopped and
        ' DisposeTagInfo for an example of the use of the media tag information option.



        ' **** 13. PLAY (OR REPEAT) ONLY A PART OF A MEDIA FILE ***********************************

        ' N.B. 'Begin' and 'End' is used to indicate the natural begin and end of media
        '      'Start' and 'Stop' is used to indicate the actual start and stop positions of media
        '      If not changed, 'Start' and 'Stop' values are the same as 'Begin' and 'End' values.

        ' You can play (or repeat) only a part of a media file by specifying the start- and/or stopposition:

        ' a. before media starts playing, e.g. from 00:10:15 to 00:20:00 (uses '...Next' keywords):
        '    myPlayer.Media.StartPositionNext = New TimeSpan(0, 10, 15)
        '    myPlayer.Media.StopPositionNext  = TimeSpan.FromMinutes(20)
        '
        ' b. while media is playing, e.g. finish in 10 minutes from the current position:
        '    myPlayer.Media.StopPosition = myPlayer.Position.FromBegin + TimeSpan.FromMinutes(10)



        ' **** 14. ADD A TASKBAR PROGRESS INDICATOR ***********************************************

        ' To add a progress indicator in the taskbar button of a Form, all you have to do is instruct
        ' the player to do so by specifying the Form
        myPlayer.TaskbarProgress.Add(Me)

        ' You can specify any Form you like (not just the Form the display of the player is on) and use
        ' multiple and/or duplicate Forms.

        ' You can specify the mode of the progress indicator (progress (default) or track (= 'begin to end')):
        myPlayer.TaskbarProgress.Mode = TaskbarProgressMode.Track



        ' **** 15. DRAG A FORM BY DRAGGING A PLAYER'S DISPLAY WINDOW ******************************

        ' In some cases it may be convenient to drag a Form not only by its title bar but also by a
        ' player's display window on the Form. You can enable this option with:
        myPlayer.Display.DragEnabled = True

        ' You can specify the mouse cursor being used while dragging the Form/display window, e.g.
        myPlayer.Display.DragCursor = Cursors.SizeAll  'SizeAll' is also the default drag cursor

        ' If you need to process other mouse events (like in item #9 above) or for other reasons,
        ' you can disable this option (if you had previously enabled it) with:
        ' myPlayer.Display.DragEnabled = False



        ' **** 16. ADD AN INFO LABEL TO THE PLAYER'S POSITION SLIDER *******************************

        ' Displaying slider information with an info label is made easy with the slider methods
        ' 'myPlayer.Sliders.ValueToPoint' gives the x/y-coordinate of a value on the slider and
        ' 'myPlayer.Sliders.PointToValue' gives the value of the slider at a certain x/y-coordinate.
        ' These methods can be used for any .net trackbar (not just the PVS.AVPlayer sliders).
        ' (Without a player you can use: SliderValue.ToPoint or SliderValue.FromPoint)

        ' Info Labels can be used 'everywhere', not just with sliders as in this example.
        ' Just use the info label Show method to display an info label wherever you want.

        ' The location of the info label is determined by the specified location in the Show method
        ' and the Align (e.g. TopCenter (default)) and AlignOffset settings.
        ' The size of the info label is determined by font, bordersize, etc. but the 'additional' size
        ' is set with the TextMargin (or TextSize for a fixed size info label) option.

        ' Most options will probably be clear, but here's some tips:
        ' - border and other brushes: first set the full size of the info label (e.g. fontsize,
        ' bordersize etc. etc. and, in most cases, also a 'dummy' text) then create the brush
        ' - setting a fixed size info label, same as with brushes and then use:
        ' myInfoLabel.TextSize = myInfoLabel.TextSize; // this sets the current size and autosize to false
        ' - the order in which the settings are made can be important.

        ' Create an info label for use with all sliders in this application:
        myInfoLabel = New InfoLabel()                       ' create an info label
        myInfoLabel.RoundedCorners = True                   ' use rounded corners
        myInfoLabel.FontSize = 9.75F                        ' set font size (same as main window)
        myInfoLabel.TextMargin = New Padding(2, 0, 2, 0)    ' fine tuning inner spacing
        myInfoLabel.AlignOffset = New Point(0, 7)           ' move closer to slider thumb


        ' Here are some more examples (uncomment one at a time):

        ' for the background any type of brush can be used (uncomment only this one to use)
        'Dim myBackBrush As LinearGradientBrush = New LinearGradientBrush(New Rectangle(New Point(0, 0), myInfoLabel.Size), SystemColors.ButtonHighlight, SystemColors.ButtonShadow, LinearGradientMode.Vertical)
        'myInfoLabel.BackBrush = myBackBrush

        ' or maybe no background at all (uncomment only this one to use):
        'myInfoLabel.ForeColor = Color.Red
        'myInfoLabel.BackColor = Color.OrangeRed ' reduce visible edges around the text (anti-aliasing on transparent background)
        'myInfoLabel.BorderThickness = New Padding(0)
        'myInfoLabel.FontSize = 36
        'myInfoLabel.Transparent = True

        ' here's a fixed size info label with a background picture (uncomment only this one to use):
        'myInfoLabel.TextMargin = New Padding(10, 70, 10, 10)
        'myInfoLabel.ForeColor = Color.White
        'myInfoLabel.Text = "Balance: Right 10.0" ' set size with the 'longest' possible text
        'myInfoLabel.TextSize = myInfoLabel.TextSize ' set size + autosize off
        'Dim infoImage As Image = Image.FromFile("C:\Users\Public\Pictures\Sample Pictures\Desert.jpg")
        'myInfoLabel.BackImage = infoImage ' set image after turning off autosize (with TextSize)


        ' Display an info label above the position slider's thumb using the slider's scroll event:
        AddHandler TrackBar1.Scroll, AddressOf TrackBar1_Scroll     ' please see below

        ' Same for the audio volume and balance sliders:
        AddHandler TrackBar2.Scroll, AddressOf TrackBar2_Scroll     ' please see below
        AddHandler TrackBar3.Scroll, AddressOf TrackBar3_Scroll     ' please see below



        ' **** 17. CHANGE THE SHAPE OF THE PLAYER'S DISPLAY WINDOW ********************************

        ' The shape of a player's display can be changed from the usual rectangular shape to any
        ' other shape with the Player.Display.SetShape method.
        '
        ' The shape of the display, a 'region', is provided by a special method (callback)
        ' called by the player when the size of the player's display window has changed.
        ' PVS.AVPlayer also provides some preset shapes.
        '
        ' You can create a display shape callback method like this (use any method name you want):
        '
        ' Private Function MyCallbackMethod(player As Player, videoShape As Boolean, shapeBounds As Rectangle) As Region
        '     Dim path As GraphicsPath = New GraphicsPath()
        '     path.AddEllipse(shapeBounds)
        '     Dim shapeRegion As New Region(path)
        '     path.Dispose()
        '     Return shapeRegion
        ' End Function
        '
        ' You can activate the display (and optional overlay) shape with for example:
        ' myPlayer.Display.SetShape(AddressOf MyCallbackMethod, True, False)
        ' This sets the callback method and specifies using video bounds and a normal overlay.

        ' Or you can use one of the preset display shapes (without a callback method), e.g.
        ' myPlayer.Display.SetShape(DisplayShape.Oval)

        ' Custom shaped overlays may not display (background colors) properly with display clones.
        ' When using custom display shapes (eg in full-screen mode), underlying interface items may
        ' become visible that were not visible with a normally formed display.

        ' Display shapes are activated and deactivated with CheckBox4.CheckedChanged - see below



        ' **** 18. USING MEDIA SIGNALS ************************************************************

        ' If you want to be informed that a certain media playback time has been reached, you can
        ' use "media signals":
        '
        ' You can create media signals:
        ' a. by creating a .msf (media signals file) file to be used with certain media, or
        ' b. with the Player.Signals.Add method while media is playing
        '
        ' You can receive media signals by subscribing to the Player.Events.MediaSignal event:
        ' AddHandler myPlayer.Events.MediaSignal, AddressOf MyPlayer_MediaSignal
        '
        ' The media signal eventhandler:
        ' Private Sub MyPlayer_MediaSignal(sender As Object, e As SignalEventArgs)
        '     ' signal index = e.Index
        '     ' signal message = e.Message
        ' End Sub
        '
        ' There are no "standard messages" in media signals, you must use your own "system" to
        ' set and interpret the media signal messages.
        '
        ' For more information please see the file "About PVS.AVPlayer versions.txt", version 0.85



        ' **** 19. AUTOMATICALLY HIDE THE MOUSE CURSOR DURING MEDIA PLAYBACK *********************

        ' If you want to keep the mouse cursor 'out of the way' when a movie (or any other media)
        ' is playing, you can use the "Player.CursorHide" methods of the player. By specifying the
        ' form(s) on which the cursor is to be hidden, the player automatically hides the cursor on
        ' those forms (when in the foreground) when media is played and the mouse has not been used
        ' for a while, for example on this form:

        myPlayer.CursorHide.Add(Me)

        ' You can add as many "cursor hiding" forms to the player as you like, for example forms
        ' that contain a display clone of the player or even forms that are not directly related
        ' to the player.

        ' If you want to be notified when the mouse cursor is hidden or shown by the player, you can
        ' subscribe to the "Player.Events.MediaCursorHideChanged" event (see eventhandler below):

        AddHandler myPlayer.Events.MediaCursorHideChanged, AddressOf MyPlayer_MediaCursorHideChanged

    End Sub

    ' Display overlay right from the start - handler created from the designer
    Private Sub Form1_Shown(sender As Object, e As EventArgs) Handles MyBase.Shown
        ' show the display overlay at the start of the application (even if no movie is playing):
        ' this instruction is put here because the player's display has to be 'created and visible'
        ' to show the overlay
        myPlayer.Overlay.Hold = True
    End Sub

    ' Clean Up - this is moved here from the 'Form1.Designer.vb' file and appended:
    Protected Overrides Sub Dispose(ByVal disposing As Boolean)
        Try
            If Not wasDisposed Then
                wasDisposed = True
                If disposing Then

                    ' disposing a player also stops its overlay, display clones, eventhandlers etc.
                    myPlayer.Dispose()      ' dispose the player
                    myOverlay.Dispose()     ' dispose the display overlay
                    myOpenFileDlg.Dispose() ' dispose the file selector

                    If myInfoLabel IsNot Nothing Then
                        myInfoLabel.Dispose()
                    End If

                    ' used by the designer - clean up
                    If components IsNot Nothing Then components.Dispose()
                End If
            End If
        Finally
            MyBase.Dispose(disposing)
        End Try
    End Sub

    ' Dispose media tag info
    Private Sub DisposeTagInfo()
        If myTagInfo IsNot Nothing Then
            myOverlay.subtitlesLabel.Text = String.Empty
            Panel1.BackgroundImage = Nothing
            myTagInfo.Dispose()
            myTagInfo = Nothing
        End If
    End Sub

#End Region

    ' **** Media Play / Pause - Resume / Stop *****************************************************

#Region "Media Play / Pause - Resume / Stop"

    Private Sub PlayMedia()
        ' select and play media
        If (myOpenFileDlg.ShowDialog() = DialogResult.OK) Then
            myPlayer.Play(myOpenFileDlg.FileName)
            If (myPlayer.LastError) Then
                MessageBox.Show(myPlayer.LastErrorString)
            Else
                ' show media tag information (for audio media only)
                If Not myPlayer.HasVideo Then
                    myTagInfo = myPlayer.Media.GetTagInfo()
                    Panel1.BackgroundImageLayout = ImageLayout.Zoom

                    Panel1.BackgroundImage = myTagInfo.Image
                    myOverlay.subtitlesLabel.Text = myTagInfo.Artist + vbNewLine + myTagInfo.Title
                End If
            End If
        End If
    End Sub

    Private Sub PauseMedia()
        myPlayer.Paused = Not myPlayer.Paused
        If (myPlayer.Paused) Then
            Button2.Text = "Resume"
        Else
            Button2.Text = "Pause"
        End If
    End Sub

    Private Sub StopMedia()
        myPlayer.Stop()
    End Sub

#End Region

    ' **** Player Eventhandlers *******************************************************************

#Region "Player Eventhandlers"

    ' Show changed audio volume value
    Private Sub MyPlayer_MediaAudioVolumeChanged(sender As Object, e As EventArgs)
        Label3.Text = myPlayer.Audio.Volume.ToString()
    End Sub

    ' Show changed audio balance value
    Private Sub MyPlayer_MediaAudioBalanceChanged(sender As Object, e As EventArgs)
        Label4.Text = myPlayer.Audio.Balance.ToString()
    End Sub

    ' Display the elapsed and remaining playback time
    Private Sub MyPlayer_MediaPositionChanged(sender As Object, e As PositionEventArgs)

        Label1.Text = e.FromStart.ToString().Substring(0, 8) ' "hh:mm:ss"
        Label2.Text = e.ToStop.ToString().Substring(0, 8)     ' "hh:mm:ss"

        ' from .NET 4.0 TimeSpan supports (custom) format strings e.g.
        ' Label1.Text = e.FromStart.ToString("hh\:mm\:ss")   ' "hh:mm:ss"

    End Sub

    ' Handle media audio output peak levels - calculate the values and paint the level displays
    Private Sub MyPlayer_MediaPeakLevelChanged(sender As Object, e As PeakLevelEventArgs)
        ' you could add some 'logic' here to make the movements of the indicators less 'jumpy'

        If e.MasterPeakValue = -1 Then ' Media playback has stopped or paused or volume set to 0
            ' graphical presentation
            leftLevel = 0
            rightLevel = 0

            ' value display
            Label5.Text = "0.00" ' same format as below
            Label6.Text = "0.00"
        Else
            ' check e.ChannelCount for more than 2 (= stereo) channels
            ' if you want to display the peak levels of all audio channels

            ' graphical presentation
            leftLevel = CInt(e.ChannelsValues(0) * levelUnit) ' levelUnit is the width of panel4 and 5
            rightLevel = CInt(e.ChannelsValues(1) * levelUnit)

            ' value display (use string format because of 'Single' rounding errors - zero can become a small value):
            Label5.Text = e.ChannelsValues(0).ToString("0.00")
            Label6.Text = e.ChannelsValues(1).ToString("0.00")
        End If
        Panel4.Invalidate() ' draw the new values
        Panel5.Invalidate()
    End Sub

    ' Paint the left channel audio output level display - handler created from the designer
    Private Sub Panel4_Paint(sender As Object, e As PaintEventArgs) Handles Panel4.Paint
        e.Graphics.FillRectangle(Brushes.LightSlateGray, 0, 0, leftLevel, Panel4.ClientRectangle.Height)
    End Sub

    ' Paint the right channel audio output level display - handler created from the designer
    Private Sub Panel5_Paint(sender As Object, e As PaintEventArgs) Handles Panel5.Paint
        e.Graphics.FillRectangle(Brushes.LightSlateGray, 0, 0, rightLevel, Panel5.ClientRectangle.Height)
    End Sub

    ' Mouse clicked on player display - movie, overlay or just display
    Private Sub MyPlayer_MediaMouseClick(sender As Object, e As MediaMouseEventArgs)
        If (myPlayer.Display.Mode = DisplayMode.Stretch) Then
            myPlayer.Display.Mode = DisplayMode.ZoomCenter
        Else
            myPlayer.Display.Mode = DisplayMode.Stretch
        End If
    End Sub

    ' Mouse clicked on player display clone
    Private Sub Clone_MouseClick(sender As Object, e As MouseEventArgs)
        Dim clickedPanel As Panel = CType(sender, Panel)
        If myPlayer.DisplayClones.GetLayout(clickedPanel) = CloneLayout.Stretch Then
            myPlayer.DisplayClones.SetLayout(clickedPanel, CloneLayout.Zoom)
        Else
            myPlayer.DisplayClones.SetLayout(clickedPanel, CloneLayout.Stretch)
        End If
    End Sub

    ' Media has finished playing (1)
    Private Sub MyPlayer_MediaEndedNotice(sender As Object, e As EndedEventArgs)

        ' you can just stop any processes (and not starting new media) from the
        ' MediaEndedNotice eventhandler that is fired just before the MediaEnded event.

        'Select Case e.StopReason

        '    Case StopReason.Finished

        '    Case StopReason.AutoStop

        '    Case StopReason.UserStop

        'End Select

    End Sub

    ' Media has finished playing (2)
    Private Sub MyPlayer_MediaEnded(sender As Object, e As EndedEventArgs)

        DisposeTagInfo()

        'Select Case e.StopReason

        '    Case StopReason.Finished
        '        ' play next media ...

        '    Case StopReason.AutoStop

        '    Case StopReason.UserStop

        'End Select

    End Sub

    ' Get media subtitles / media subtitle changed
    Private Sub MyPlayer_MediaSubtitleChanged(sender As Object, e As SubtitleEventArgs)
        ' In this example the subtitle's text is shown in a label on a display overlay.
        myOverlay.subtitlesLabel.Text = e.Subtitle
    End Sub

    ' Show an infolabel on the position slider of the player when scrolled
    Private Sub TrackBar1_Scroll(sender As Object, e As EventArgs)

        ' Get the position slider's x-coordinate of the current position (= thumb location)
        ' (myInfoLabel.AlignOffset has been set to 0, 7)
        Dim myInfoLabelLocation As Point = myPlayer.Sliders.ValueToPoint(TrackBar1, TrackBar1.Value)

        ' Show the infolabel
        myInfoLabel.Show(myPlayer.Position.FromStart.ToString().Substring(0, 8), TrackBar1, myInfoLabelLocation)

    End Sub

    ' Show an infolabel on the audio volume slider of the player when scrolled
    Private Sub TrackBar2_Scroll(sender As Object, e As EventArgs)

        ' Get the audio volume slider's x-coordinate of the current volume (= thumb location)
        ' (myInfoLabel.AlignOffset has been set to 0, 7)
        Dim myInfoLabelLocation As Point = myPlayer.Sliders.ValueToPoint(TrackBar2, TrackBar2.Value)

        ' Show the infolabel
        myInfoLabel.Show("Volume: " + (myPlayer.Audio.Volume / 100.0).ToString("0.0"), TrackBar2, myInfoLabelLocation)

    End Sub

    ' Show an infolabel on the audio balance slider of the player when scrolled
    Private Sub TrackBar3_Scroll(sender As Object, e As EventArgs)

        ' Get the position slider's x-coordinate of the current position (= thumb location)
        ' (myInfoLabel.AlignOffset has been set to 0, 7)
        Dim myInfoLabelLocation As Point = myPlayer.Sliders.ValueToPoint(TrackBar3, TrackBar3.Value)

        ' Calculate balance display value
        Dim val As Double = (myPlayer.Audio.Balance - 500) / 50.0

        ' Set the text for the infolabel (using StringBuilder)
        myInfoLabelText.Length = 0
        If val = 0 Then
            myInfoLabelText.Append("Balance: Center")
        Else
            If val < 0 Then
                myInfoLabelText.Append("Balance: Left ").Append((-val).ToString("0.0"))
            Else
                myInfoLabelText.Append("Balance: Right ").Append(val.ToString("0.0"))
            End If
        End If

        ' Show the infolabel
        myInfoLabel.Show(myInfoLabelText.ToString, TrackBar3, myInfoLabelLocation)

    End Sub

    ' The mouse cursor is hidden or shown by the player
    Private Sub MyPlayer_MediaCursorHideChanged(sender As Object, e As CursorHideEventArgs)
        If e.Visible Then
            Panel6.BackColor = Color.Lime   ' cursor is shown
        Else
            Panel6.BackColor = Color.Red    ' cursor is hidden
        End If
    End Sub

#End Region

    ' **** Controls Handling **********************************************************************

#Region "Checkboxes"

    ' Set player display overlay
    Private Sub CheckBox1_CheckedChanged(sender As Object, e As EventArgs) Handles CheckBox1.CheckedChanged
        If isInitializing Then Return

        If CheckBox1.Checked Then
            myPlayer.Overlay.Window = myOverlay
        Else
            myPlayer.Overlay.Window = Nothing
        End If
    End Sub

    ' Set player display clones overlay
    Private Sub CheckBox2_CheckedChanged(sender As Object, e As EventArgs) Handles CheckBox2.CheckedChanged
        If isInitializing Then Return
        myPlayer.DisplayClones.ShowOverlay = CheckBox2.Checked
    End Sub

    ' Set position slider live update
    Private Sub CheckBox3_CheckedChanged(sender As Object, e As EventArgs) Handles CheckBox3.CheckedChanged
        If isInitializing Then Return
        myPlayer.Sliders.PositionLiveUpdate = CheckBox3.Checked
    End Sub

    ' Set taskbar progress indicator
    Private Sub TaskbarBox_CheckedChanged(sender As Object, e As EventArgs) Handles TaskbarBox.CheckedChanged
        If isInitializing Then Return
        If (TaskbarBox.Checked) Then
            myPlayer.TaskbarProgress.Add(Me)
        Else
            myPlayer.TaskbarProgress.Remove(Me)
        End If
    End Sub

    ' Set display window shapes
    Private Sub CheckBox4_CheckedChanged(sender As Object, e As EventArgs) Handles CheckBox4.CheckedChanged

        shapeStatus += 1
        If shapeStatus > 5 Then shapeStatus = 0

        Select Case shapeStatus

            Case 1 ' oval shaped video and overlay
                myPlayer.Display.SetShape(DisplayShape.Oval, True, True)
                Panel2.BackColor = Me.BackColor
                Panel3.BackColor = Me.BackColor

            Case 3 ' rounded rectangle shaped video and overlay
                myPlayer.Display.SetShape(DisplayShape.Rounded, True, True)
                Panel2.BackColor = Me.BackColor
                Panel3.BackColor = Me.BackColor

            Case 5 ' star shaped video and overlay
                myPlayer.Display.SetShape(DisplayShape.Star, True, True)
                Panel2.BackColor = Me.BackColor
                Panel3.BackColor = Me.BackColor

            Case Else
                ' normal shaped display and overlay
                myPlayer.Display.SetShape(DisplayShape.Normal)
                Panel2.BackColor = Color.FromArgb(32, 32, 32)
                Panel3.BackColor = Color.FromArgb(32, 32, 32)

        End Select

    End Sub

#End Region

#Region "Buttons"

    ' Play
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        PlayMedia()
    End Sub

    ' Pause / Resume
    Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
        PauseMedia()
    End Sub

    ' Stop
    Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
        StopMedia()
    End Sub

    ' Quit
    Private Sub Button4_Click(sender As Object, e As EventArgs) Handles Button4.Click
        Application.Exit()
    End Sub


#End Region

End Class

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