DirectX:Direct3D:Tutorials:VB:DX8:DirectShow

From GPWiki

Files:GUITutorial_warn.gif The Game Programming Wiki has moved! Files:GUITutorial_warn.gif

The wiki is now hosted by GameDev.NET at wiki.gamedev.net. All gpwiki.org content has been moved to the new server.

However, the GPWiki forums are still active! Come say hello.

DirectShow is used to play videos. It can also play MP3's.

Contents

Initialization

Start a new project, then add a Module.

The first thing that needs to be done is to add a Reference to the Active Movie Control Type Library in the Project->References... menu item.

All code will be placed in the module.

Private dx_BasicAudio As IBasicAudio
Private dx_BasicVideo As IBasicVideo
Private dx_MediaEvent As IMediaEvent
Private dx_VideoWindow As IVideoWindow
Private dx_MediaControl As IMediaControl
Private dx_MediaPosition As IMediaPosition

Each object is used to control the seperate processes of DirectShow (Audio, video, events, the output window, controls and position).

Clean up

Public Sub CleanUp()
    On Error GoTo CleanUpError:
    'If the MediaControl has been initialized, then stop it.
    If ObjPtr(dx_MediaControl) > 0 Then
        dx_MediaControl.Stop
    End If
    'Destroy all objects
    If ObjPtr(dx_BasicAudio) > 0 Then Set dx_BasicAudio = Nothing
    If ObjPtr(dx_BasicVideo) > 0 Then Set dx_BasicVideo = Nothing
    If ObjPtr(dx_MediaControl) > 0 Then Set dx_MediaControl = Nothing
    If ObjPtr(dx_VideoWindow) > 0 Then Set dx_VideoWindow = Nothing
    If ObjPtr(dx_MediaPosition) > 0 Then Set dx_MediaPosition = Nothing
CleanUpError:
End Sub

This sub will stop any media that is currently playing, and release all the memory from objects that may have been loaded. Its important to run this sub before loading a new media file, and also when you exit the application.

Loading Media

Public Sub LoadMedia(ByVal DstHwnd As Long, Filename As String)
    On Error GoTo LoadMediaError
    
    'clean up any media that may be loaded already
    CleanUp
    
    'attach the filter graph manager to the media control
    Set dx_MediaControl = New FilgraphManager
    'process the media
    Call dx_MediaControl.RenderFile(Filename)
    
    'attach the media control to the audio object
    Set dx_BasicAudio = dx_MediaControl
 
    'reset the volume and balance
    dx_BasicAudio.Volume = 0 'Loudest
    dx_BasicAudio.Balance = 0 'Centered
    
    'attach the media control to the video window
    Set dx_VideoWindow = dx_MediaControl
    
    'set the window style to none.
    dx_VideoWindow.WindowStyle = 0
    
    'ensure the video window is in the right place
    dx_VideoWindow.Top = 0
    dx_VideoWindow.Left = 0
    
    'play the video only on the destination
    dx_VideoWindow.Owner = DstHwnd
    
    'attach the media control to the events
    Set dx_MediaEvent = dx_MediaControl
    
    'attach the media control to the position
    Set dx_MediaPosition = dx_MediaControl
    dx_MediaPosition.Rate = 1 'Normal forward playback speed
         
LoadMediaError:
End Sub

This sub will initialize all the DirectShow objects, and load your media file. Notice that you have to attach something called a filgraphmanager to your media control object. According to Microsoft, the filgraphmanager manages the run, pause, and stop functions.

In order to play MP3 files, you need to skip all the dx_VideoWindow calls in the LoadMedia sub. If you don't, it will cause an error and will not play.

Play, Pause and Stop

Play

Public Sub PlayMedia()
On Error GoTo PlayMediaError:
    If dx_MediaPosition.CurrentPosition = dx_MediaPosition.Duration Then
        dx_MediaPosition.CurrentPosition = 0
    End If
    Call dx_MediaControl.Run
PlayMediaError:
End Sub

Pretty easy, if the current position is the same as the duration, then reset the position to zero, then play the media.

Pause

Public Sub PauseMedia()
On Error GoTo PauseMediaError:
    Call dx_MediaControl.Pause
PauseMediaError:
End Sub

In order to un-pause, you must call dx_MediaControl.Run again.

Stop

Public Sub StopMedia()
On Error GoTo StopMediaError:
    Call dx_MediaControl.Stop
    dx_MediaPosition.CurrentPosition = 0
StopMediaError:
End Sub

Stop the media, and reset the position to zero.


Final thoughts

Well thats all there is to it. As you can see, there are many things you can do to the playback of videos. Notice in the LoadMedia sub, that dx_VideoWindow has a .Left and .Top property. There is also a .Width and .Height property as well, which can be used to adjust the playback size of the video. Also you can adjust the speed of the video using dx_MediaPosition.Rate. Since 1 is normal speed, .5 is half speed and 2 is double speed.


Moglor 10:01, 1 September 2006 (EDT)