Results 1 to 3 of 3

Thread: Classic VB - How can I play MP3/WAV songs?

  1. #1

    Thread Starter
    Smitten by reality Harsh Gupta's Avatar
    Join Date
    Feb 2005
    Posts
    2,938

    Classic VB - How can I play MP3/WAV songs?

    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 !

    (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.

    (f) .GetMediaInfoString -> Retrieves show or clip information like that of ID3 tags.

    (g) .Next -> Jumps to the next clip in a playlist.

    (h) .Previous -> Jumps to the previous clip in a playlist.
    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.

    (f) .CanSeek -> Read-only Retrieves a value specifying whether the current file has the ability to seek to a specific time.

    (g) .CreationDate -> Read-only Retrieves a value specifying the date and time when the clip was created.

    (h) .CurrentPosition -> Read/Write Sets or retrieves a value representing the clip's current position, in seconds.

    (i) .CurrentState -> Read-only Specifies the playback file's current state: stopped, paused, or running.

    (j) .Duration -> Read-only Retrieves a value indicating the clip's playing time in seconds.

    (k) .Enabled -> Read/Write Sets or retrieves a value specifying whether the control is enabled.

    (l) .FileName -> Read/Write Sets or retrieves a value specifying the name of the clip to play.

    (m) .HasError -> Read-only Retrieves a value specifying whether the control currently has an error.

    (n) .Mute -> Read/Write Sets or retrieves a value indicating the current mute state of the Windows Media Player control.

    (o) .PlayCount -> Read/Write Sets or retrieves a value indicating the number of times a clip plays.

    (p) .PlayState -> Read-only Retrieves a value indicating the state of the Windows Media Player operation.

    (q) .Volume -> Read/Write Sets or retrieves a value specifying the volume, in hundredths of decibels.
    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.
    Attached Files Attached Files
    Last edited by si_the_geek; Apr 8th, 2007 at 02:41 PM.

  2. #2
    College Grad!!! Jacob Roman's Avatar
    Join Date
    Aug 2004
    Location
    Miami Beach, FL
    Posts
    5,339

    Another Way To Play Mp3's In VB

    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:
    1. Option Explicit
    2.  
    3. Private Const MAX_VOLUME As Long = 100
    4. Private Const MAX_BALANCE As Long = 100
    5. Private Const MAX_SPEED As Long = 226
    6.  
    7. Public DirectShow_Event As IMediaEvent
    8. Public DirectShow_Control As IMediaControl
    9. Public DirectShow_Position As IMediaPosition
    10. Public DirectShow_Audio As IBasicAudio
    11.  
    12. Public Function DirectShow_Load_Media(File_Name As String) As Boolean
    13.  
    14.     On Error GoTo Error_Handler
    15.                
    16.  
    17.         If Right(File_Name, 4) = ".mp3" Then
    18.  
    19.             Set DirectShow_Control = New FilgraphManager
    20.             DirectShow_Control.RenderFile (File_Name)
    21.  
    22.             Set DirectShow_Audio = DirectShow_Control
    23.  
    24.             DirectShow_Audio.Volume = 0
    25.             DirectShow_Audio.Balance = 0
    26.  
    27.             Set DirectShow_Event = DirectShow_Control
    28.             Set DirectShow_Position = DirectShow_Control
    29.  
    30.             DirectShow_Position.Rate = 1
    31.  
    32.             DirectShow_Position.CurrentPosition = 0
    33.  
    34.         Else
    35.  
    36.             GoTo Error_Handler
    37.  
    38.         End If
    39.  
    40.     DirectShow_Load_Media = True
    41.  
    42.     Exit Function
    43.  
    44. Error_Handler:
    45.  
    46.     DirectShow_Load_Media = False
    47.  
    48. End Function
    49.  
    50.  
    51. Public Function DirectShow_Play() As Boolean
    52.  
    53.     On Error GoTo Error_Handler
    54.  
    55.     DirectShow_Control.Run
    56.  
    57.     DirectShow_Play = True
    58.  
    59.     Exit Function
    60.  
    61. Error_Handler:
    62.  
    63.     DirectShow_Play = False
    64.  
    65. End Function
    66.  
    67.  
    68. Public Function DirectShow_Stop() As Boolean
    69.  
    70.     On Error GoTo Error_Handler
    71.  
    72.     DirectShow_Control.Stop
    73.  
    74.     DirectShow_Position.CurrentPosition = 0
    75.  
    76.     DirectShow_Stop = True
    77.  
    78.     Exit Function
    79.  
    80. Error_Handler:
    81.  
    82.     DirectShow_Stop = False
    83.  
    84. End Function
    85.  
    86.  
    87. Public Function DirectShow_Pause() As Boolean
    88.  
    89.     On Error GoTo Error_Handler
    90.  
    91.     DirectShow_Control.Stop
    92.  
    93.     DirectShow_Pause = True
    94.  
    95.     Exit Function
    96.  
    97. Error_Handler:
    98.  
    99.     DirectShow_Pause = False
    100.  
    101. End Function
    102.  
    103.  
    104. Public Function DirectShow_Volume(ByVal Volume As Long) As Boolean
    105.  
    106.     On Error GoTo Error_Handler
    107.  
    108.     If Volume >= MAX_VOLUME Then Volume = MAX_VOLUME
    109.  
    110.     If Volume <= 0 Then Volume = 0
    111.  
    112.     DirectShow_Audio.Volume = (Volume * MAX_VOLUME) - 10000
    113.  
    114.     DirectShow_Volume = True
    115.  
    116.     Exit Function
    117.  
    118. Error_Handler:
    119.  
    120.     DirectShow_Volume = False
    121.  
    122. End Function
    123.  
    124.  
    125. Public Function DirectShow_Balance(ByVal Balance As Long) As Boolean
    126.  
    127.     On Error GoTo Error_Handler
    128.    
    129.     If Balance >= MAX_BALANCE Then Balance = MAX_BALANCE
    130.    
    131.     If Balance <= -MAX_BALANCE Then Balance = -MAX_BALANCE
    132.    
    133.     DirectShow_Audio.Balance = Balance * MAX_BALANCE
    134.    
    135.     DirectShow_Balance = True
    136.    
    137.     Exit Function
    138.    
    139. Error_Handler:
    140.  
    141.     DirectShow_Balance = False
    142.  
    143. End Function
    144.  
    145.  
    146. Public Function DirectShow_Speed(ByVal Speed As Single) As Boolean
    147.  
    148.     On Error GoTo Error_Handler
    149.  
    150.     If Speed >= MAX_SPEED Then Speed = MAX_SPEED
    151.    
    152.     If Speed <= 0 Then Speed = 0
    153.  
    154.     DirectShow_Position.Rate = Speed / 100
    155.  
    156.     DirectShow_Speed = True
    157.  
    158.     Exit Function
    159.  
    160. Error_Handler:
    161.  
    162.     DirectShow_Speed = False
    163.  
    164. End Function
    165.  
    166.  
    167. Public Function DirectShow_Set_Position(ByVal Hours As Long, ByVal Minutes As Long, ByVal Seconds As Long, Milliseconds As Single) As Boolean
    168.  
    169.     On Error GoTo Error_Handler
    170.  
    171.     Dim Max_Position As Single
    172.  
    173.     Dim Position As Double
    174.  
    175.     Dim Decimal_Milliseconds As Single
    176.  
    177.     'Keep minutes within range
    178.  
    179.         Minutes = Minutes Mod 60
    180.  
    181.     'Keep seconds within range
    182.  
    183.         Seconds = Seconds Mod 60
    184.  
    185.     'Keep milliseconds within range and keep decimal
    186.  
    187.         Decimal_Milliseconds = Milliseconds - Int(Milliseconds)
    188.  
    189.         Milliseconds = Milliseconds Mod 1000
    190.  
    191.         Milliseconds = Milliseconds + Decimal_Milliseconds
    192.  
    193.     'Convert Minutes & Seconds to Position time
    194.  
    195.         Position = (Hours * 3600) + (Minutes * 60) + Seconds + (Milliseconds * 0.001)
    196.  
    197.     Max_Position = DirectShow_Position.StopTime
    198.  
    199.     If Position >= Max_Position Then
    200.  
    201.         Position = 0
    202.  
    203.         GoTo Error_Handler
    204.  
    205.     End If
    206.  
    207.     If Position <= 0 Then
    208.  
    209.         Position = 0
    210.  
    211.         GoTo Error_Handler
    212.  
    213.     End If
    214.  
    215.     DirectShow_Position.CurrentPosition = Position
    216.  
    217.     DirectShow_Set_Position = True
    218.  
    219.     Exit Function
    220.  
    221. Error_Handler:
    222.  
    223.     DirectShow_Set_Position = False
    224.  
    225. End Function
    226.  
    227.  
    228. Public Function DirectShow_End() As Boolean
    229.  
    230.     On Error GoTo Error_Handler
    231.  
    232.     If DirectShow_Loop = False Then
    233.  
    234.         If DirectShow_Position.CurrentPosition >= DirectShow_Position.StopTime Then DirectShow_Stop
    235.  
    236.     End If
    237.  
    238.     DirectShow_End = True
    239.  
    240.     Exit Function
    241.  
    242. Error_Handler:
    243.  
    244.     DirectShow_End = False
    245.  
    246. End Function
    247.  
    248.  
    249. Public Function DirectShow_Loop() As Boolean
    250.  
    251.     On Error GoTo Error_Handler
    252.  
    253.     If DirectShow_Position.CurrentPosition >= DirectShow_Position.StopTime Then
    254.  
    255.         DirectShow_Position.CurrentPosition = 0
    256.  
    257.     End If
    258.  
    259.     DirectShow_Loop = True
    260.  
    261.     Exit Function
    262.  
    263. Error_Handler:
    264.  
    265.     DirectShow_Loop = False
    266.  
    267. End Function
    268.  
    269.  
    270. Public Sub DirectShow_Shutdown()
    271.  
    272.     Set DirectShow_Audio = Nothing
    273.     Set DirectShow_Event = Nothing
    274.     Set DirectShow_Control = Nothing
    275.     Set DirectShow_Position = Nothing
    276.  
    277. 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:
    1. Option Explicit
    2.  
    3. Private Sub Form_Activate()
    4.    
    5.     'Change the file path of your mp3 in here.
    6.  
    7.     DirectShow_Load_Media "C:\Jacob's Stuff\Music\Mp3's\Black Metal\Dimmu Borgir - Enthrone Darkness Triumphant - 10 - A Succubus In Rapture.mp3"
    8.    
    9.     'You don't need to do this part, but it's nice to be
    10.     'able to control it.
    11.     '------------------------------------------------
    12.     DirectShow_Volume 100
    13.     DirectShow_Balance 0
    14.     DirectShow_Speed 100
    15.     DirectShow_Set_Position 0, 0, 0, 0
    16.     '------------------------------------------------
    17.  
    18.     DirectShow_Play
    19.  
    20. End Sub
    21.  
    22. Private Sub Form_Unload(Cancel As Integer)
    23.  
    24.      DirectShow_Shutdown
    25.  
    26. End Sub

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

    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.
    Last edited by Jacob Roman; Aug 27th, 2005 at 09:46 AM.

  3. #3
    Hyperactive Member Jenova's Avatar
    Join Date
    Feb 2006
    Location
    Googleplex
    Posts
    413

    ..and another way

    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:
    1. Option Explicit
    2.  
    3. ' Plays the sound specified by
    4. ' lpszPlaySound. This function is
    5. ' limited to .wav files.
    6. Private Declare Function sndPlaySound _
    7.     Lib "winmm.dll" _
    8.     Alias "sndPlaySoundA" ( _
    9.         ByVal lpszSoundName As String, _
    10.         ByVal uFlags As Long) _
    11.         As Long
    12.        
    13. Private Const SND_NOWAIT    As Long = &H2000                    ' Don't wait if the driver is busy
    14. Private Const SND_SYNC      As Long = &H0                       ' Play synchronously (default)
    15. Private Const SND_FLAGS     As Long = SND_SYNC Or SND_NOWAIT    ' Combination of two constants above
    16.  
    17. Private Sub Command1_Click()
    18.     ' Call sndPlaySound to play our file
    19.     Call sndPlaySound(App.Path & "\Chimes.wav", SND_FLAGS)
    20. 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:
    1. Private Sub Form_Load()
    2.     With MMControl1
    3.         .FileName = App.Path & "\Sound.wma"     ' Set the file to be played
    4.         .Command = "Open"                       ' Open the file
    5.         .Command = "Play"                       ' Play the file
    6.     End With
    7. End Sub

    Hope this is of some help
    Last edited by si_the_geek; Apr 8th, 2007 at 02:36 PM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width