With sounds I mean songs, like mp3 songs. We can play songs in various ways. First one is a Windows media Player control inside the form. Second is using API i.e. MCI Send String. This is a very useful API which provides many multimedia features to the VB6 application, point is you must know how to use it.
Using WMP
To add the WMP component. Right Click the toolbox and open components. Scroll to the last and tick the Windows Media Player. See the image below for hint.
after this we are good to go. Add three buttons for Play Pause and Stop, a text box for path and add a WMP component as well, name them as following.
Set the tURL's text property to a media file's path ex - "D:/songs/a.mp3". Once you're done with that add the following code.
- cPlay - Play Button
- cPause - Pause Button
- cStop - Stop Button
- tURL - Text Box for getting URL of song.
- WMP - Windows media Player component.
Set the tURL's text property to a media file's path ex - "D:/songs/a.mp3". Once you're done with that add the following code.
Private Sub cPause_Click()
WMP.Controls.pause
End Sub
Private Sub cPlay_Click()
WMP.URL = tURL
WMP.Controls.play
End Sub
Private Sub cStop_Click()
WMP.Controls.stop
End Sub
Private Sub Form_Load()
WMP.settings.autoStart = False
End Sub
WMP.Controls.pause
End Sub
Private Sub cPlay_Click()
WMP.URL = tURL
WMP.Controls.play
End Sub
Private Sub cStop_Click()
WMP.Controls.stop
End Sub
Private Sub Form_Load()
WMP.settings.autoStart = False
End Sub
There are some other properties that will have to be changed. Change the caption of the Command Buttons that you just added to their Purpose.... and also in the form load event we are setting the autostart property of the WMP component to false because we don't want it to start automatically.
Additionally if you want to display the position and duration of the current song then you might use the following code.
Make sure you have a timer and a label to display the time.
Additionally if you want to display the position and duration of the current song then you might use the following code.
Make sure you have a timer and a label to display the time.
Private Sub Timer1_Timer()
On Error Resume Next
Label1 = WMP.Controls.currentPositionString + " / " + WMP.currentMedia.durationString
End Sub
On Error Resume Next
Label1 = WMP.Controls.currentPositionString + " / " + WMP.currentMedia.durationString
End Sub
Add the above code and run the app. One thing you may notice is that when you click the pause button and then the play button, the song starts from the beginning. This happens because we are telling the player to reset its URL everytime we press the Play button. So what is the solution?
We can first check if the song that is being played is same as the new link that has been provided, if they are same then there is no need of setting the URL, if they are different then the URL will be set. This can be done by replacing the cPlay_click code with the following:
We can first check if the song that is being played is same as the new link that has been provided, if they are same then there is no need of setting the URL, if they are different then the URL will be set. This can be done by replacing the cPlay_click code with the following:
Private Sub cPlay_Click()
If WMP.URL = tURL Then
WMP.Controls.play
Else
WMP.URL = tURL
WMP.Controls.play
End If
End Sub
If WMP.URL = tURL Then
WMP.Controls.play
Else
WMP.URL = tURL
WMP.Controls.play
End If
End Sub
The Final Code :
Private Sub cPause_Click()
WMP.Controls.pause
End Sub
Private Sub cPlay_Click()
If WMP.URL = tURL Then
WMP.Controls.play
Else
WMP.URL = tURL
WMP.Controls.play
End If
End Sub
Private Sub cStop_Click()
WMP.Controls.stop
End Sub
Private Sub Form_Load()
WMP.settings.autoStart = False
End Sub
Private Sub Timer1_Timer()
On Error Resume Next
Label1 = WMP.Controls.currentPositionString + " / " + WMP.currentMedia.durationString
End Sub
WMP.Controls.pause
End Sub
Private Sub cPlay_Click()
If WMP.URL = tURL Then
WMP.Controls.play
Else
WMP.URL = tURL
WMP.Controls.play
End If
End Sub
Private Sub cStop_Click()
WMP.Controls.stop
End Sub
Private Sub Form_Load()
WMP.settings.autoStart = False
End Sub
Private Sub Timer1_Timer()
On Error Resume Next
Label1 = WMP.Controls.currentPositionString + " / " + WMP.currentMedia.durationString
End Sub
Screen Shot
Project Files :
wmp.zip |