利用API函数[mciSendString]可以轻松实现MP音乐文件的播放下面这段程序实现了MP播放的大部分常规操作对其稍加修改做一个KB大小的MP播放器轻而易举 启动VB程序在窗体上放置个命令按钮三个标签一个公用对话框一个进度条一个状态栏和一个计时器窗本的布置请参考附图 按钮Open MP File是用来打开对话框选择MP文件其他个按钮分别为Play(播放)Pause(暂停)Stopplay(停止播放)Back(向后跳跃)Prew(向前跳跃)Label用来表示歌曲当前时间label放在时度条的最左边Caption属性为lable放在时度条的右边用来表示歌曲总长 下面就可以编写代码了首先在窗体的通用声明部分声明函数 Private Declare Function mciSendString Lib winmmdll Alias mciSendStringA(Byval lpstrCommand As StringByVal lpstrRetumString As StringByVal uReturnLength As longByVal hwndCallback as long) As long Dim mfn As String 下面就是各个对象的代码了 Private Sub Form_load() mfn= playEnabled=False pauseEnabled=False StopplayEnabled=False backEnabled=False prewEnabled=False TimerEnabled=False TimerInterval= End Sub Private Sub open_Click() On Error Goto err With CommonDialog CancelError=True Filter=音乐文件|*mp;*wav;*mid Flags=cdlOFNNoChangeDir And cdlOFNPathMustExist Action= stopplay_Click 停止按钮同时可以设备初始化 mfn=FileName 这个程序中文件名一定不能带空格 FormCaption=FileName playEnabled=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&) Open后边的空格一定不能丢 Dim ret As String * t=mciSendString(status + mfn + lengthret) 显示歌曲总长 ret=Left(ret) If ret<> Then ProgressBarMin= ProgressBarMax=Val(ret) LabelCaption=gettime(Val(ret)) End If playEnabled=False pauseEnabled=True stopplayEnabled=True backEnabled=True prewEnabled=True t=mciSendString(status + mfn + moderet) 得到设备的当前状态是播放还是暂停等等 ret=Left(ret) StatusBarPanels()text=ret 在状态栏显示播放状态 t=mciSendString(play + mfn + form + Str(ProgressBarValue)&) 开始播放 TimerEnabled=True End Sub Private Sub pause_Click() t%=mciSendString(pause + mfn&) 发出暂停的命令 playEnable=True pauseEnabled=False stopplayEnabled=True backEnabled=False prewEnabled=False End Sub Private Sub stopplay_Click() t%=mciSendString(stop + mfn&) t%=mciSendString(close + mfn&) 停止播放 playEnabled=True pauseEnabled=False stopplayEnabled=False backEnabled=False prewEnabled=False End Sub Private Sub back_Click() t%=mciSendString(play + mfn + from + Str(ProgressBarValue(ProgressBarMax\))&) 向后跳一小段再播放 End Sub Private Sub prew_Click() t%=mciSendString(play + mfn + from + Stri(ProgressBarValue+(ProgressBarMax\))&) 向前跳一小段再播放 End Sub Private Sub Timer_Timer() Dim t As Long Dim ret As String * t=mciSendString(status + mfn + positionret) 得到当前播放位置 ret=left(ret) ProgressBarValue=Val(ret) LabelCaption=gettime(Val(ret)) 显示歌曲当前时间 If ProgressBarValue=ProgressBarMax Then stopplay_Click End If t=mciSendString(status + mfn + moderet) ret=Left(ret) StatusBarPanels()Text=ret End Sub Private Sub Form_Unload(Cacel As Integer) t%=mciSendString(stop + mfn&) t%=mciSendString(close + mfn&) End Sub Private Function Gettime(position As Long) As String 这个函数的功能是把以长整型表示的时间转换为电子钟式的**:** Dim minsec min=position/ min=min/ sec=minInt(min) min=Int(min) sec= * sec / sec=Int(sec * ) gettime=Str(min) + : + Str(sec) End Function 图中个控制按钮其实是把字体设为Webdings让它们的Caption属性分别为 ; <" "7" "8" 就可以了 |