Click here to Skip to main content
Click here to Skip to main content

Play your AVI files with this DirectX video player

, 24 Apr 2013 CPOL
Rate this:
Please Sign up or sign in to vote.
Plays AVI files using Microsoft's DirectX.AudioVideoPlayback.

Notice: The only version that is being updated is the Visual Studio 2012 version. For all who thought they were downloading a video player and got a MP3 player instead, OOPs, SORRY. The rspPlayer is not what you thought. please download the middle link for the video player. Once again....Sorry!

2012 AVI Player

Intro to the 2012 version

The 2012 version looks similar to the VLC Media Player. You can scroll forward and back from 30 seconds to 5 minutes. It has fullscreen and a message to return to normal view while in fullscreen. It works really nice.

Stuff Needed to work!

You will need the Latest version DirectX SDK for .Net 3.5. Visual Studio 2012, VB.NET. The player use .NET Framework 3.5 to be compatible with the Microsoft.DirectX.AudioVideoPlayBack.dll library. The 2012 version now plays *.avi, *.flv, *.wmv, *.divX, *.xvid, *.mpeg, *.mpg as long as you have the codecs installed.

Introduction and Background

I started dabbling in DirectX a few months ago to play AVI files and in my searching, I came across Mike Gold's C# version which can be found here. This serves as an excellent starting point that you can build on. I converted his code to VB which took me a while to do. I could have used some conversion program or used the website that does this, but I decided to do this on my own. It's good practice.

After a few builds and a couple of updates I came up with the 2012 version. I used a panel to display the video in fullscreen. When you are in fullscreen mode, it is not really fullscreen mode. I set the panels height and width to the Screen.PrimaryScreen.WorkingArea.Height and the Screen.PrimaryScreen.WorkingArea.Width and set the toolbars( top and bottom ) visible property to "False". This imitates the fullscreen mode. I added a label to show to the user the hot keys to exit the fullscreen mode. The hot-key label is displayed by use of a timer at 2, 12, and 25 minute intervals. By right-clicking the fast_forward and rewind buttons, a context menu appears so you can skip forwards or backwards from 30 seconds to 5 minutes. I removed the volume and balance trackbars. I replaced the labels with a few new ones to display just the full length and the current time before and after the progress bar ( in pic above ). I kept the progressbar to handle the current video position. This just pertains to the VS 2012 version.

Added Save and OpenSavedFrom Subs via the Menustrip

The mnuFSave and mnuFOpenSavedFile Menu Click events were added in case you needed to leave in a hurry. All you have to do is press Pause, Press Save, Press Stop and close the app. When you return to the app, you can play from where you left off by pressing the mnuFOpenSavedFile menu item. Here is the code I used to do this...

Private Sub mnuFSaveFrom_Click(sender As Object, e As EventArgs) Handles mnuFSaveFrom.Click
    My.Settings.m_StartFrom = _myVideo.CurrentPosition
    My.Settings.m_StartFromFilename = _strSavedFile
    _toBeContinued = True
    My.Settings.m_ToBeContinued = _toBeContinued
    My.Settings.Save()
End Sub

Private Sub OpenSavedFile(ByVal fName As String)
    If fName = "" Or fName = String.Empty Or fName = vbNullString Then Exit Sub

    Dim hite As Integer = pnlVideo.Height
    Dim wide As Integer = pnlVideo.Width

    _myVideo = New Video(fName)

    'Set the Owner...
    _myVideo.Owner = pnlVideo

    'Set the video's size...
    pnlVideo.Height = hite
    pnlVideo.Width = wide

    'Set the starting point of the saved file where you left off.
    _myVideo.CurrentPosition = _
       _myVideo.SeekCurrentPosition(My.Settings.m_StartFrom, SeekPositionFlags.AbsolutePositioning)

    'Set Trackbar values...
    gtbPosition.MaxValue = _myVideo.Duration
    gtbPosition.Value = _myVideo.CurrentPosition()

    'Play it.
    _myVideo.Play()

    'Show the cursor...
    _myVideo.ShowCursor()

    'Start the timer...
    videoTimer.Enabled = True

    'Set the form text
    lblFormText.Text = "RSVP 2 - " & fName

    'Show the PlayState in a label...
    lblStatus.Text = StateFlags.Running.ToString()

    'Do some Button and Menu checking
    btnPlay.Enabled = False
    mnuOPlay.Enabled = False
    cmsPlay.Enabled = False

    btnPause.Enabled = True
    mnuOPause.Enabled = True
    cmsPause.Enabled = True

    btnStop.Enabled = False
    mnuOStop.Enabled = False
    cmsStop.Enabled = False

    mnuONormalView.Checked = True
    mnuOFullScreen.Checked = False

    mnuFOpen.Enabled = False
    mnuFSaveFrom.Enabled = True

End Sub

Private Sub mnuFOpenSavedFile_Click(sender As Object, e As EventArgs) Handles mnuFOpenSavedFile.Click
    If My.Settings.m_ToBeContinued = True Then
        OpenSavedFile(My.Settings.m_StartFromFilename)

        _toBeContinued = False
        My.Settings.m_ToBeContinued = _toBeContinued
        My.Settings.m_StartFrom = Nothing
        _strSavedFile = ""
        My.Settings.m_StartFromFilename = _strSavedFile
        My.Settings.Save()
    End If

End Sub

Obstacles

I came across a few problems like getting the cursor to display while the video is playing, formatting the length in time to a label, formatting the current play time in a label, displaying the context menu in full-screen mode, and playing the video in the panel.

Let's start with getting the video to play in the panel. As I said earlier, I removed a lot of Mike's code and it just so happens that this was part of it. So here is my version of this:

Private Sub OpenAviFile()

    Dim vTime As String = Nothing
    lblTime.Text = "00:00:00"

    With ofd
        .InitialDirectory = "F:\movz\"
        .RestoreDirectory = True
    End With

    If ofd.ShowDialog = Windows.Forms.DialogResult.OK Then
        Dim height As Integer = pnlVideo.Height
        Dim width As Integer = pnlVideo.Width

        'Set the video to the selected file.
        video = New Video(ofd.FileName)

        'Get the file Length in time.
        vTime = GetVideoTime(vTime)

        'Display the Labels and what is playing.
        txtPlaying.Visible = True
        txtPlaying.Text = ""
        txtPlaying.Text = "<<<--- Now Playing   " & ofd.FileName & " --->>>"
        lblTime.Text = vTime
        fName = ofd.FileName
        GetFileLength()
        'Set the owner of the video.
        video.Owner = pnlVideo

        'Set the height and width of the video panel and
        'set the progressbar to zero.
        pnlVideo.Height = height
        pnlVideo.Width = width
        videoProgress.Value = 0

        'Play the file, enable the timer, set the boolean to true.
        video.Play()
        lblTimer.Enabled = True
        videoPlaying = True
    End If
End Sub

Next, I had to update the progressbar and the lblDuration label to correspond with the video that is currently playing.

Private Sub lblTimer_Tick(ByVal sender As System.Object, _
                            ByVal e As System.EventArgs) Handles lblTimer.Tick
    'Display the current position on the progressBar.
    Dim vPosition As Integer = Convert.ToInt32(video.CurrentPosition * 1000)
    Dim vDuration As Integer = Convert.ToInt32(video.Duration * 1000)
    If vDuration > 0 Then
        videoProgress.Value = Convert.ToInt32((vPosition * 100) / vDuration)
    End If

    'Display the current time of the video on a label.
    lblDuration.Text = Format(Int(video.Duration \ 3600), "00") & ":" & _
    Format(Int(video.Duration - video.CurrentPosition) \ 60, "00") & ":" & _
    Format(Int((video.Duration - video.CurrentPosition) Mod 60), "00").ToString

End Sub

The length of the file in MB size was quite simple to implement. I created a variable myFile and set it to FileInfo. I then initialized it to FileInfo(fName) which is declared in the top variables. I then created another variable named length and set it to myFile.Length. All that was left was to format it to MBs. Here's the code:

Private Sub GetFileLength()
    'Gets the length of the selected file.
    'This sub is called in OpenAVIFile sub.
    Dim myFile As FileInfo
    myFile = New FileInfo(fName)
    Dim length As Long = myFile.Length()
    lblLength.Text = Format(Int(length / 1048000), "000") & " MB"
End Sub

This program is not loaded with a lot of useless stuff. All it does is play AVI files, displays a few labels with some data, adjusts the volume, and balances or uses the Fullscreen and back to Normal views. That's it.

Screensaver control

I added Kurt Shultz' screensaver control, which can be viewed here on CodeProject. I changed the forms' look to accommodate my program. I also converted his C# code to VB. This took me quite a while. About 5 hours.

Avi_Player/DadsAviPlayer_ScreenSaverControl.jpg

Updates

  • Added newly designed video player 09/24/2012
  • Added a save routine if you need to leave in a hurry. 04/06/2013
  • Added a OpenSaveFile() Sub when you want to resume playing. 04/06/2013

Here is a list of all updates that I performed on the player. Added menu checking, FastForward/Rewind, Kurts Shultz' screensaver control which takes care of the full-screen reverting back to Normal view. I also added an input box when you exit to check if the screensaver function was reactivated. Hope you enjoy this program.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

rspercy65
Retired
United States United States
I am currently retired.
I have no degree but I have some programming experience
when I was in college(Cobol, Pascal).
 
My accomplishments thus far are;
Best VB.Net article for January(2009)
Best VB.Net article for July(2009)

Comments and Discussions

 
QuestionCan we play the video and audio out to DVD card? PinmemberJeffrey Kosal24-Jan-13 0:45 
SuggestionRe: Can we play the video and audio out to DVD card? Pinmemberrspercy657-Feb-13 14:32 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.141030.1 | Last Updated 24 Apr 2013
Article Copyright 2009 by rspercy65
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid