Playing a video clip on a form

oneillc9

New member
Joined
Feb 12, 2005
Messages
3
Programming Experience
1-3
I'm having a bit of trouble figuring out how to play an mpg on a form in my project.

Is there a control I can use or can anyone direct me to a tutorial or something??

Cheers
 

jaggedarrow

New member
Joined
Feb 22, 2005
Messages
1
Programming Experience
3-5
Using MMC is the easiest way to play video files. Unfortunately it was only installed with Visual Basic 6. However, if you have a previous installation of VB6, the control will work in .NET.
 

ImDaFrEaK

Well-known member
Joined
Jan 7, 2006
Messages
416
Location
California
Programming Experience
5-10
Goto Microsoft.com and download the DirectX SDK and use it. It works great with a nice help menu showing you the Methods, Properties, and Events available to use. Works with all .NET languages.
http://www.microsoft.com/downloads/details.aspx?FamilyId=1FD20DF1-DEC6-47D0-8BEF-10E266DFDAB8&displaylang=en

Just add a reference to your project. Reference Microsoft.DirectX.AudioandVideoPlayback.dll

Then declare an object to use as the audio or video file.

Code:
[COLOR=blue]Dim[/COLOR] [COLOR=blue]WithEvents[/COLOR] Vid [COLOR=blue]as new[/COLOR] Microsoft.DirectX.AudioandVideoPlayback.Video(fileName)
Then Vid will have all the propertys and settings you want to use as well as events.

For example:
Code:
Vid.Owner = [COLOR=blue]Me[/COLOR]
Vid.Play()
That will paint the current form with the video. You can figure the rest out, it's really simple.

Some quick tips to help you out since I am familiar with DX.

You have to supply a fileName with the DX constructor; however if you attempt to open a new video file then the old one will continue to play along with the new one and you will often get a null reference if you just try to use vid.open(fileName). So I write my own wrappers for the object to work better.

Example:
First Import the namespace to save time typing.

Imports Microsoft.DirectX.AudioandVideoPlayback


Then I wrote an overloaded sub play.

When I call just play I simply play (you know b/c you may be in a pause state) but not if it's null b/c DX gives null reference error.

When I call it with FileName it checks to either instantiate the object is null or just open new if not. This prevents overlapping video. Of course, overlapping may be a process you want to happen sometimes so figure out your style. Hope this helps give you a nice start. :)
Code:
[COLOR=blue]Public Sub Play[/COLOR]([COLOR=blue]ByVal[/COLOR] FileName [COLOR=blue]as String[/COLOR])
 
     [COLOR=blue]If[/COLOR] Vid [COLOR=blue]is Nothing then[/COLOR]
          Vid = [COLOR=blue]New[/COLOR] Video(FileName, [COLOR=blue]True[/COLOR])
     [COLOR=blue]Else[/COLOR]
          Vid.Open(FileName, [COLOR=blue]True[/COLOR])
     [COLOR=blue]End If[/COLOR]
 
[COLOR=blue]End Sub[/COLOR]
 
[COLOR=#0000ff]Public Sub [/COLOR][COLOR=black]Play[/COLOR][COLOR=black]()[/COLOR]
 
    [COLOR=blue] If[/COLOR] Vid [COLOR=blue]is Nothing then Return[/COLOR]
     Vid.Play()
 
[COLOR=blue]End Sub[/COLOR]
 
Last edited:

JohnH

VB.NET Forum Moderator
Staff member
Joined
Dec 17, 2005
Messages
15,286
Location
Norway
Programming Experience
10+
Windows include the Windows Media Player and you can also add this as a control to the form. First you must add it to your Toolbox, do this with Toolbox context menu "Choose items..", browse the COM tab and select "Windows Media Player". Now you can add the control to form like any other control.

To play music/video set the URL property to the (path+)filename. If you don't want user interface controls change the uiMode property from "full" to "none", in this case you also may want disable the player context menu by setting the enableContextMenu property to False.
 

Ultrawhack

Well-known member
Joined
Jul 5, 2006
Messages
164
Location
Canada
Programming Experience
3-5
Sounds interesting, ImDaFreak. Have you tried playing the video on the form and simultaneously on 2nd monitor in fullscreen mode in a multi-Monitor setup ?

I'd be keen to tap your knowledge on using DShow and VB.net...
 

ImDaFrEaK

Well-known member
Joined
Jan 7, 2006
Messages
416
Location
California
Programming Experience
5-10
My knowledge is wide open, im on yahoo messenger, check e-mail often and of course im here and I love to help out. As far as running on a second monitor, no I havn't tried it. I use to have dual monitors but never programmed any video for it. As for running the vid on dual monitors, yes it's practical and virtually easy (im assuming it's easy). The process would be the same as running the vid on two seperate forms, one normal state and one maximized. You only have the second form on the second monitor. Of course, im playing that by ear but if you have two monitors then maybe you can just try it and verify it. SHould be quite simple. Tell you this much, if you can think it then it can be achieved, just might not be as easy as using current functions in our managed code, .NET, and the coding may take too long, but the potential always exists. As for this, i honestly don't believe it would be hard at all and most likely is as easy as I just mentioned.
 

JohnH

VB.NET Forum Moderator
Staff member
Joined
Dec 17, 2005
Messages
15,286
Location
Norway
Programming Experience
10+
I may be off track here, but I was under the impression that what is displayed in each display when system got several monitors is controlled by graphics card software, with input from user to choose what application goes to what display. NVidia for instance got the nView Desktop Manager where this may be configured. There usually also is the video overlay setting that tells where this specific type graphics stream is routed. You might argue that if Nvidia can do it then you can too, but your wish and the graphics card driver with control-software and user-settings might not be compatible and able to come to an agreement about this. On a similar note, there exist a powertoy for XP for multiple virtual desktops, still this is control software with user interface you can't automate from code.
 

Ultrawhack

Well-known member
Joined
Jul 5, 2006
Messages
164
Location
Canada
Programming Experience
3-5
I am experimenting with DShow and VB2005Express and the
DShow VB.NET library from http://directshownet.sourceforge.net/about.html

Is it just me or are the samples supplied not upgrading properly to VB2005E. Unless I'm mistaken, trying to open the examples from VB2005E initiates the upgrade wizard... and not one of the examples actually upgrade/open. I know, I should not be surprised...

Can you guys verify that for me ? I don't have VS2003.

ImDaFreak, I'm getting in touch with you for your help on playing videos off a form in vb2005e
 

ImDaFrEaK

Well-known member
Joined
Jan 7, 2006
Messages
416
Location
California
Programming Experience
5-10
If you are getting errors then let me know b/c I may have dealt with the same things. Also, rather than using extended examples, I suggest starting from scratch and learning the basic for yourself and or just getting a bare basic example. Then again that may be what you have. I never used any examples to get started but then again it took me a long time to get started. However; as a result, I have learned a lot of in's and outs. One thing I can tell you is that when running DX in VS 2005 you need to disable one of the exceptions it looks for. The "Loader Lock" exception.

Here's how to do it.

With the project open in the IDE goto the top menu. Click Debug/Exceptions.

Then a new window will popup with options for turning on or off certain exceptions the IDE looks for. Click "Managed Debugging Assistants", scroll down then uncheck "Loader Lock". I'm not totally sure what Loader Lock is doing so don't ask, something to do with Multi-Threading and cross referencing or somthing, I don't know. Anyways, DX works just like it should after you uncheck that.


As for using an example I will send you a bare basic example with comments throughout the code if you like. Just hit me up.



As far as JohnH, I didn't mean control the video cards drivers, I just meant with my NVidia, when I had dual monitors, it was as easy as moving the form to the next window then maximizing. I used to play realplayer on my tv and still be online with my computer. And yes, I split realplayer up so that the screen was on the tv and the controls ect were on the monitor. (The tv being used just as a monitor with adapters) As far as programming the split screens, then like you said, i have no idea. I just think it can be done without having to control the Card Drivers ect... maybe just by using the forms location. You'll have to experiment.
 

SVA

Member
Joined
Oct 12, 2006
Messages
16
Programming Experience
3-5
ImDaFrEak, I tried as you explained, but I still get this error:

LoaderLock was detected
Message: DLL 'C:\WINDOWS\assembly\GAC\Microsoft.DirectX\1.0.2902.0__31bf3856ad364e35\Microsoft.DirectX.dll' is attempting managed execution inside OS Loader lock. Do not attempt to run managed code inside a DllMain or image initialization function since doing so can cause the application to hang.


Here's my code:

Code:
Imports Microsoft.DirectX.AudioVideoPlayback
 
Public Class SomeForm
Dim FileName As String = "C:\Sort Files.avi"
Dim WithEvents Vid As New Microsoft.DirectX.AudioVideoPlayback.Video(FileName)
 
Private Sub SomeForm_Activated(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Activated
If Vid Is Nothing Then
Vid = New Video(FileName, True)
Else
Vid.Open(FileName, True)
End If
End Sub
 
Public Sub Play()
If Vid Is Nothing Then Return
Vid.Play()
End Sub
End Class
What do you propose to solve this error?
 
Last edited by a moderator:

ImDaFrEaK

Well-known member
Joined
Jan 7, 2006
Messages
416
Location
California
Programming Experience
5-10
Yeah, that is an issue with DirectX. Here's what you have to do.

Open your project.
On the Visual Studio toolstrip click 'Debug'
Then choose 'Exceptions'
Expand 'Managed Debugging Assistants'
Find 'Loader Lock' about 3/4 the way down the list and un-check it.

It will work fine but you will not be able to detect any other loader locks in your current application. Just be aware of what a loader lock is and try to avoid it.
 
Top Bottom