![]() |
Languages »
VB.NET »
General
Intermediate
License: The Code Project Open License (CPOL)
The Ultimate Media PlayerBy MatrixCoderHave you ever wanted to create a Music/Video Player? Well, now you can. This article will teach you how to create a fully functional general media player. |
VB.NET 2.0, WinXPVS2005, Dev
|
||||||||||
|
Advanced Search |
|
|
|
||||||||||||||||

Playing Media isn't hard (at least not in Visual Basic). This tutorial will teach you how to create a fully functional general media player for any use by using the Windows Media Player Control.
First, you must insert a Media Player Control in your form (Note: If you cannot find the control, look under "Com Components" in the "Choose Toolbox Items" menu). Now the Media Player will have its own controls, so to design your own, you will have to remove these by setting the UiMode to none. Next, you will need to add your own controls, like this:

Not only are there "Play, Pause and Stop" controls, but there are also "Volume, Balance and Track" controls. The interesting part is the Duration and the Track Bar, which are not as easy as they look.
Dim CurPos As Integer = Convert.ToInt32(PlayerControl.Ctlcontrols.currentPosition * 1000) 'milliseconds Dim DurationVar As Integer = Convert.ToInt32(PlayerControl.currentMedia.duration * 1000) 'milliseconds If DurationVar > 0 Then PlayBar.Value = Convert.ToInt32((CurPos * 100) / DurationVar) '% complete End If 'Update the time label Duration.Text = PlayerControl.Ctlcontrols.currentPositionString
Try If (PlayerControl.currentMedia.duration <> 0) Then Dim NewPerc As Double = Convert.ToDouble(PlayBar.Value) / 100 Dim DurationVar As Integer = Convert.ToInt32(PlayerControl.currentMedia.duration * 1000) 'milliseconds Dim NewPos As Integer = (DurationVar * NewPerc) / 1000 PlayerControl.Ctlcontrols.currentPosition = NewPos Else PlayBar.Value = 0 End If Catch ex As Exception MsgBox(ex.Message) End Try
Now there are a few Media Player Properties that are set when the form loads.
PlayerControl.settings.autoStart = True PlayerControl.settings.volume = VolumeBar.Value PlayerControl.settings.balance = BalanceBar.Value PlayerControl.settings.enableErrorDialogs = False PlayerControl.enableContextMenu = False
Well, that about wraps it up. Remember that when you add a Media Player to your form, Visual Studio will create two DLL files that are vital to run the program: AxInterop.WMPLib.dll and Interop.WMPLib.dll. Make sure that these are included wherever the main program is.
Now, if you do not want to use Windows Media Player, there is also a Quick Time Control that's available to use (found in the COM Components). However, Quick Time Player must be installed in order to use it.
General
News
Question
Answer
Joke
Rant
Admin
|
PermaLink |
Privacy |
Terms of Use
Last Updated: 17 Aug 2007 Editor: Sean Ewington |
Copyright 2006 by MatrixCoder Everything else Copyright © CodeProject, 1999-2009 Web16 | Advertise on the Code Project |