Media player made in DirectShow in C #
Overview: My program just tells you how to use DirectShow to make a player in C #, there is no too much function in the world. Maybe you can solve the problem as long as you spend five points. Yes if you use it Ide, I feel that everything is just using your mouse in your designer, you can set something simple. Of course, you still have to code it. At least about the DirectShow interface. For example, the view And sound.
Small problems in the program: 1. How to open media files from your disk 2. How to make buttons on the toolbar and disable 3. How to set the status bar 4. How to control time 5. How to use time controls Event 6. How to play media files with DirectShow 7. How to determine play status, etc. ...
The user interface is as follows: Is it simple? Yes, I said it is a simple program. I just make it a basic function. The three Button control play, stop and pause on the toolbar. A file menu is used Open media files and closes the program. Of course, there is an introduction of INFO for the program without doubt that this is the most basic interface configuration.
Here is the DirectShow interface
Play the video and sound files We have to use DIECTX to provide the DirectShow component. Using this interface allows you to easily play the shared images and sound files. You have to do just just install the DirectShow interface and use its function function and Configure the correct interface parameters.
Unfortunately .NET does not formally support DirectX. Yes Maybe you heard that DirectX9 support is it? Yes, but in the final version of the day, we haven't come to the best effect. But no matter how Do we still use it? If you don't have this article, we have been invalid. Yes, maybe you have used VB, right, it is it, we are using it.
Ok, before this, we must also do things. I think you have already guessed, is it right? Remember Xcopy? Yes. Net advantage. Come, hurry this "interop.quartztypelib.dll "DLL references come in, just see the picture below this? It is very simple.
Don't forget to see if there is such a using quartztypelib in your code; is there? The compiler is added to you. If you don't have you, please add it.
The preparation work is over, which is the code part, this is the responsibility that the programmer can't shirk. Otherwise, we have to laid off.
Program implementation section:
How to open you want the media file?
Remember? "File -> Open ..." Yes, almost every person using Windows will operate this way. How to implement? Simply look at the code below:
OpenFiledialog OpenFileDialog = New OpenFiledialog ();
OpenfileDialog.filter = "Media Files | * .mpg; *. Avi; *. WMA; *. MOV; *. WAV; *. MP2; *. MP3 | All Files | *. *";
IF (DialogResult.ok == OpenFileDialog.showdialog ()) {.. Look, is it very simple? Remember to write a function put it in. When you click the OK button, you will get you want to play. Document. Look at the picture below you know how it works.
DirectShow provides the most basic services for multimedia stream playback, which can be a local file, or the server is transmitted. In particular, DirectShow can support video playback, supports compressed video content in different files and stream formats, including Windows Media, MPEG, AVI, and WAV.
At the core of DirectShow, the service is a modular set of components, called a filter, which can be arranged in a filter diagram according to the media type. Filters can operate data streams such as read, analysis, decoding, formatting, or rendering.
The filter is arranged in a tree, which is called a filter tree, which is managed by Filter Graph Manager, referred to as FGM. With the FGM application, you can directly control the filter tree by using the Microsoft Windows Media Player control, and can also be directly controlled by calling the COM interface method. DirectShow Filter Tree (see Figure 1) consists of a source to the target renderer, all of which are connected by an input and output filter pin. Filter pin negotiations which media types will be supported. The FGM controls the multimedia data stream between the tree filter. Because DirectShow has a flexible, reconfigurable filter tree architecture, DirectSHOW can support multiple media types of playback and splitting using the same software ingredients. Developers can also extend DirectShow multimedia support by writing their own filters. Figure 1. DirectShow Filter Tree
The filter filter is a registered DirectShow class, which performs many media information processing tasks. These tasks include:
Get source information (for example, obtaining media flow) analysis (eg, in streaming, separation, separation, and formatting) conversion (eg, decoding WMA and MPEG-4 audio and video stream) rendering (for example, when appropriate Generating audio PCM or video RGB / YUV output, transmitting data to the DirectSound and DirectDraw filters Use several types of interfaces, such as pins, counters, transmitters, and clock interfaces to perform their tasks. Filters achieve and open many interfaces. FGM can use these interfaces to create, connect, and control the tree. The filter often implements the IBaseFilter interface containing the following methods:
Run, stop and suspend the filter status. Restore filter and manufacturers information. Get and set the reference clock. Restore the filter status information. Enumerate filter leads. Relocating filter tree positioning pin
I have already introduced so much, your hand may not be idle. Take a look at how the code below is implemented.
Cleanup ();
m_objfiltergraph = new filgraphManager (); m_objfiltergraph.renderfile (OpenFiledialog.FileName);
m_objbasicaudio = m_objfiltergraph as ibasicaudio;
try {m_objVideoWindow = m_objFilterGraph as IVideoWindow; m_objVideoWindow.Owner = (int) panel1.Handle; m_objVideoWindow.WindowStyle = WS_CHILD | WS_CLIPCHILDREN; m_objVideoWindow.SetWindowPosition (panel1.ClientRectangle.Left, panel1.ClientRectangle.Top, panel1.ClientRectangle.Width, panel1 .ClientRectangle.height;} catch (exception ex) {m_objvideoWindow = null;}
m_objmediaevent = m_objfiltergraph as iMediaEvent;
M_objmediaeventex = m_objfiltergraph as imediaeventex; m_objmediaeventex.setNotifyWindow ((int) this.handle, wm_graphNotify, 0);
m_objmediaposition = m_objfiltergraph as imediaposition;
m_objmediacontrol = m_objfiltergraph as iMediacontrol;
//
How to play, suspend, stop? Simple these functions look at the literal. M_Objmediacontrol.run (); // Play
m_objmediacontrol.pause (); // Pause
m_objmediacontrol.stop (); // Stop
OK, in order to control how time progress? // private void Timer1_Tick (Object sender, system.eventargs e) {if (m_currentstatus == mediable attus.running) {updatestatusbar ();}}
See above that UpdateStatusBar (); here, it is not updated every 100ms status bar code is as follows: private void UpdateStatusBar () {switch (m_CurrentStatus) {case MediaStatus.None:. StatusBarPanel1.Text = "Stopped"; break; case MediaStatus. Paused: statusBarPanel1.Text = "Paused"; break; case MediaStatus.Running: statusBarPanel1.Text = "Running"; break; case MediaStatus.Stopped: statusBarPanel1.Text = "Stopped"; break;}
IF (m_objmediaposition! = null) {int S = (int) m_objmediaposition.duration; int h = S / 3600; INT M = (S - (h * 3600)) / 60; s = s - (h * 3600 m * 60);
StatusbarPanel2.Text = string.format ("{0: D2}: {1: D2}: {2: D2}", H, M, S);
s = (int) m_objmediaposition.currentPosition; h = s / 3600; m = (s - (h * 3600)) / 60; s = s - (h * 3600 m * 60);
Statusbarpanel3.text = string.format ("{0: D2}: {1: D2}: {2: D2}", h, m, s);} else {statusbarpanel2.text = "00:00:00"; Statusbarpanel3.text = "00:00:00";}}
There is also a question program how can it be played ???? This will be a bit trouble. Ok, think about what way? Yes, Windows is a message. So what news is good. . Some of the EC_COMPLETE this. Remember "WndProc" it ?? Yes, my old friend. This time we have to overwrite it to capture the EC_COMPLETE message. This message is DirectShow to inform the parent form, the play is over.
protected override void WndProc (ref Message m) {if (m.Msg == WM_GRAPHNOTIFY) {int lEventCode; int lParam1, lParam2; while (true) {try {m_objMediaEventEx.GetEvent (out lEventCode, out lParam1, out lParam2, 0) ;
M_ObjmediaeventEx.FreeEventParams (Leventcode, LPARAM1, LPARAM2);
if (lEventCode == EC_COMPLETE) {m_objMediaControl.Stop (); m_objMediaPosition.CurrentPosition = 0; m_CurrentStatus = MediaStatus.Stopped; UpdateStatusBar (); UpdateToolBar ();}} catch (Exception) {break;}}}
Base.WndProc (Ref M);} Ok, everything is over, now you have to do something to find a movie to enjoy your own results.