Media Player using DirectShow in C #
Overview:
My program only tells you how to use DirectShow to make a player in C #.
There are too many features. Maybe you only need to spend five minutes to solve the problem. Yes, if you are using IDE, I am sure
You just need to use your mouse to set properties in your designer.
It still requires a little bit of encoding. At least it is about the DirectShow interface. For example, the video screen and sound.
Minor issues in the program:
1. How to open a media file from your disk
2. How to enable or disable the buttons on the toolbar
3. How to set the display text of the status bar
4. How to control the time
5. How to use time control events
6. How to Use DirectShow to play media files
7. How to Determine the playback status...
...
The user interface is as follows:
Is it easy? Yes, I said it was a simple program. I just gave it basic functions. on the toolbar
To control the playback, stop, and pause. A file menu is used to open the media file and close the program. Of course
There is another info that introduces the program, which is undoubtedly the most basic interface configuration.
The following describes the DirectShow interface.
We need to use the DirectShow component provided by diectx to play the video screen and sound files. This interface is used.
You can easily play the shared images and sound files. All you need to do is install the DirectShow interface.
And use its function and configure the correct interface parameters.
Unfortunately,. Net does not officially support DirectX. Yes, maybe you heard that directx9 supports it, right? Yes, but in
The final version has not been finalized yet, and we will not be able to achieve the best results. But in any case, do we still need to use it?
Otherwise this article has to be voided. Yes, maybe you have used VB. Right, that's it. We just need to use it.
Well, we have to do something before that. I guess you have already guessed it. Is it true to reference it? Do you still remember xcopy? Yes
. Net advantages. Come on, and reference this "InterOP. quartztypelib. dll" DLL, just like this
Have you seen the figure below? Very simple.
Finally, don't forget to see if there is such a sentence in your code.
Using quartztypelib;
Yes? The compiler has added it for you. If not, you can add it yourself.
The preparation is over. This is part of the code, which cannot be shirked by programmers. Otherwise, we will be laid off.
Program Implementation:
How to open the media file you want?
Remember? "File-> open..." Yes, almost everyone who uses Windows will perform this operation. How can this problem be achieved?
Let's take a look at the following code:
Openfiledialog = new openfiledialog ();
Openfiledialog. filter = "media files | *. MPG ;*. avi ;*. WMA ;*. moV ;*. wav ;*. MP2 ;*. MP3 | all files | *. *";
If (dialogresult. OK = openfiledialog. showdialog ())
{
.
.
.
Look, it's easy, right? Remember to write a function to put it in. When you click OK, The. DirectShow interface will
Get the file you want to play.
As shown in the figure below, you will know how it works.
DirectShow provides the most basic services for multimedia stream playback. These multimedia streams can be local files or transmitted by servers. In particular, DirectShow supports video playback and supports compressing video content in different file and stream formats, including Windows Media, MPEG, Avi, and WAV.
At the core of DirectShow, a service is a modular set of components, called a filter, which can be arranged into a filter Chart Based on the media type. Filters can operate on data streams, such as reading, analyzing, decoding, formatting, or rendering.
Filters are arranged in a tree. This tree is called a filter tree and is managed by the filter graph Manager (FGM. The FGM application can indirectly control the filter tree by using the Microsoft Windows Media Player control, and directly control the tree by calling the COM interface. The DirectShow filter tree (see Figure 1) consists of a directed filter sequence from the source to the target Renderer, all connected through the input and output filter pins. Filter pins negotiate which media types they will support. Multi-media data streams between FGM Control tree filters. Because DirectShow has a flexible and reconfigurable filter tree architecture, DirectShow supports playback and shunting of multiple media types using the same software components. Developers can also compile their own filters to expand DirectShow multimedia support.
Figure 1. DirectShow filter tree
Filter
A filter is a registered DirectShow class that executes many media information processing tasks. These tasks include:
Obtain source information (for example, obtain a Media Stream)
Analysis (for example, reading, splitting, and formatting packets on a stream)
Conversion (for example, decoding WMA and MPEG-4 audio and video streams)
Rendering (for example, generating audio PCM or video RGB/YUV output when appropriate, and passing data to directsound and DirectDraw)
Filters use several types of interfaces, such as pins, counters, transmitter, and clock interfaces, to execute their tasks. Filters implement and open many interfaces. FGM can use these interfaces to create, connect, and control trees. Filters often implement the ibasefilter interface that includes the following methods:
Filter status: Run, stop, and pause.
Restore the filter and vendor information.
Obtain and set the reference clock.
Restores the filter status information.
Enumerative filter leads.
Position pin when rebuilding the filter tree
Well, with so many introductions, your hands may be idle. Let's see how the following code 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_objvideowindoincluowner = (INT) panel1.handle;
M_objvideow.windowstyle = ws_child | ws_clipchildren;
M_objvideow.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.setpolicywindow (INT) This. Handle, wm_graphnotify, 0 );
M_objmediaposition = m_objfiltergraph as imediaposition;
M_objmediacontrol = m_objfiltergraph as imediacontrol;
//
How to play, pause, and stop?
Simply read these functions.
M_objmediacontrol.run (); // playback
M_objmediacontrol.pause (); // pause
M_objmediacontrol.stop (); // stop
OK. Let's see how we control the time progress?
//
Private void timerjavastick (Object sender, system. eventargs E)
{
If (m_currentstatus = mediastatus. Running)
{
Updatestatusbar ();
}
}
See the updatestatusbar () above. Here, the status bar is updated not once every Ms.
The 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 ";
}
}
How can the program know that it has finished playing ????
This will be a little troublesome. Well, think about the solution? By the way, Windows is message-driven. Find out what messages are there.
Okay. Some of them are ec_complete. Do you still remember "wndproc ?? Yes, my old friend. This time we must
Rewrite it to capture the ec_complete message. This message is the DirectShow notification parent form, and the playback is complete.
Protected override void wndproc (ref message m)
{
If (M. MSG = wm_graphpolicy)
{
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 ();
Updatemedilbar ();
}
}
Catch (exception)
{
Break;
}
}
}
Base. wndproc (ref m );
}
Well, everything is over. Now we have to do something to find a film to enjoy our achievements.