Click here to Skip to main content
15,883,883 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

 
QuestionWill it run on PC with No Windows Media Player installed Pin
Foji5417-Sep-20 9:29
Foji5417-Sep-20 9:29 
QuestionOOP implementation Pin
sesquipidelian2-Mar-13 15:22
sesquipidelian2-Mar-13 15:22 
QuestionVOB File Format Pin
Muhammad Adnan Umar18-Dec-12 23:12
Muhammad Adnan Umar18-Dec-12 23:12 
Questionfps Pin
Member 39252646-Feb-12 3:11
Member 39252646-Feb-12 3:11 
GeneralA few problems Pin
offroaderdan9-Aug-09 5:38
offroaderdan9-Aug-09 5:38 
GeneralRe: A few problems Pin
MatrixCoder13-Aug-09 19:31
MatrixCoder13-Aug-09 19:31 
GeneralRe: A few problems Pin
offroaderdan27-Aug-09 0:49
offroaderdan27-Aug-09 0:49 
GeneralRe: A few problems Pin
offroaderdan27-Aug-09 3:10
offroaderdan27-Aug-09 3:10 
GeneralMy vote of 2 Pin
smton9-Jul-09 15:30
smton9-Jul-09 15:30 
Generalerror in wmp control [modified] Pin
tads17-Jun-09 6:09
tads17-Jun-09 6:09 
Questionavi files Pin
Juraj Chabada8-Oct-08 22:04
Juraj Chabada8-Oct-08 22:04 
GeneralMultimedia enabled context relative help Pin
coolestCoder10-Dec-07 1:42
coolestCoder10-Dec-07 1:42 
GeneralThanks Pin
aarti838-Oct-07 16:57
aarti838-Oct-07 16:57 
QuestionDoesn't work Pin
Yauri28-Sep-07 17:58
Yauri28-Sep-07 17:58 
AnswerRe: Doesn't work Pin
MatrixCoder3-Oct-07 11:34
MatrixCoder3-Oct-07 11:34 
Generalgood job, but doesn't work on Vista?! Pin
Infomax16-Sep-07 13:27
Infomax16-Sep-07 13:27 
GeneralRe: good job, but doesn't work on Vista?! Pin
MatrixCoder18-Sep-07 16:40
MatrixCoder18-Sep-07 16:40 
Sorry, I have not written this to work with Vista.

Trinity: Neo... nobody has ever done this before.
Neo: That's why it's going to work.


Static Caffeine

GeneralHow to make it work on Vista?! Pin
dulcie_liu30-May-09 6:01
dulcie_liu30-May-09 6:01 
GeneralButton problem Pin
flypk9121-Aug-07 11:12
flypk9121-Aug-07 11:12 
GeneralRe: Button problem Pin
MatrixCoder22-Aug-07 16:13
MatrixCoder22-Aug-07 16:13 
GeneralRe: Button problem Pin
flypk912-Sep-07 10:33
flypk912-Sep-07 10:33 
GeneralRe: Button problem Pin
MatrixCoder2-Sep-07 18:30
MatrixCoder2-Sep-07 18:30 
GeneralVery Cool but... Pin
slammers413-Aug-07 12:26
slammers413-Aug-07 12:26 
GeneralRe: Very Cool but... Pin
Yannou26-Oct-08 3:23
Yannou26-Oct-08 3:23 
Generalcool controls Pin
Bosko19789-Jul-07 3:57
Bosko19789-Jul-07 3:57 

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.