Click here to Skip to main content
11,804,919 members (68,177 online)
Click here to Skip to main content

Play Your Avi Files with this DirectX Video Player

, 13 Jan 2009 CPOL 80.2K 5.6K 58
Rate this:
Please Sign up or sign in to vote.
Plays avi files using Microsoft's DirectX.AudioVideoPlayback
This is an old version of the currently published article.

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-Sharp 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 web site that does this, but, I decided to do this on my own. It's good practice.

I removed a lot of Mike's code, buttons and check box (for fullscreen mode) and replaced it with a menustrip, context menu (for Normal and Fullscreen modes). I added volume and balance trackbars, three labels to display the avi's file size in hh:mm:ss, file length in MB, current playing time in hh:mm:ss and a progressbar to handle the current video position.


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 Fullscreen 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. Frown | :( 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
            '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.
            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-Sharp code to VB. This took me quite a while. About 5 hours.



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


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


About the Author

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)

You may also be interested in...

Comments and Discussions

Discussions posted for the Published version of this article. Posting a message here will take you to the publicly available article in order to continue your conversation in public.
GeneralThe UPDATE only pertains to the Visual Studio 2012 Version Pin
rspercy657-Apr-13 7:05
memberrspercy657-Apr-13 7:05 
GeneralThis is for anybody wanting to play a DVD Pin
rspercy656-Feb-13 8:01
memberrspercy656-Feb-13 8:01 
QuestionCan we play the video and audio out to DVD card? Pin
Jeffrey Kosal24-Jan-13 0:45
memberJeffrey Kosal24-Jan-13 0:45 
SuggestionRe: Can we play the video and audio out to DVD card? Pin
rspercy657-Feb-13 14:32
memberrspercy657-Feb-13 14:32 
QuestionIncrease or Boost Volume Pin
Akshay 00721-Nov-12 4:26
groupAkshay 00721-Nov-12 4:26 
SuggestionRe: Increase or Boost Volume Pin
rspercy6521-Nov-12 13:42
memberrspercy6521-Nov-12 13:42 
Questiontank Pin
Don_Hard26-May-12 23:27
memberDon_Hard26-May-12 23:27 
GeneralMy vote of 5 Pin
manoj kumar choubey24-Feb-12 1:36
membermanoj kumar choubey24-Feb-12 1:36 
QuestiondirectX fastforward? Pin
lettm123425-Feb-10 23:02
memberlettm123425-Feb-10 23:02 
AnswerRe: directX fastforward? Pin
rspercy6027-Feb-10 2:46
memberrspercy6027-Feb-10 2:46 
QuestionThe Rapidshare link for the new file is broken... Pin
Paul Grimmer2-Nov-09 12:45
memberPaul Grimmer2-Nov-09 12:45 
AnswerRe: The Rapidshare link for the new file is broken... Pin
rspercy603-Nov-09 1:07
memberrspercy603-Nov-09 1:07 
GeneralNice Pin
Jared James Sullivan16-Mar-09 14:07
memberJared James Sullivan16-Mar-09 14:07 
GeneralDownloading... Pin
dherrmann19-Jan-09 0:35
memberdherrmann19-Jan-09 0:35 
GeneralRe: Downloading... Pin
rspercy5819-Jan-09 9:35
memberrspercy5819-Jan-09 9:35 
GeneralMy vote of 2 Pin
sam.hill14-Jan-09 19:25
membersam.hill14-Jan-09 19:25 
GeneralRe: My vote of 2 Pin
rspercy5815-Jan-09 0:57
memberrspercy5815-Jan-09 0:57 
GeneralRe: My vote of 2 Pin
rspercy5815-Jan-09 1:15
memberrspercy5815-Jan-09 1:15 
GeneralError found in lblTimer_Tick Event ... [modified] Pin
rspercy5814-Jan-09 11:00
memberrspercy5814-Jan-09 11:00 
GeneralMissing files and references ... Pin
sam.hill13-Jan-09 18:15
membersam.hill13-Jan-09 18:15 
GeneralRe: Missing files and references ... Pin
rspercy5813-Jan-09 19:24
memberrspercy5813-Jan-09 19:24 
GeneralRe: Missing files and references ... Pin
rspercy5813-Jan-09 19:47
memberrspercy5813-Jan-09 19:47 
GeneralRe: Missing files and references ... Pin
sam.hill14-Jan-09 18:56
membersam.hill14-Jan-09 18:56 

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
Web03 | 2.8.151002.1 | Last Updated 13 Jan 2009
Article Copyright 2009 by rspercy65
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid