Click here to Skip to main content
15,860,972 members
Articles / Programming Languages / Visual Basic

Play your AVI files with this DirectX video player

Rate me:
Please Sign up or sign in to vote.
4.83/5 (12 votes)
24 Apr 2013CPOL4 min read 129.4K   8K   60   23
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!

Image 1

2012 AVI Player

Image 2

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...

VB
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:

VB
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.

VB
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:

VB.NET
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)


Written By
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

 
GeneralThe UPDATE only pertains to the Visual Studio 2012 Version Pin
rspercy657-Apr-13 7:05
rspercy657-Apr-13 7:05 
GeneralThis is for anybody wanting to play a DVD Pin
rspercy656-Feb-13 8:01
rspercy656-Feb-13 8:01 
QuestionCan we play the video and audio out to DVD card? Pin
Jeffrey Kosal24-Jan-13 0:45
Jeffrey Kosal24-Jan-13 0:45 
SuggestionRe: Can we play the video and audio out to DVD card? Pin
rspercy657-Feb-13 14:32
rspercy657-Feb-13 14:32 
QuestionIncrease or Boost Volume Pin
Akshay 00721-Nov-12 4:26
Akshay 00721-Nov-12 4:26 
SuggestionRe: Increase or Boost Volume Pin
rspercy6521-Nov-12 13:42
rspercy6521-Nov-12 13:42 
Questiontank Pin
Eduardo_David26-May-12 23:27
Eduardo_David26-May-12 23:27 
GeneralMy vote of 5 Pin
Manoj Kumar Choubey24-Feb-12 1:36
professionalManoj Kumar Choubey24-Feb-12 1:36 
QuestiondirectX fastforward? Pin
lettm123425-Feb-10 23:02
lettm123425-Feb-10 23:02 
AnswerRe: directX fastforward? Pin
rspercy6527-Feb-10 2:46
rspercy6527-Feb-10 2:46 
QuestionThe Rapidshare link for the new file is broken... Pin
Paul Grimmer2-Nov-09 12:45
Paul Grimmer2-Nov-09 12:45 
AnswerRe: The Rapidshare link for the new file is broken... Pin
rspercy653-Nov-09 1:07
rspercy653-Nov-09 1:07 
GeneralNice Pin
J Sullivan16-Mar-09 14:07
J Sullivan16-Mar-09 14:07 
GeneralDownloading... Pin
dherrmann19-Jan-09 0:35
dherrmann19-Jan-09 0:35 
GeneralRe: Downloading... Pin
rspercy6519-Jan-09 9:35
rspercy6519-Jan-09 9:35 
GeneralMy vote of 2 Pin
sam.hill14-Jan-09 19:25
sam.hill14-Jan-09 19:25 
GeneralRe: My vote of 2 Pin
rspercy6515-Jan-09 0:57
rspercy6515-Jan-09 0:57 
Please tell me what bugs are in the program and where. I know of one and I posted a message on it with a fix. THNX 4 the vote.

rspercy
1 + 1 = 186,440....Depending on the species.

GeneralRe: My vote of 2 Pin
rspercy6515-Jan-09 1:15
rspercy6515-Jan-09 1:15 
GeneralError found in lblTimer_Tick Event ... [modified] Pin
rspercy6514-Jan-09 11:00
rspercy6514-Jan-09 11:00 
GeneralMissing files and references ... Pin
sam.hill13-Jan-09 18:15
sam.hill13-Jan-09 18:15 
GeneralRe: Missing files and references ... Pin
rspercy6513-Jan-09 19:24
rspercy6513-Jan-09 19:24 
GeneralRe: Missing files and references ... Pin
rspercy6513-Jan-09 19:47
rspercy6513-Jan-09 19:47 
GeneralRe: Missing files and references ... Pin
sam.hill14-Jan-09 18:56
sam.hill14-Jan-09 18:56 

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

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