Private Declare Function mciSendString Lib "winmm.dll" Alias "mciSendStringA" (ByVal lpstrCommand As String, ByVal lpstrRetumString As String, ByVal uReturnLength As Long, ByVal hwndCallback As Long) As Long Dim mfn As String Private Sub Form_load()
mfn = ""
play.Enabled = False
pause.Enabled = False
Stopplay.Enabled = False
back.Enabled = False
prew.Enabled = False
Timer1.Enabled = False
Timer1.Interval = 500
End Sub Private Sub open_Click()
On Error GoTo err
With CommonDialog1
.CancelError = True
.Filter = "音乐文件|*.mp3;*.wav;*.mid"
.Flags = cdlOFNNoChangeDir And cdlOFNPathMustExist
.Action = 1
stopplay_Click '停止按钮同时可以设备初始化
mfn = .FileName '这个程序中文件名一定不能带空格
Form1.Caption = .FileName
play.Enabled = True
End With
play_Click
Exit Sub
err:
End Sub Private Sub play_Click()
On Error Resume Next
Dim t As Long
t = mciSendString("open " + mfn, 0&, 0, 0) 'Open后边的空格一定不能丢
Dim ret As String * 128
t = mciSendString("status " + mfn + " length", ret, 128, 0)
'显示歌曲总长
ret = Left(ret, 8)
If ret <> "" Then
ProgressBar1.min = 0
ProgressBar1.Max = Val(ret)
Label3.Caption = Gettime(Val(ret))
End If
play.Enabled = False
pause.Enabled = True
Stopplay.Enabled = True
back.Enabled = True
prew.Enabled = True
t = mciSendString("status " + mfn + " mode", ret, 128, 0)
'得到设备的当前状态,是播放还是暂停等等
ret = Left(ret, 8)
StatusBar1.Panels(1).Text = ret
'在状态栏显示播放状态
t = mciSendString("play " + mfn + " form " + Str(ProgressBar1.Value), 0&, 0, 0)
'开始播放
Timer1.Enabled = True
End Sub
Private Sub pause_Click()
t% = mciSendString("pause " + mfn, 0&, 0, 0)
'发出暂停的命令
play.Enabled = True
pause.Enabled = False
Stopplay.Enabled = True
back.Enabled = False
prew.Enabled = False
End Sub
Private Sub stopplay_Click()
t% = mciSendString("stop " + mfn, 0&, 0, 0)
t% = mciSendString("close " + mfn, 0&, 0, 0)
'停止播放
play.Enabled = True
pause.Enabled = False
Stopplay.Enabled = False
back.Enabled = False
prew.Enabled = False
End Sub
Private Sub back_Click() '向后跳跃
t% = mciSendString("play " + mfn + " from " + Str(ProgressBar1.Value - (ProgressBar1.Max \ 10)), 0&, 0, 0) '向后跳一小段再播放
End Sub
Private Sub prew_Click() '向前跳跃
t% = mciSendString("play " + mfn + " from " + Str(ProgressBar1.Value + (ProgressBar1.Max \ 10)), 0&, 0, 0) '向前跳一小段再播放
End Sub
Private Sub Timer1_Timer()
Dim t As Long
Dim ret As String * 128
t = mciSendString("status " + mfn + " position", ret, 0, 0)
'得到当前播放位置
ret = Left(ret, 8)
ProgressBar1.Value = Val(ret)
Label1.Caption = Gettime(Val(ret))
'显示歌曲当前时间
If ProgressBar1.Value = ProgressBar1.Max Then
stopplay_Click
End If
t = mciSendString("status " + mfn + " mode", ret, 128, 0)
ret = Left(ret, 8)
StatusBar1.Panels(1).Text = ret
End Sub Private Sub Form_Unload(Cacel As Integer)
t% = mciSendString("stop " + mfn, 0&, 0, 0)
t% = mciSendString("close " + mfn, 0&, 0, 0)
End Sub Private Function Gettime(position As Long) As String
'这个函数的功能是把以长整型表示的时间转换为电子钟式的"**:**"
Dim min, sec
min = position / 1000
min = min / 60
sec = min - Int(min)
min = Int(min)
sec = 60 * sec / 100
sec = Int(sec * 100)
Gettime = Str(min) + ":" + Str(sec)
End Function
mfn = ""
play.Enabled = False
pause.Enabled = False
Stopplay.Enabled = False
back.Enabled = False
prew.Enabled = False
Timer1.Enabled = False
Timer1.Interval = 500
End Sub Private Sub open_Click()
On Error GoTo err
With CommonDialog1
.CancelError = True
.Filter = "音乐文件|*.mp3;*.wav;*.mid"
.Flags = cdlOFNNoChangeDir And cdlOFNPathMustExist
.Action = 1
stopplay_Click '停止按钮同时可以设备初始化
mfn = .FileName '这个程序中文件名一定不能带空格
Form1.Caption = .FileName
play.Enabled = True
End With
play_Click
Exit Sub
err:
End Sub Private Sub play_Click()
On Error Resume Next
Dim t As Long
t = mciSendString("open " + mfn, 0&, 0, 0) 'Open后边的空格一定不能丢
Dim ret As String * 128
t = mciSendString("status " + mfn + " length", ret, 128, 0)
'显示歌曲总长
ret = Left(ret, 8)
If ret <> "" Then
ProgressBar1.min = 0
ProgressBar1.Max = Val(ret)
Label3.Caption = Gettime(Val(ret))
End If
play.Enabled = False
pause.Enabled = True
Stopplay.Enabled = True
back.Enabled = True
prew.Enabled = True
t = mciSendString("status " + mfn + " mode", ret, 128, 0)
'得到设备的当前状态,是播放还是暂停等等
ret = Left(ret, 8)
StatusBar1.Panels(1).Text = ret
'在状态栏显示播放状态
t = mciSendString("play " + mfn + " form " + Str(ProgressBar1.Value), 0&, 0, 0)
'开始播放
Timer1.Enabled = True
End Sub
Private Sub pause_Click()
t% = mciSendString("pause " + mfn, 0&, 0, 0)
'发出暂停的命令
play.Enabled = True
pause.Enabled = False
Stopplay.Enabled = True
back.Enabled = False
prew.Enabled = False
End Sub
Private Sub stopplay_Click()
t% = mciSendString("stop " + mfn, 0&, 0, 0)
t% = mciSendString("close " + mfn, 0&, 0, 0)
'停止播放
play.Enabled = True
pause.Enabled = False
Stopplay.Enabled = False
back.Enabled = False
prew.Enabled = False
End Sub
Private Sub back_Click() '向后跳跃
t% = mciSendString("play " + mfn + " from " + Str(ProgressBar1.Value - (ProgressBar1.Max \ 10)), 0&, 0, 0) '向后跳一小段再播放
End Sub
Private Sub prew_Click() '向前跳跃
t% = mciSendString("play " + mfn + " from " + Str(ProgressBar1.Value + (ProgressBar1.Max \ 10)), 0&, 0, 0) '向前跳一小段再播放
End Sub
Private Sub Timer1_Timer()
Dim t As Long
Dim ret As String * 128
t = mciSendString("status " + mfn + " position", ret, 0, 0)
'得到当前播放位置
ret = Left(ret, 8)
ProgressBar1.Value = Val(ret)
Label1.Caption = Gettime(Val(ret))
'显示歌曲当前时间
If ProgressBar1.Value = ProgressBar1.Max Then
stopplay_Click
End If
t = mciSendString("status " + mfn + " mode", ret, 128, 0)
ret = Left(ret, 8)
StatusBar1.Panels(1).Text = ret
End Sub Private Sub Form_Unload(Cacel As Integer)
t% = mciSendString("stop " + mfn, 0&, 0, 0)
t% = mciSendString("close " + mfn, 0&, 0, 0)
End Sub Private Function Gettime(position As Long) As String
'这个函数的功能是把以长整型表示的时间转换为电子钟式的"**:**"
Dim min, sec
min = position / 1000
min = min / 60
sec = min - Int(min)
min = Int(min)
sec = 60 * sec / 100
sec = Int(sec * 100)
Gettime = Str(min) + ":" + Str(sec)
End Function