Click here to Skip to main content
Licence CPOL
First Posted 13 Jan 2009
Views 35,393
Downloads 1,407
Bookmarked 25 times

Play Your Avi Files with this DirectX Video Player

By | 13 Jan 2009 | Article
Plays avi files using Microsoft's DirectX.AudioVideoPlayback

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.

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

DadsAviPlayer_ScreenSaverControl.jpg

Updates

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.

License

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

About the Author

rspercy65

Retired

United States United States

Member

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)

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
Questiontank PinmemberDon_Hard23:27 26 May '12  
GeneralMy vote of 5 Pinmembermanoj kumar choubey1:36 24 Feb '12  
QuestiondirectX fastforward? Pinmemberlettm123423:02 25 Feb '10  
AnswerRe: directX fastforward? Pinmemberrspercy602:46 27 Feb '10  
QuestionThe Rapidshare link for the new file is broken... PinmemberPaul Grimmer12:45 2 Nov '09  
AnswerRe: The Rapidshare link for the new file is broken... Pinmemberrspercy601:07 3 Nov '09  
GeneralNice PinmemberJared James Sullivan14:07 16 Mar '09  
GeneralDownloading... Pinmemberdherrmann0:35 19 Jan '09  
GeneralRe: Downloading... Pinmemberrspercy589:35 19 Jan '09  
GeneralMy vote of 2 Pinmembersam.hill19:25 14 Jan '09  
GeneralRe: My vote of 2 Pinmemberrspercy580:57 15 Jan '09  
GeneralRe: My vote of 2 Pinmemberrspercy581:15 15 Jan '09  
That sounds wierd. I have no problems with it. I can make an exe and publish it with no problems and it runs just fine. I did post a fix for one bug that I found, but that was just a math problem. I am currently working on the memory leakage problem that is always present when you use AudioVideoPlayback.dll and also doing a video search (FF and RW).
If you have Option Explicit ON, please turn it off, this could be the problem. Option Explicit does not allow conversions of particular types and I have those types in this program more than once.
Please let me know of the bugs and (type of) that you are having the problems with so I can post fixes for them. THNX for the Heads-Up on the bugs.
 
rspercy
1 + 1 = 186,440....Depending on the species.

GeneralError found in lblTimer_Tick Event ... [modified] Pinmemberrspercy5811:00 14 Jan '09  
GeneralMissing files and references ... Pinmembersam.hill18:15 13 Jan '09  
GeneralRe: Missing files and references ... Pinmemberrspercy5819:24 13 Jan '09  
GeneralRe: Missing files and references ... Pinmemberrspercy5819:47 13 Jan '09  
GeneralRe: Missing files and references ... Pinmembersam.hill18:56 14 Jan '09  

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.

Permalink | Advertise | Privacy | Mobile
Web01 | 2.5.120529.1 | Last Updated 13 Jan 2009
Article Copyright 2009 by rspercy65
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid