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. So here is my version of this:
Private Sub OpenAviFile()
Dim vTime As String = Nothing
lblTime.Text = "00:00:00"
.InitialDirectory = "F:\movz\"
.RestoreDirectory = True
If ofd.ShowDialog = Windows.Forms.DialogResult.OK Then
Dim height As Integer = pnlVideo.Height
Dim width As Integer = pnlVideo.Width
video = New Video(ofd.FileName)
vTime = GetVideoTime(vTime)
txtPlaying.Visible = True
txtPlaying.Text = ""
txtPlaying.Text = "<<<--- Now Playing " & ofd.FileName & " --->>>"
lblTime.Text = vTime
fName = ofd.FileName
video.Owner = pnlVideo
pnlVideo.Height = height
pnlVideo.Width = width
videoProgress.Value = 0
lblTimer.Enabled = True
videoPlaying = True
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
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)
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
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()
Dim myFile As FileInfo
myFile = New FileInfo(fName)
Dim length As Long = myFile.Length()
lblLength.Text = Format(Int(length / 1048000), "000") & " MB"
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.
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.
- Added Newly Designed Video Player 09/24/2012
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.