Make your own Mp3 Player!
Every wanted to make your own Mp3 Player instead of using WinAmp etc...This is your chance. Read this tuturial and find out how you can make your own Mp3 Player.
Original Author: Kevin
Code
Welcome! 1) Start VB The first thing we'll have On Error Resume Next Line1: Prevents the program Now that we're playing a On Error Resume Next Line1: Prevents the program I don't know what you think, On Error Resume Next Line1: Prevents the program Every audio player contains On Error Resume Next Line1: Prevents the program Well, If a player contains a On Error Resume Next Line1: Prevents the program Well, If a player contains a On Error Resume Next Line1: Prevents the program Now that the PlayBack Function ConvertTime(i As Integer) Line1: This is the name of Now that you've placed the If MediaPlayer1.PlayState = mpPlaying Then Line1: Are we playing Note: The time of the file We have playback controls, a On Error Resume Next Line1: Prevents the program We're not ready with our On Error Resume next Line1: Prevents the program Earlier in this tutorial I Dim a As Integer, b As Integer Line1: Make 2 variables Well, Just to make sure a timer1.interval = 1000 Line1: Set the interval of Congratulations! You're If you want to have an If MediaPlayer1.Mute = True Then Line1: If the sound is muted Your audio player contains a On Error Resume Next Line1: Prevents the program Here's the code to remove If List1.ListIndex > -1 Then Line1: If the PlayList's Here's the code to move the on error resume next Line1: Prevents the program Here's the code to move the on error resume next Line1: Prevents the program Here's the code to clear the ask = MsgBox("Do you want to clear your list ?", vbQuestion + vbYesNo, "Confirm") Line1: We show a Msgbox Here's the code to save your On Error Resume Next Line1: Prevents the program Here's the code to open a On Error Resume Next Line1: Prevents the program Here's the code to repeat if check1.value = 1 then Line1: If the checkbox is Here's the code to stop if check2.value = 1 then Line1: If the checkbox is Here's the code to play if check3.value = 1 then Line1: If the checkbox is A very important thing that filename.text = Line1: Put the filename of If Trim(filename.Text) <> "" Then Line1: Check if the textbox I hope you know how you can Since English is not my Enjoy your own Mp3 Player !! KEVIN,
In the tutorial I will try to explain how to make your own mp3 player using the
Media Player control.
It will be a fully featured audio player with a playlist and options like:
"Repeat" and "Random Play".
Gray Text = Things to do!
Blue Text = Source Code
Red Text = Information about the code
etc...
2) Press CTRL + T
3) Insert: Windows Media Player Control, Microsoft Common Dialog Control and Microsoft
Windows Common Controls.
4) Put on your form: 6 Command buttons (Playback), A Label (Time Label), A
Textbox, 2 Sliders (Volume and seekbar), A Listbox (PLS)
5) Set the Media Player Control Invisible.
to do is making a "Open File" button. This way a user is able to
choose an audio file.
Select one of the 6 command
buttons you've created earlier in this tutorial and put the following code on
it.
CommonDialog1.Filter = "Audio Files|*.wav;*.mid;*.mp3;mp2;*.mod|"
CommonDialog1.Flags = cdlOFNHideReadOnly
CommonDialog1.CancelError = True
CommonDialog1.DialogTitle = "Choose an mediafile to open"
CommonDialog1.FileName = ""
CommonDialog1.ShowOpen
List1.AddItem CommonDialog1.FileName
List1.ListIndex = List1.ListIndex + 1
MediaPlayer1.FileName = CommonDialog1.FileName
Text1.Text = CommonDialog1.Filename
from giving errors
Line2: It will display only mp3,
wav, mid files etc...
Line3: This will remove the "Read Only" checkbox at the end of the open
dialog
Line4: This will handle the error you get if you click the cancel button
Line5: This will set the text between the "..." on the titlebar of the
open dialog
Line5: This will show the open dialog
Line6: An Empty Line!
Line7: This will put the file
you've selected in the listbox (Used as playlist Control)
Line8: This will select the file you've chosen in the PlayList
Line9: This tells the Media Player Control which file it needs to play
Line10: Put the name of the file we're going to play in the "Filename"
textbox.
file we can put other code in our player such as "Play Selected
Track"...
Select one of the 5
command buttons you've created earlier in this tutorial and put the following
code on it.
MediaPlayer1.FileName = List1.Text
Mediaplayer1.Play
text1.text =
mediaplayer1.filename
from giving errors
Line2: The first line tells the Media Player Control the filename. In this case
the selected item in the PlayList.
Line3: The second line tells the control that it must play the filename which
was set above
Line4: Put the name of the file we're going to play in the "Filename"
textbox.
But I'd like to be able to Pause the playing track :-)
Select one of the 4 command
buttons you've created earlier in this tutorial and put the following code on
it.
If MediaPlayer1.PlayState = mpPlaying Then
MediaPlayer1.Pause
Else
MediaPlayer1.Play
End If
from giving errors
Line2: If the Media Control is
playing a file then...
Line3: Pause the playing file!
Line4: Else. If it's not playing a file. So it's either stoped or already
paused...
Line5: Play the file which is still in the memory of the Media Player Control.
a stop button. Well here's the code for it...
Select one of the 3 command
buttons you've created earlier in this tutorial and put the following code on
it.
Mediaplayer1.stop
from giving errors
Line2: Stop playing the current
file.
PlayList I'd like to be able to switch between my tracks. Here's the
"Previous Track" code.
Select one of the 2 command
buttons you've created earlier in this tutorial and put the following code on
it.
List1.ListIndex = List1.ListIndex - 1
MediaPlayer1.FileName = List1.Text
MediaPlayer1.Play
text1.text =
mediaplayer1.filename
from giving errors
Line2: This will go one item back from the selected item.
Line3:
This tells the Media Player Control which file it needs to play
Line4: Say to the Media Player control : Play the file I've set above!
Line5: Put the name of the file we're going to play in the "Filename"
textbox.
PlayList I'd like to be able to switch between my tracks. Here's the "Next
Track" code.
Select the last
commandbutton that's left and put the following code on it.
List1.ListIndex = List1.ListIndex + 1
MediaPlayer1.FileName = List1.Text
MediaPlayer1.Play
text1.text =
mediaplayer1.filename
from giving errors
Line2: This will go one item further than the selected item.
Line3: Tell the Media Player control which file it needs to load. In this case the
selected item in the PlayList.
Line4: Play the file!
Line5: Put the name of the file we're going to play in the "Filename"
textbox.
controls are done we can add some more code to our player.
Put the following code in your form. It's a function which is called in the next Sub.
Secs = i Mod 60
Mins = Int(i / 60) Mod 60
Hours = Int(i / 3600)
If Secs < 10 Then Secs = "0" & Secs
If Mins < 10 Then Mins = "0" & Mins
ConvertTime = Hours & ":" & Mins & ":" & Secs
End Function
the function and the statement which tells VB it's a function.
Line2: We make a variable named secs which converts I (which is specified when
the function is called) to seconds
Line3: We make a variable named mins which converts I to minutes
Line4: We make a variable which hours which converts I to hours
Line5: If the number of seconds is less the then we put a 0 before the seconds
like this: 01,02,03 etc...
Line6: The same as above but now with minutes
Line7: Now we update the sub with the output format which you can get in the
next sub.
Line8: The end of this function :-(
above function in your code we're ready to call it.
Insert a Timer
Control in your project and put the following code on it. Don't forget to set
it's interval to: "1000".
Interval="1000": This means that the timer will update the Timer
Window every second.
Label1.Caption = ConvertTime(Round(MediaPlayer1.CurrentPosition, 0)) & " / " & ConvertTime(Round(MediaPlayer1.Duration, 0))
Else
Label1.Caption = "00:00:00 / 0:00:00"
End If
a file ?
Line2: If we are playing a file update the Timer Window every second with the
time of the file you're playing
Line3: If we are not playing a file...
Line4: Put the following text in your Timer Window: "00:00:00 /
00:00:00"
Line5: Stop our check function
will be showed like this: "03:46:13 / 04:12:34"
The "03:46:13" indicates the current playing time and
"04:12:34" indicates the total time of the file.
PlayList and a Timer Window but we don't have a seekbar yet!
Insert a Timer Control in
your project and put the following code on it. Don't forget to set it's interval
to: "1000".
Interval="1000": This means that the timer will update the Seekbar
control's position every second.
Slider1.Max = MediaPlayer1.Duration
Slider1.Value = MediaPlayer1.CurrentPosition
from giving errors
Line2: The Maximum value of the slider is the duration of the file we're playing
Line3: Update the position of the slider to the current position of the Media
Player Control
slider yet because we want to change the position of the track when moving
the slider.
Put the following code in
the Slider1_Scroll() function.
MediaPlayer1.CurrentPosition = Slider1.Value
from ginving errors
Line2: Update the position of the file we're playing to the slider's position.
Easy huh?
said you had to put 2 slider control's on your form.
Well, We've already used 1 slider so now we're going to use the second one. This
one is for the Volume Control.
Put the following code in
the Slider2_Scroll() function and set the Max Value of the slider to: "2500"
Dim d, c
c = Slider2.Value - 2500
MediaPlayer1.Volume = c
b = Slider2.Min
a = Slider2.Value
called "a" and "b" and say to VB they are an Integer.
Line2: Set variable "D" and "C"
Line3: Update variable c with Slider2's value - 2500
Line4: Set the volume of the Media Player control from Slider2's value
Line5: Variable "b" is The Minium Value of Slider 2
Line6: Variable "a" is the Maximum Value of Slider 2
few things you need to place this code in the Form itself.
Put the following code in
the Form_Load function.
timer2.interval = 1000
Slider2.Max = 2500
Timer1 to "1000" in case you forgot :-)
Line2: Set the interval of Timer2 to "1000" in case you forgot :-)
Line3: Set the maximum value of the volume slider to "2500" in case
you forgot :-)
Audio Player is now ready!
You will find some more useful code for it below...
"Mute" option in your Audio Player use the following code.
MediaPlayer1.Mute = False
Else
MediaPlayer1.Mute = True
End If
then...
Line2: UnMute the sound!
Line3: Mute the sound because it's not muted yet!
Line4: Stop the check function
PlayList but why would you need a PlayList if it cannot contain more than 1
file.
Here's the code to add files to your PlayList.
CommonDialog1.Filter = "Audio Files|*.wav;*.mid;*.mp3;mp2;*.mod|"
CommonDialog1.Flags = cdlOFNHideReadOnly
CommonDialog1.CancelError = True
CommonDialog1.DialogTitle = "Add File"
CommonDialog1.FileName = ""
CommonDialog1.ShowOpen
List1.AddItem CommonDialog1.FileName
from giving errors
Line2: It will display only mp3,
wav, mid files etc...
Line3: This will remove the "Read Only" checkbox at the end of the open
dialog
Line4: This will handle the error you get if you click the cancel button
Line5: This will set the text between the "..." on the titlebar of the
open dialog
Line6: This will clear the previous selected Filename in the Open Dialog
Line7: This will show the open dialog
Line8: An Empty Line!
Line9: Add the selected file to our PlayList
the selected item from your PlayList...
On Error Resume Next
If list1.text = mediaplayer1.filename then msgbox "You can't remove the
file you're playing":exit sub
List1.RemoveItem List1.ListIndex
End If
index is bigger than -1 go on. This means that there's actually something.
Line2: Prevents the program
from giving errors
Line3: If the item you're trying to remove is the current playing item show a
msgbox telling the user the item cannot be removed. Also stop the code so it
won't be removed. This code
is needed because it will keep your PlayList working. If you remove this line
the player can't determine anymore which file was playing and it won't go next
anymore from the file you were playing. It starts the list again.
Line4: Remove the selected line from the PlayList
Line5: Stop our check function
selected item in your PlayList one line up.
Dim nItem As Integer
With lstItems
If List1.ListIndex < 0 Then Exit Sub
nItem = List1.ListIndex
If nItem = 0 Then Exit Sub
List1.AddItem List1.Text, nItem - 1
List1.RemoveItem nItem + 1
List1.Selected(nItem - 1) = True
End With
from giving errors
Line2: Set 'NItem" as variable and tell VB that it's an Integer
Line3: Tell VB that we're working with the List Items
Line4: If the List Index is smaller that 0 it can't move up anymore so STOP the
code
Line5: Tell VB that our variable the index of the selected item in our PlayList
is
Line6: If the index is 0 STOP because the line can't move up further.
Line7: Add the listindex text one item before the selected item
Line8: Remove the current selected item
Line9: Select the line we've just moved one line up
Line10: Tell VB we're not working with the List Items anymore
selected item in your PlayList one line down.
Dim nItem As Integer
With lstItems
If List1.ListIndex < 0 Then Exit Sub
nItem = List1.ListIndex
If nItem = List1.ListCount - 1 Then Exit Sub
List1.AddItem List1.Text, nItem + 2
List1.RemoveItem nItem
List1.Selected(nItem + 1) = True
End With
from giving errors
Line2: Set 'NItem" as variable and tell VB that it's an Integer
Line3: Tell VB that we're working with the List Items
Line4: If the List Index is smaller that 0 it can't move up anymore so STOP the
code
Line5: Tell VB that our variable the index of the selected item in our PlayList
is
Line6: If the index is at the end of the PlayList it can't go down anymore so
STOP the code
Line7: Add the listindex text one item after the current item. The listbox works
a bit strange so line1 is actually line2
Line8: Remove the current selected item
Line9: Select the line we've just moved one line down
Line10: Tell VB we're not working with the List Items anymore
whole PlayList...
If ask = vbYes Then
List1.clear
Else
End If
which asks the user if he/she really want to clear the PlayList
Line2: If they answer YES...
Line3: The PlayList will be cleared
Line4: If they answer something else...NO in this case!
Line5: Don't do anything
Line6: Stop the check function
PlayList...
CommonDialog1.Filter = "PlayList File (M3u)|*.m3u|PlayList File
(Pls)|*.pls"
CommonDialog1.DialogTitle = "Save List"
CommonDialog1.Flags = cdlOFNHideReadOnly
CommonDialog1.ShowSave
CommonDialog1.CancelError = True
Open CommonDialog1.FileName For Output As #1
For X = 0 To List1.ListCount - 1
Print #1, List1.List(X)
Next X
Close #1
from giving errors
Line2: It can only save your PlayList as: "M3u or Pls"
Line3: This will set the text between the "..." on the titlebar of the
save dialog
Line4: This will remove the "Read Only" checkbox at the end of the
save dialog
Line5: This will show the save dialog
Line6: This will handle the error you get if you click the cancel button
Line7: An empty line!
Line8: Open the selected file and place it in a variable called #1
Line9: Make sure we save the whole PlayList
Line10: Write the contents to the selected file
Line11: Go on till we have saved all items
Line12: Close the file
previously saved PlayList file...
On Error GoTo err
Close #1
Dim X
OpenFile:
CommonDialog1.Filter = "All Supported|*.m3u;*.pls|"
CommonDialog1.DialogTitle = "Open List"
CommonDialog1.Flags = cdlOFNHideReadOnly
CommonDialog1.ShowOpen
CommonDialog1.CancelError = True
Open CommonDialog1.FileName For Input As #1
List1.clear
Do
Input #1, X
List1.AddItem (X)
Loop
Close #1
err:
Exit Sub
from giving errors
Line2: If there's an error go the a sub called: "err"
Line3: Close all files so we don't get any errors while trying to open a file
Line4: A sub called "OpenFile
Line5: It will display only mp3,
wav, mid files etc...
Line6: This will set the text between the "..." on the titlebar of the
open dialog
Line7: This will remove the "Read Only" checkbox at the end of the
save dialog
Line8: This will show the open dialog
Line9: This will handle the error you get if you click the cancel button
Line10: Load the file that was selected into the memory as variable #1
Line11: Clear the list before adding new items in it
Line12: Do function
Line13: Load all contents into variable X
Line14: Add X to our PlayList
Line15: Gon on till we have placed all files in our PlayList
Line16: Close the files which was opened
Line17: Error Handler sub which is called above
Line18: Stop the code. If case there's an error
the current playing file...
Insert a checkbox on your
form. Add the following code in the "EndOfStream" function of the
Media Player control.
mediaplayer1.play
else
end if
checked...
Line2: Play the file again!
Line3: If it's not checked...
Line4: Stop our check function
playing after the current playing track...
Put a checkbox on your
form. Add the following code in the "EndOfStream" function of the
Media Player control.
mediaplayer1.stop
else
end if
checked...
Line2: Stop Playing!
Line3: If it's not checked...
Line4: Stop our check function
normal. After it's done it will start playing the next track in your PlayList.
Put a checkbox on your
form. Add the following code in the "EndOfStream" function of the
Media Player control.
list1.listindex = list1.listindex + 1
mediaplayer1.filename = list1.text
mediaplayer1.play
text1.text = mediaplayer1.filename
else
end if
checked...
Line2: Select the next line in the PlayList from the selected item.
Line3: Tell the Media Player Control that it needs to load the selected item in
the PlayList
Line4: Play the loaded file
Line5: Update our Filename Window with the current playing track
Line6: If it's not checked...
Line7: Stop our check function
you must do is the following...
You must make a function
that will select the current playing track every time it's done playing.
If you don't do this the player will play the next track from the item you've
selected and not from the file you're playing.
Put the following code in the "EndOfStream" function of the Media
Player control...
mediaplayer1.filename
the current played track in a textbox so we can look it up later.
Now add the following code
in the "_Change" function of the textbox called "filename".
For i = 0 To List1.ListCount - 1
If Left(List1.List(i), Len(Trim(filename.Text))) = Trim(filename.Text) Then
List1.Selected(i) = True
Else
List1.Selected(i) = False
End If
Next i
End If
contains some text
Line2: Search the whole list
Line3: Search the list for contents of the textbox
Line4: If it's found select the item that was found --> Now the player goes
next from this selected item. This was the playing item!
Line5: If it was not found don't select anything. But the file is always found
because you were playing it :-)
Line6: Stop check function
Line7: Go on till end of PlayList
Line8: Stop check function
make your own Mp3 Player after reading this tutorial.
Please, Send as much feedback as you want and DON'T
VORGET TO VOTE FOR MY WORK!
Some people are complaining about using the Media Player Control but I don't
care.
As long as we have fun with it it's good, and another great thing is that you
don't have to use WinAmp anymore :-)
Primary Language there can be some spelling faults in it.
Please, Report them and I'll fix it. Dutch is my primary language...
kevin_verp@hotpop.com
Loading Comments ...
Comments
No comments have been added for this post.
You must be logged in to make a comment.