Click here to Skip to main content
15,867,756 members
Articles / Multimedia / DirectX
Article

DirectShow VB.NET Example

Rate me:
Please Sign up or sign in to vote.
4.74/5 (19 votes)
24 Dec 2008Public Domain1 min read 141.6K   16.3K   37   21
A VB.net example to show how to use DirectShow in VB.NET.

Introduction

This application can play almost all media types. DirectShow can play a lot more file types but I haven't got around to writing the code. You can use this in your applications. DirectX is very reliable. I wrote this because there where no VB.NET source code on how to use DirectShow to Play Video (.avi, .mpg, .wmv) and Play Audio (.mp3, .wav). I converted it from a C# Example that was included with the DirectShow SDK. It still needs a lot of work. This is the first one that I know of. I may be wrong.

DirShowPic.JPG

Background

I converted a C# sample from the DirectShow SDK and got started. This is a example and still needs work. I included the DirectShow Library DLL. It's very easy to use.

Using the Code

Make sure DirectShow Library DLL is referenced in your application and loads a new graph. To use this application choose the handle you want the video To be displayed on. And call the Sub OpenFile(FilePath, Handle of Window, Control Name).

VB
Private Sub OpenFileDialog1_FileOk(ByVal sender As System.Object,
    ByVal e As System.ComponentModel.CancelEventArgs) Handles OpenFileDialog1.FileOk
    If e.Cancel = True Then Exit Sub
    'Call The Sub to Open the File. OpenFile(FilePath, Handle of Window, Control Name)
    OpenFile(OpenFileDialog1.FileName, VidScreenBox.Handle, Me)
End Sub

The Sub OpenFile sets the properties. It sets the owner to the Forms Handle that will play the Video if its a Video File.

VB
Private Sub OpenFile(ByVal fName As String, ByVal VidHand As IntPtr,
    ByVal VidCtrl As System.Windows.Forms.Control)
        ' Make sure everything is closed
        filename = ""
        CloseClip()

        MaxToolStripMenuItem.Enabled = True
        MinToolStripMenuItem.Enabled = True
        CustomToolStripMenuItem.Enabled = True
        NormalPlaybackRateToolStripMenuItem.Enabled = True
        DecreasePlaybackRateToolStripMenuItem.Enabled = False
        IncreasePlaybackRateToolStripMenuItem.Enabled = True
        HalfSpeedToolStripMenuItem.Enabled = True
        DoubleSpeedToolStripMenuItem.Enabled = True
        DoubleSize200ToolStripMenuItem.Enabled = True
        NormalSize100ToolStripMenuItem.Enabled = True
        HalfSize50ToolStripMenuItem.Enabled = True
        Size75ToolStripMenuItem.Enabled = True
        FullScreenToolStripMenuItem.Enabled = True

        UseHand = VidHand 'Handle to Display Video if any
        UseCtrl = VidCtrl 'Control to Display Video if any

        filename = fName
        currentState = PlayState.Stopped 'Reset State to Stopped
        currentVolume = VolumeFull 'Reset Volume

        PlayMedia(fName) 'Call Main Sub
        Call SetFormTitle()
        Timer1.Enabled = True
    End Sub

The next Sub loads the Graph Builder and Loads the interfaces DirectShow uses to Play Audio and Render the Video to the Forms Handle. After you call these subs. You can control the Audio and Video using the DirectShow Interfaces mediaControl, mediaEventEx, basicAudio, basicVideo, videoWindow, MediaPosition...Easy.

VB
Private Sub PlayMedia(ByVal fName As String)
        Dim hr As Integer = 0
        If fName = Nothing Then Exit Sub
        Try
            graphBuilder = DirectCast(New FilterGraph,
                IFilterGraph2) 'Load Graph Builder Device

            hr = graphBuilder.RenderFile(fName, Nothing) ' Initialize Graph Builder
            DsError.ThrowExceptionForHR(hr)

            'Load all Interfaces we will use
            mediaControl = DirectCast(graphBuilder, IMediaControl)
            mediaEventEx = DirectCast(graphBuilder, IMediaEventEx)
            mediaSeeking = DirectCast(graphBuilder, IMediaSeeking)
            mediaPosition = DirectCast(graphBuilder, IMediaPosition)
            videoWindow = DirectCast(graphBuilder, IVideoWindow)
            basicAudio = DirectCast(graphBuilder, IBasicVideo)
            basicVideo = DirectCast(graphBuilder, IBasicAudio)

            Call CheckType() 'Check to See if Audio or Video Call

            If isAudioOnly = False Then
                'Notfy Window of Video
                hr = mediaEventEx.SetNotifyWindow(UseHand, WMGraphNotify, IntPtr.Zero)
                DsError.ThrowExceptionForHR(hr)

                'Set Owner to Display Video
                hr = videoWindow.put_Owner(UseHand)
                DsError.ThrowExceptionForHR(hr)

                'Set Owner Video Style
                hr = videoWindow.put_WindowStyle(
                   WindowStyle.Child And WindowStyle.ClipSiblings And WindowStyle.ClipChildren)
                DsError.ThrowExceptionForHR(hr)
            End If

#If DEBUG Then
            rot = New DsROTEntry(graphBuilder)
#End If

            Me.Focus()
            'Update Form Title
            Call SetFormTitle()

            'Start Media
            hr = mediaControl.Run
            DsError.ThrowExceptionForHR(hr)

            currentState = PlayState.Running

            If isAudioOnly = False Then
                'Set Video Size
                hr = VideoWindowSize(1, 1)
                DsError.ThrowExceptionForHR(hr)
            End If
        Catch ex As Exception
            MsgBox("Error " & ex.Message, MsgBoxStyle.Critical, "Error")
            RaiseEvent MedClose()
        End Try
    End Sub

Points of Interest

Feel free to edit and redistribute the source code.

License

This article, along with any associated source code and files, is licensed under A Public Domain dedication


Written By
Software Developer None
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestioniVIDEO AUTO FIT ON ITS FORM Pin
sir_kaloy21-Apr-15 21:09
sir_kaloy21-Apr-15 21:09 
QuestionAudio stream count Pin
Member 1026358610-Sep-13 21:25
Member 1026358610-Sep-13 21:25 
Great job lilbiz8, good example.
I am trying to get the number of audio streams in the loaded video and I just need a little help, please.

In the PlayMedia sub, after the video is loaded I try to use IAMStreamSelect so I can use the Count function but I just can't get the IAMStreamSelect interface 'connected' to the graphBuilder.

My code should be something like:

VB
Private Sub PlayMedia(ByVal fName As String)
...
   If isAudioOnly = False Then
   ...
      'Here is my code
      Dim aud As DirectShowLib.IAMStreamSelect
      'Some code to connect aud to existing graphbuilder
      MsgBox(aud.Count.toString)
   ...
   End If
...
End Sub


I don't want to bother you but I've been many months looking for this and I would really appreciate you help.

Thank you very much!
Generalgood Pin
woonhow28-Mar-13 0:09
woonhow28-Mar-13 0:09 
QuestionPlay Video with DirectShow without ActiveMovieWindow... only over picturebox Pin
morandeira24-Feb-13 21:17
morandeira24-Feb-13 21:17 
QuestionRotate the video Pin
pedrazasual29-May-12 5:21
pedrazasual29-May-12 5:21 
QuestionAutoLoop Pin
Nippo18-Jan-12 23:33
Nippo18-Jan-12 23:33 
AnswerRe: AutoLoop Pin
Code4Tech14-Mar-12 3:50
Code4Tech14-Mar-12 3:50 
QuestionOn win 7 mp4 is not supported Pin
ridaria5-Aug-11 7:47
ridaria5-Aug-11 7:47 
AnswerRe: On win 7 mp4 is not supported Pin
_Plutonix23-Dec-11 15:04
_Plutonix23-Dec-11 15:04 
QuestionRepeat a video continuosly Pin
ridaria5-Aug-11 5:10
ridaria5-Aug-11 5:10 
QuestionHow to use IMpegAudioDecoder to change Audio Left/Right Channel in VCD Pin
binary00113-Dec-10 17:12
binary00113-Dec-10 17:12 
AnswerRe: How to use IMpegAudioDecoder to change Audio Left/Right Channel in VCD Pin
woonhow18-Apr-13 18:22
woonhow18-Apr-13 18:22 
GeneralSelecting different sound card Pin
Member 44443184-Dec-09 14:51
Member 44443184-Dec-09 14:51 
GeneralRe: Selecting different sound card Pin
lilbiz87-Dec-09 5:48
lilbiz87-Dec-09 5:48 
GeneralSelecting the sound card in Directshow for VB.Net Pin
J Lorkers30-Apr-09 7:18
J Lorkers30-Apr-09 7:18 
QuestionRe: Selecting the sound card in Directshow for VB.Net Pin
johnnynine9-Sep-09 15:14
johnnynine9-Sep-09 15:14 
QuestionHow can you prevent the window from resizing as you load a movie? Pin
Eyal K.8-Apr-09 5:51
Eyal K.8-Apr-09 5:51 
AnswerRe: How can you prevent the window from resizing as you load a movie? Pin
lilbiz86-Dec-09 12:47
lilbiz86-Dec-09 12:47 
GeneralStill images from running video Pin
Member 306612830-Dec-08 4:12
Member 306612830-Dec-08 4:12 
GeneralRe: Still images from running video Pin
lilbiz88-Jan-09 18:26
lilbiz88-Jan-09 18:26 
GeneralRe: Still images from running video Pin
ologbozigee23-Jul-11 21:18
ologbozigee23-Jul-11 21:18 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.