# Visual Basic > Visual Basic FAQs >  Classic VB - How can I play MP3/WAV songs?

## Harsh Gupta

The simplest method to play MP3 or WAV songs through a VB project is to use the *Windows Media Player Control*. In fact, you can use this control to play all file formats supported by the Windows Media Player, the default player in your Windows OS. But let's now focus on MP3 and WAV !  :Smilie: 

(See below for alternative methods using DirectX, or sndPlaySound, or MMControl)


You can access this control through Project->Components (or press CTRL+T to open the Components Dialog box). Under the Controls tab, tick the box next to "Windows Media Player".

Place one instance of this control on your form of the project (I renamed it to mp3 in the attached project). The 4 basic controls you need to play a song is PLAY it, STOP it, PAUSE it, and of course to start with you must OPEN it in the control.

The *Methods* used by this control for the above-mentioned operations are:



> 1 -> .Play            to PLAY a song;
> 2 -> .Stop           to STOP a song;
> 3 -> .Pause          to PAUSE a song; and
> 4 -> .FileName      to OPEN a song in the Media Player control.


Various other useful *Methods* supported by this control are as follows:



> (a) .Cancel -> Cancels the Open method before the file completes opening. 
> 
> (b) .FastForward -> Scans rapidly forward through the current clip. 
> 
> (c) .FastReverse -> Scans rapidly backward through the current clip. 
> 
> (d) .GetCodecDescription -> Retrieves the descriptive name of the given                         codec. 
> 
> (e) .GetCurrentEntry -> Retrieves the current clip being played.
> ...


Please note that this is not the full list, these are just the ones which are most useful in this situation.


There are several *Properties* available to you, which you can use to determine (or set) features of the player or sound file. These include:



> (a) .AllowScan -> _Read/Write_ Sets or retrieves a value specifying whether scanning is enabled for files that support scanning (fast-forwarding and rewinding). 
> 
> (b) .AutoRewind -> _Read/Write_ Sets or retrieves a value specifying whether the Windows Media Player control automatically returns to the clip's starting point after the clip finishes playing or has otherwise stopped. 
> 
> (c) .AutoStart -> _Read/Write_ Sets or retrieves a value specifying whether to start playing the clip automatically. 
> 
> (d) .Balance -> _Read/Write_ Sets or retrieves a value indicating the stereo balance. 
> 
> (e) .Bandwidth -> _Read-only_ Retrieves the bandwidth of the current clip in bits per second. 
> ...


If you still have any queries, please use the following link for more documentation on the Windows Media Player Control: MSDN



The project attached is an example of how you can use the control.  Hope it helps you !!!!
NB: to select the file to play, this example uses the Common Dialog control.

----------


## Jacob Roman

By using DirectShow, which is part of DirectX, you can play Mp3's and other media types, even videos. This is another simple way to play media. The major difference between the Windows Media Player control and DirectShow is that DirectShow gives you more control over your media. Like for example, the speed of it, which in effect changes the pitch. You can have it play in slow motion, giving it that errie and evil kind of sound (speed 75 is ideal for this), or you can have it play in fast motion, making it sound like a chipmunk. To access DirectShow, all you do is go up in the menu to Project > References... and add Active Movie Control Type Library into your references.

Then add a module into your project and put this code in:


VB Code:
Option Explicit
 Private Const MAX_VOLUME As Long = 100
Private Const MAX_BALANCE As Long = 100
Private Const MAX_SPEED As Long = 226
 Public DirectShow_Event As IMediaEvent
Public DirectShow_Control As IMediaControl
Public DirectShow_Position As IMediaPosition
Public DirectShow_Audio As IBasicAudio
 Public Function DirectShow_Load_Media(File_Name As String) As Boolean
     On Error GoTo Error_Handler
                
         If Right(File_Name, 4) = ".mp3" Then
             Set DirectShow_Control = New FilgraphManager
            DirectShow_Control.RenderFile (File_Name)
             Set DirectShow_Audio = DirectShow_Control
             DirectShow_Audio.Volume = 0
            DirectShow_Audio.Balance = 0
             Set DirectShow_Event = DirectShow_Control
            Set DirectShow_Position = DirectShow_Control
             DirectShow_Position.Rate = 1
             DirectShow_Position.CurrentPosition = 0
         Else
             GoTo Error_Handler
         End If
     DirectShow_Load_Media = True
     Exit Function
 Error_Handler:
     DirectShow_Load_Media = False
 End Function
  Public Function DirectShow_Play() As Boolean
     On Error GoTo Error_Handler
     DirectShow_Control.Run
     DirectShow_Play = True
     Exit Function
 Error_Handler:
     DirectShow_Play = False
 End Function
  Public Function DirectShow_Stop() As Boolean
     On Error GoTo Error_Handler
     DirectShow_Control.Stop
     DirectShow_Position.CurrentPosition = 0
     DirectShow_Stop = True
     Exit Function
 Error_Handler:
     DirectShow_Stop = False
 End Function
  Public Function DirectShow_Pause() As Boolean
     On Error GoTo Error_Handler
     DirectShow_Control.Stop
     DirectShow_Pause = True
     Exit Function
 Error_Handler:
     DirectShow_Pause = False
 End Function
  Public Function DirectShow_Volume(ByVal Volume As Long) As Boolean
     On Error GoTo Error_Handler
     If Volume >= MAX_VOLUME Then Volume = MAX_VOLUME
     If Volume <= 0 Then Volume = 0
     DirectShow_Audio.Volume = (Volume * MAX_VOLUME) - 10000
     DirectShow_Volume = True
     Exit Function
 Error_Handler:
     DirectShow_Volume = False
 End Function
  Public Function DirectShow_Balance(ByVal Balance As Long) As Boolean
     On Error GoTo Error_Handler
    
    If Balance >= MAX_BALANCE Then Balance = MAX_BALANCE
    
    If Balance <= -MAX_BALANCE Then Balance = -MAX_BALANCE
    
    DirectShow_Audio.Balance = Balance * MAX_BALANCE
    
    DirectShow_Balance = True
    
    Exit Function
    
Error_Handler:
     DirectShow_Balance = False
 End Function
  Public Function DirectShow_Speed(ByVal Speed As Single) As Boolean
     On Error GoTo Error_Handler
     If Speed >= MAX_SPEED Then Speed = MAX_SPEED
    
    If Speed <= 0 Then Speed = 0
     DirectShow_Position.Rate = Speed / 100
     DirectShow_Speed = True
     Exit Function
 Error_Handler:
     DirectShow_Speed = False
 End Function
  Public Function DirectShow_Set_Position(ByVal Hours As Long, ByVal Minutes As Long, ByVal Seconds As Long, Milliseconds As Single) As Boolean
     On Error GoTo Error_Handler
     Dim Max_Position As Single
     Dim Position As Double
     Dim Decimal_Milliseconds As Single
     'Keep minutes within range
         Minutes = Minutes Mod 60
     'Keep seconds within range
         Seconds = Seconds Mod 60
     'Keep milliseconds within range and keep decimal
         Decimal_Milliseconds = Milliseconds - Int(Milliseconds)
         Milliseconds = Milliseconds Mod 1000
         Milliseconds = Milliseconds + Decimal_Milliseconds
     'Convert Minutes & Seconds to Position time
         Position = (Hours * 3600) + (Minutes * 60) + Seconds + (Milliseconds * 0.001)
     Max_Position = DirectShow_Position.StopTime
     If Position >= Max_Position Then
         Position = 0
         GoTo Error_Handler
     End If
     If Position <= 0 Then
         Position = 0
         GoTo Error_Handler
     End If
     DirectShow_Position.CurrentPosition = Position
     DirectShow_Set_Position = True
     Exit Function
 Error_Handler:
     DirectShow_Set_Position = False
 End Function
  Public Function DirectShow_End() As Boolean
     On Error GoTo Error_Handler
     If DirectShow_Loop = False Then
         If DirectShow_Position.CurrentPosition >= DirectShow_Position.StopTime Then DirectShow_Stop
     End If
     DirectShow_End = True
     Exit Function
 Error_Handler:
     DirectShow_End = False
 End Function
  Public Function DirectShow_Loop() As Boolean
     On Error GoTo Error_Handler
     If DirectShow_Position.CurrentPosition >= DirectShow_Position.StopTime Then
         DirectShow_Position.CurrentPosition = 0
     End If
     DirectShow_Loop = True
     Exit Function
 Error_Handler:
     DirectShow_Loop = False
 End Function
  Public Sub DirectShow_Shutdown()
     Set DirectShow_Audio = Nothing
    Set DirectShow_Event = Nothing
    Set DirectShow_Control = Nothing
    Set DirectShow_Position = Nothing
 End Sub

As you can see, all I setup was some very simple functions for Play, Stop, Pause, Loop, Speed, Volume, Balance, Position, etc. Now in your Form, put this code in, and you are all set:


VB Code:
Option Explicit
 Private Sub Form_Activate()
    
    'Change the file path of your mp3 in here.
     DirectShow_Load_Media "C:\Jacob's Stuff\Music\Mp3's\Black Metal\Dimmu Borgir - Enthrone Darkness Triumphant - 10 - A Succubus In Rapture.mp3"
    
    'You don't need to do this part, but it's nice to be
    'able to control it.
    '------------------------------------------------
    DirectShow_Volume 100
    DirectShow_Balance 0
    DirectShow_Speed 100
    DirectShow_Set_Position 0, 0, 0, 0
    '------------------------------------------------
     DirectShow_Play
 End Sub
 Private Sub Form_Unload(Cancel As Integer)
      DirectShow_Shutdown
 End Sub

That's all you really need. DirectShow_Load_Media and DirectShow_Play. It's that simple.  :Wink: 

To play videos, here is a sample project: Video Player

Feel free to add to my DirectShow engine. I only added the basic functions that is perfect for making your own media player.

----------


## Jenova

Two other ways that you might find helpful are the sndPlaySound API and the MMControl that can be added in VB.

*sndPlaySound*

This function plays the sound specified by 'lpszPlaySound' parameter. This function is limited to .wav files. An example of use is shown below.


VB Code:
Option Explicit
 
' Plays the sound specified by
' lpszPlaySound. This function is
' limited to .wav files.
Private Declare Function sndPlaySound _
    Lib "winmm.dll" _
    Alias "sndPlaySoundA" ( _
        ByVal lpszSoundName As String, _
        ByVal uFlags As Long) _
        As Long
        
Private Const SND_NOWAIT    As Long = &H2000                    ' Don't wait if the driver is busy
Private Const SND_SYNC      As Long = &H0                       ' Play synchronously (default)
Private Const SND_FLAGS     As Long = SND_SYNC Or SND_NOWAIT    ' Combination of two constants above
 Private Sub Command1_Click()
    ' Call sndPlaySound to play our file
    Call sndPlaySound(App.Path & "\Chimes.wav", SND_FLAGS)
End Sub

*MMControl*

This is a control that can be added through Visual Basic 6.0 by going to Project > Components and selecting 'Microsoft Multimedia Control 6.0'. This control can play a mixture of sound types and is of more use than sndPlaySound if you are playing music through VB. An example on using this is below:


VB Code:
Private Sub Form_Load()
    With MMControl1
        .FileName = App.Path & "\Sound.wma"     ' Set the file to be played
        .Command = "Open"                       ' Open the file
        .Command = "Play"                       ' Play the file
    End With
End Sub

Hope this is of some help  :wave:

----------

