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.
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)
.
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
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.
Private Sub OpenFile(ByVal fName As String, ByVal VidHand As IntPtr,
ByVal VidCtrl As System.Windows.Forms.Control)
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
UseCtrl = VidCtrl
filename = fName
currentState = PlayState.Stopped
currentVolume = VolumeFull
PlayMedia(fName)
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.
Private Sub PlayMedia(ByVal fName As String)
Dim hr As Integer = 0
If fName = Nothing Then Exit Sub
Try
graphBuilder = DirectCast(New FilterGraph,
IFilterGraph2)
hr = graphBuilder.RenderFile(fName, Nothing)
DsError.ThrowExceptionForHR(hr)
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()
If isAudioOnly = False Then
hr = mediaEventEx.SetNotifyWindow(UseHand, WMGraphNotify, IntPtr.Zero)
DsError.ThrowExceptionForHR(hr)
hr = videoWindow.put_Owner(UseHand)
DsError.ThrowExceptionForHR(hr)
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()
Call SetFormTitle()
hr = mediaControl.Run
DsError.ThrowExceptionForHR(hr)
currentState = PlayState.Running
If isAudioOnly = False Then
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.