Make a simple MP3 player with VB, and use the API function [mciSendString] to play MP3 music files easily. The following program realizes most of the routine operations of MP3 playback. The MP3 player with the size of 100KB can be easily made with a little modification.
Start VB program and put six command buttons, three labels, a general dialog box, a progress bar, a status bar and a timer on the form. Please refer to the attached drawings for the layout of the window book. The button "Open MP3 File" is used to open a dialog box to select MP3 file, and the other five buttons are Prue Play, Pause, Stop Play, Back and Back. Label 1 is used to indicate the current time of the song; Label2 is placed on the leftmost side of the time bar, and the Caption property is "00: 00"; Label 3 is located on the right side of the time bar, indicating the total length of the song. Now you can write code. First, declare the function in the General Declaration section of the form.
Private declaration function mcisendStringlib "winmm.dll" alias "mcisendStringa" (byval lpstr command is string, ByVal lpstrRetumString is string, ByVal uReturnLength is Long, ByVal hwndCallback is Long) is Long.
Mark mfn as a string
The following is the code for each object:
Private Sub-Form _load ()
mfn = " "
Play. Enabled = False
Time out. Enabled = False
Stop playing. Enabled = False
Come back. Enabled = False
Prey. Enabled = False
Timer 1. Enabled = False
Timer 1. Interval = 500
End joint
Private child open_Click ()
Go to error when error occurs.
Use CommonDialog 1
. CancelError = True
. Filter = "Music file | * .mp3*.wav*. In"
. Flags = cdlOFNNoChangeDir and cdlOFNPathMustExist
. Action = 1
Stopplay_Click' button can also initialize the device.
Mfn =。 FileName' The file name in this program cannot have spaces.
Form 1. Title =. filename
Play. Enabled = true
End with ...
Play _ Click
outlet connection
Error:
End joint
Private Sub-game _ Click ()
Continue to the next step when an error occurs.
As long as Dim t
T = mciSendString("open "+mfn, 0&, 0,0)' open' cannot be lost.
Dim ret As String * 128
t = mciSendString(" status "+MFN+" length ",ret, 128,0)
Displays the total length of the song.
ret = Left(ret,8)
If ret < > ""then
ProgressBar 1.min = 0
ProgressBar 1。 Max = Val(ret)
Label 3. Caption = Gettime(Val(ret))
If ... it will be over.
Play. Enabled = False
Time out. Enabled = true
Stop playing. Enabled = true
Come back. Enabled = true
Prey. Enabled = true
t = mciSendString(" status "+MFN+" mode ",ret, 128,0)
Get the current status of the device, whether it is playing or pausing.
ret = Left(ret,8)
StatusBar 1。 Panel (1). Text = ret
Display the playback status in the status bar
t = mciSendString(" play "+MFN+" form "+Str(progress bar 1。 Value), 0 & amp, 0, 0)
Let's play
Timer 1. Enabled = true
End joint
Private Sub pause_Click ()
t% = mciSendString("pause " + mfn,0 & amp, 0, 0)
Issue a moratorium
Play. Enabled = true
Time out. Enabled = False
Stop playing. Enabled = true
Come back. Enabled = False
Prey. Enabled = False
End joint
Private Sub stopplay_Click ()
t% = mciSendString("stop " + mfn,0 & amp, 0, 0)
t% = mciSendString("close " + mfn,0 & amp, 0, 0)
do bot play
Play. Enabled = true
Time out. Enabled = False
Stop playing. Enabled = False
Come back. Enabled = False
Prey. Enabled = False
End joint
Private Sub back_Click ()
t % = mciSendString(" play "+MFN+" from "+Str(progress bar 1。 Value-(ProgressBar 1. Max \ 10)), 0 & amp0, 0)' Jump back a little and play it again.
End joint
Private Sub prew_Click()
t % = mciSendString(" play "+MFN+" from "+Stri(progress bar 1。 Value+(ProgressBar 1. Max \ 10)), 0 & amp0; mp0, 0)' Jump forward for a short time and then play.
End joint
Private sub-timer 1_Timer ()
As long as Dim t
Dim ret As String * 128
t = mciSendString(" status "+MFN+" position ",ret,0,0)
Gets the current playback position.
ret = Left(ret,8)
ProgressBar 1。 Value = Val(ret)
Label 1。 Caption = Gettime(Val(ret))
Displays the current time song.
If ProgressBar 1. Value = ProgressBar 1. Then Max.
Stop playing _ click
If ... it will be over.
t = mciSendString(" status "+MFN+" mode ",ret, 128,0)
ret = Left(ret,8)
StatusBar 1。 Panel (1). Text = ret
End joint
Private Sub-Form _Unload(Cacel is an integer)
t% = mciSendString("stop " + mfn,0 & amp, 0, 0)
t% = mciSendString("close " + mfn,0 & amp, 0, 0)
End joint
Private function Gettime (location as long as string)
The function is to convert the time expressed by a long integer into an electronic clock "* * * *"
Minimum size, seconds
Minimum value = position/1000
Minimum value = minimum value/60
sec = min - Int(min)
min = Int(min)
Seconds = 60 * seconds/100
sec = Int(sec * 100)
Gettime = Str(min) + ":" + Str(sec)
End function