Click here to Skip to main content
15,885,929 members
Articles / Programming Languages / Visual Basic

The Ultimate Media Player

Rate me:
Please Sign up or sign in to vote.
2.42/5 (34 votes)
17 Aug 2007CPOL1 min read 203.9K   15.2K   62   36
Have 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.

Sample Image - Screen1.jpg

Introduction

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.

Begin

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

Sample Image

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 TrackBar, which are not as easy as they look.

Duration

VB
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

TrackBar

VB
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

There are a few Media Player properties that are set when the form loads:

VB
PlayerControl.settings.autoStart = True
PlayerControl.settings.volume = VolumeBar.Value
PlayerControl.settings.balance = BalanceBar.Value
PlayerControl.settings.enableErrorDialogs = False
PlayerControl.enableContextMenu = False

Points of Interest

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.

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.

License

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


Written By
Software Developer
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralRe: cool controls Pin
MatrixCoder9-Jul-07 8:14
MatrixCoder9-Jul-07 8:14 
GeneralRe: cool controls Pin
Bosko19789-Jul-07 8:51
Bosko19789-Jul-07 8:51 
GeneralRe: cool controls Pin
MatrixCoder12-Jul-07 12:13
MatrixCoder12-Jul-07 12:13 
Questionhow did you override the WMP's full scrren controls? Pin
banerjen7-Jul-07 8:35
banerjen7-Jul-07 8:35 
AnswerRe: how did you override the WMP's full scrren controls? Pin
MatrixCoder7-Jul-07 12:30
MatrixCoder7-Jul-07 12:30 
GeneralVersion 2003 plzz Pin
steve shafa18-Dec-06 7:30
steve shafa18-Dec-06 7:30 
GeneralRe: Version 2003 plzz Pin
MatrixCoder1-Jan-07 22:14
MatrixCoder1-Jan-07 22:14 
GeneralRe: Version 2003 plzz Pin
Tony Cooper10-Mar-09 20:43
Tony Cooper10-Mar-09 20:43 
QuestionOverstatement? Pin
Ri Qen-Sin21-Nov-06 20:33
Ri Qen-Sin21-Nov-06 20:33 
AnswerRe: Overstatement? Pin
toxcct21-Nov-06 23:32
toxcct21-Nov-06 23:32 
AnswerRe: Overstatement? Pin
JumboSized o.O20-Jul-07 19:43
JumboSized o.O20-Jul-07 19:43 

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.