一个简单的MP3
播放器 利用API函数[mciSendString]可以轻松实现MP3音乐文件的播放。下面这段程序实现了MP3播放的大部和漏吵分常规 *** 作,对其稍加修改,做一个100KB大小的MP3播放器轻而易举 启动VB程序,在窗体上放置6个命令
按钮,三个标签,一个公用对话框、一个进度条、一个状态栏和一个计时器,窗本的布置请参考附图 按钮Open MP3 File是用来打开对话框选择MP3文件,其他5个按钮分别为Play(播放)、Pause(暂停)、Stopplay(停止播放)、Back(向后跳跃)、Prew(向前跳跃)。Label1用来表示歌曲当前时间;label2
放在时度条的最左边,Caption属性为00:00;lable3放在时度条的右边,用来表示歌曲总长。 下面就可以编写代码了。首先在窗体的通用声明部分声明函数 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) 发出暂停的命令’api可以实现
Option Explicit
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 mp3filename As String
Private Sub Command1_Click()
Dim n As Integer
Command1.Caption = IIf(Command1.Caption = "&Play", "&Stop", "&Play")
n = IIf(Command1.Caption = "&Play", 0, 1)
playsound (n)
End Sub
Private Sub Form_Load()
Command1.Caption = "&Play"
mp3filename = "d:\123.mp3"
If Dir(mp3filename) = "" Then
MsgBox "没简滚态找到:" &mp3filename
Unload Me
End If
End Sub
Function playsound(n)
If n = 1 Then
mciSendString "play " &mp3filename &" repeat", vbNullString, 0, 0
Else
mciSendString "close "备派 &mp3filename, 0&, 0, 0
End If
End Function
Private Sub Form_Unload(Cancel As Integer)
If Command1.Caption = "&Stop" Then playsound 0
End Sub
添加两个Command,Command2.Caption="上一曲",Command3.Caption="下一曲"
添加代码如下:
Private Sub Command2_Click()
If List1.ListIndex = 0 Or List1.ListIndex = -1 Then
List1.ListIndex = List1.ListCount - 1
Else
List1.ListIndex = List1.ListIndex - 1
End If
Me.Caption = "我缺祥的播放器 当前播放" &pa &List1.Text
WindowsMediaPlayer1.URL = pa &List1.Text
End Sub
Private Sub Command3_Click()
If List1.ListIndex = List1.ListCount - 1 Then
List1.ListIndex = 0
Else
List1.ListIndex = List1.ListIndex + 1
End If
Me.Caption = "慎冲我的播放器 当前播伏孝搏放" &pa &List1.Text
WindowsMediaPlayer1.URL = pa &List1.Text
End Sub
评论列表(0条)