Opening a video file in an application
Hi. Does anyone know how to open a file like a video and jpg file in a window application? I know how to open the jpg file in a picture viewer but not any other kind of files like video files. I was hoping it was easy like the web languages with a simple link. Say the file was located c:/vid.wvm, how could I load and run this in an application?
Jennifer
Re: Opening a video file in an application
I don't know about pure .NET, but in my dark grimy world of API programming we used ShellExecute to do that. (I assume you mean open the video in it's default handler?)
Anyway you just pass the path the video file and the command as "open".
Code:
using System.Runtime.InteropServices;
[DllImport("shell32")]
static extern IntPtr ShellExecute (
IntPtr hWnd,
[MarshalAs(UnmanagedType.LPTStr)] string lpOperation,
[MarshalAs(UnmanagedType.LPTStr)] string lpFile,
[MarshalAs(UnmanagedType.LPTStr)] string lpParameters,
[MarshalAs(UnmanagedType.LPTStr)] string lpDirectory,
int nShowCmd
);
// usage:
const int SW_SHOWNORMAL = 1;
ShellExecute(IntPtr.Zero, "open", "c:/vid.wvm", "", "", SW_SHOWNORMAL);
Re: Opening a video file in an application
I ran your code and its executing fine. However, how exactly do you display it inside of the window form? what component would I use?
jennifer
Re: Opening a video file in an application
Oh.... Right I get ya :)
To display a video you need to place the appropriate handler component on your form. If you want to use Windows Media Player, you can add that component (It is a COM component, most are). I'm not aware of any .NET implementations of media players.
If on the other hand you want to display the window itself, there is a hacky way using SetParent. Do you wish to trap the WMP window inside say a part of your form, is that what you're after?
Re: Opening a video file in an application
Yes that is exactly what I'm after. I want it to run with window media player, but to trap the window inside of my form. You know like how easy it is to trap win media player or others like pdf inside of a browser, that's what I'm after. I want to open a wvm file, play it but show the video inside of my form.
Jennifer
Re: Opening a video file in an application
I could open the file in win media player with:
System.Diagnostics.Process.Start(c:/file.wvm);
But that opens it in win media player in its own window cause it's a separate process.
Again, I want to display the video inside a window form.
Jennifer
Re: Opening a video file in an application
If it's only ever videos, you can achieve that by using the Windows Media Player COM component. Add that to your form and it has the same appeareance as what you get in a browser. You can open and play videos using it and they will show in a little window which you can resize like any other control.
I believe that is the same way browsers themselves do it.
Re: Opening a video file in an application
OK. I understand what you're saying but how do you do what you're saying? I'm a little new to this penegate lol. I need the window media player com component. Where do I get this? What exactly do I do next. Please have patience. I'm a little slow to learning, not a brighthead like yourself.
Jennifer
Re: Opening a video file in an application
I was hoping you knew! :D
I don't have C# installed on this machine, I'm at a mate's house. So I can't tell you exactly what to do (I've only been using VC# for a few weeks). But to add a component, from the Project menu, select References. In that dialog box there should be a tab called "COM components" or something. Have a wade through that list to find WIndows Media Player (I'm not sure exactly what it's called). Once you add that as a reference, it should appear in your component toolbox. Then you can add it to your form.
Hope that works :p
Re: Opening a video file in an application
ok. I've done that. I added the win com file but i'm not seeing it in the components list.
Re: Opening a video file in an application
Thank penegate babes. I got it to work. Here's what I did. As you said I added the com file Window media player. Then in the toolbox, I added the com file and simple added it onto the form. After, all I did was specify the url / path in my code as: wmpPlayer.URL =@"C:\file.wmv"; and it played. Hurray it worked! Kisses to you penegate!
One thing though. I'm a fussy girl i know that but I will still ask. When I want to reference a file in the parent folder of my files, like if my project is Proj, and I want to open a file located inside that folder, I use: "..\\..\\file.wmv"
However when I specify this string as the url path, it's not working. It's the first time I tried using the path as that and its not working. I used it successfully to write files, copy files, read xml files etc. Do you know how I could specify the path as: wmpPlayer.URL =@"..\\..\\file.wmv";?? which as present is not working. Only path like c:/xxxx.xxx is working. Thanks
Jennifer
Re: Opening a video file in an application
OK well (and I'm still not home yet :D) evidently you need to pass an absolute path which means you need to do the path parsing yourself (try saying that really quick :p). Up two levels would mean removing all up to the previous \ section. I can't claim to be at all familiar with .NET string parsing methods off the top of my head, so I'll wait till I get home to whip up a demo (unless you do one).
Re: Opening a video file in an application
Depending on whether you are working relative to the current folder or the application folder:
VB Code:
wmpPlayer.URL = IO.Path.Combine(IO.Path.GetDirectoryName(IO.Path.GetDirectoryName(Environment.CurrentDirectory)), "file.wmv");
or
VB Code:
wmpPlayer.URL = IO.Path.Combine(IO.Path.GetDirectoryName(IO.Path.GetDirectoryName(Application.StartupPath)), "file.wmv");