用VB给播放器加上时间显示功能
软件世界
使用MediaPlayer或RealAudio控件编写过多媒体播放器的朋友都知道,这两个控件本身并未直接提供播放的时间属性,只提供了一组数值表示当前播放的进度。属性如下:
MediaPlayer.CurrentPosition (当前播放进度值)
MediaPlayer.Duration (文件总长度数值)
RealAudio.GetPosition (当前播放进度值)
RealAudio.GetLength (文件总长度数值)
这样我们要在自己编写的播放器界面上显示播放的时间比例,那就要将上面属性返回的数值转换成时间“00:00:00”格式,下面就是实现转换功能的函数:
Public Function StrTime(Mr As Long, Value As Long) As String
On Error Resume Next
Dim SS, FF, mm, FM As Long
Dim SSD, FFD, MMD As String
Dim V As Long
V = Int(Value/Mr)
If V < 0 Then Exit Function
SS = Int(V/3600)
FF = Int(V/60 - SS * 60)
FM = Int(V/60)
mm = V - 60 * FM
SSD = SS
If SS < 10 Then SSD = "0" & SS
FFD = FF
If FF < 10 Then FFD = "0" & FF
MMD = mm
If mm < 10 Then MMD = "0" & mm
StrTime = SSD & ":" & FFD & ":" & MMD
End Function
将上面的函数加到你的程序中,然后在窗体上添加Timer1控件和Label1控件。
Timer1──用来即时将返回的播放进度值转换为时间格式。如使用Media...控件,请将Timer1.Interval的值设为1000,使用Real...控件则将Timer1.Interval的值设为1。
Label1──将转换的时间显示出来,例如 “00:02:12/00:05:34”。
以下为两控件的代码:
'先声明一全局布尔变量Time_Disp
Private Sub Label1_Click()
Time_Disp = Not (Time_Disp) '在时间显示顺,逆时之间切换
End Sub
时间控件代码:(根据控件不同选择不同代码)
MediaPlayer控件使用下面代码:
Private Sub Timer1_Timer()
On Error Resume Next
If Time_Disp Then
Label1 = "-" & StrTime(1;MediaPlayer1.Duration-MediaPlayer1.CurrentPosition) & "/" & StrTime(1, MediaPlayer1.Duration) '逆时显示
Else
Label1 = StrTime(1, MediaPlayer1.CurrentPosition)& "/"&StrTime(1,MediaPlayer1.Duration) '顺时显示
End If
End Sub
RealAudio控件使用下面代码:
Private Sub Timer1_Timer()
On Error Resume Next
If Time_Disp Then
Label1 = "-" & StrTime(1000, RealAudio1.GetLength - RealAudio1.GetPosition) & "/" & StrTime(1000, RealAudio1.GetLength) '逆时显示
Else
Label1 = StrTime(1000, RealAudio1.GetPosition) & "/" & StrTime(1000, RealAudio1.GetLength) '顺时显示
End If
End Sub
注:本例程在Win98,VB6.0环境下调试通过。