在多媒体应用开发中,用户界面的友好性是至关重要的。其中,能够通过滑块控制视频播放进度是一个常见的需求。本文将介绍如何使用VB.NET结合DirectShow技术来实现这一功能。
在开始之前,建议读者先了解一些相关的背景知识。可以通过以下资源获取更多信息:
在创建了图形对象之后,可以使用下面的代码来实现滑块控制视频播放进度的功能。
以下是VB.NET的代码示例:
Public Event Movie_Playhead_Moved(ByVal frame As Long, duration As Long)
Public Sub Graph_Seek(ByVal frame As Long)
If FilterGraphState = FilterGraphStateEnum.Ready Then
Try
Dim hr As Integer
Dim duration As Long
hr = f_MediaPositioning.GetDuration(duration)
DsError.ThrowExceptionForHR(hr)
hr = f_MediaPositioning.SetPositions(frame, _
AMSeekingSeekingFlags.AbsolutePositioning, _
duration, AMSeekingSeekingFlags.AbsolutePositioning)
RaiseEvent Movie_Playhead_Moved(frame, duration)
DsError.ThrowExceptionForHR(hr)
Catch ex As Exception
ThrowError("error sub graph_seek : ", ex)
End Try
Else
ThrowError("not ready to do seeking")
End If
End Sub
Private Sub HeANewPlayheadEvent(ByVal frame As Long, ByVal duration As Long)
Handles Me.Movie_Playhead_Moved
Invoke_trackbar_playhead_update(frame, duration)
End Sub
Public Sub Invoke_trackbar_playhead_update(ByVal frame As Long, duration As Long)
If Me.TrackbarPlayhead.InvokeRequired Then
Dim del As New Delegate_Trackbar_Playhead_Update(AddressOf Process_Trackbar_playhead_update)
del.Invoke(frame, duration)
Else
Process_Trackbar_playhead_update(frame, duration)
End If
End Sub
Private Sub Process_Trackbar_playhead_update(ByVal frame As Long, ByVal duration As Long)
If TrackbarPlayhead.Value <> frame Then
TrackbarPlayhead.Maximum = CInt(duration / 1000)
TrackbarPlayhead.Value = CInt(frame / 1000)
TrackbarPlayhead.TickFrequency = CInt(duration / 10)
TrackbarPlayhead.Update()
Application.DoEvents()
End If
End Sub
可以使用一个定时器来调用一个子程序,每当图形加载并且处于播放/暂停模式时:
Dim hr As Integer
Dim frame As Long
hr = f_MediaPositioning.GetCurrentPosition(frame)
hr = f_MediaPositioning.GetDuration(duration)
Invoke_trackbar_playhead_update(frame, duration)
通过使用IMediaSeeking
接口,可以控制视频的播放速率。