Media player made with DirectShow in C #

Source: Internet
Author: User
Tags filter format
Media

Media player made with DirectShow in C #

Overview:
My program is just to tell you how to use DirectShow in C # to do a player, the world can not
There are too many functions. Maybe you can solve the problem if you just spend five minutes. Yes, if you're using the IDE, I'm sure.
Everything is just using your mouse to set some things in your designer midpoint point attribute can be simply done. Of course.
Or a little bit of code. At least about the DirectShow interface. For example, the screen and sound.

Minor problems in the program:
1. How to open a media file from your disk
2. How to enable and Disable buttons on the sidebar
3. How to set the status bar display text
4. How to control time
5. How to use the events of the Time control
6. How to use DirectShow to play media files
7. How to determine the playback status and so on ...
...

The user interface is shown below:
It's simple, isn't it? Yes, I said it was a simple procedure, I just let it have the basic function. On the toolbar
Three button controls play, stop, and Pause. A File menu is used to open the media file and close the program. Of course.
There is also an introduction to the program's info which is undoubtedly the most basic interface configuration.

The DirectShow interface is described below

Playing video and sound files we need to use the DirectShow component that DIECTX provides for us. Use this interface
Allows you to easily play those shared images and sound files. All you have to do is install the DirectShow interface.
and use its functional functions and configure the correct interface parameters just.


Unfortunately, it's. NET does not officially support DirectX. Yes, maybe you heard about DirectX9 support, right? Yes, but in
The final version of the day has not come, we have not been the best results. But we're going to use it anyway, right?
Or this article will be voided. Yes, maybe you used VB, right, that's it, we just need to use that.

Well, we have to do something before that. I think you've guessed it, right? Do you remember Xcopy?
. NET's advantages. Come on, come on, put this "Interop.QuartzTypeLib.dll" DLL in, like this.
See the diagram below? It's very simple.

Finally, don't forget to see if there's a sentence in your code
Using QuartzTypeLib;
Is that right? The compiler adds for you, if you don't add it yourself.

The preparation work is over, it is the code part, this is the responsibility that the programmer cannot shirk. Otherwise we all have to be laid off.

Program Implementation section:

How to open the media file you want?

Do you remember? " File-> Open ... " Yes, almost everyone who uses Windows does this. How do I achieve this?
It's easy to look at the following code:


OpenFileDialog OpenFileDialog = new OpenFileDialog ();

Openfiledialog.filter = "Media files|*.mpg;*.avi;*.wma;*.mov;*.wav;*.mp2;*.mp3| All Files|*.* ";

if (DialogResult.OK = = Openfiledialog.showdialog ())
{
.
.
.
See, it's simple, right? Remember to write a function to put it in. When you click the OK button. The DirectShow interface will
Get the file you want to play.
Look at the picture below and you'll know how it works.


DirectShow provides the most basic service for multimedia streaming playback, which can be local files, and can be transmitted by the server. In particular, DirectShow can support video playback, compressing video content in different file and streaming formats, including Windows Media, MPEG, AVI, and WAV.

At the heart of DirectShow, a service is a modular set of components, called filters, that can be arranged into filter graphs according to media types. Filters can manipulate data streams, such as reading, parsing, decoding, formatting, or rendering.

Filters are arranged in a tree, called a filter tree, managed by the Filter Tree Manager (Filter Graph Manager, referred to as FGM). You can use the FGM application to control the filter tree indirectly by using the Microsoft Windows Media Player control, or you can control it directly by invoking the COM interface method. The DirectShow filter tree (see Figure 1) consists of a sequence of forward filters from the source to the target renderer, all connected by the input and output filter pins. The filter pins negotiate which media types they will support. FGM controls the multimedia data flow between the tree filters. Because DirectShow has a flexible, reconfigurable filter tree architecture, DirectShow can use the same software component to support playback and streaming of multiple media types. Developers can also extend DirectShow multimedia support by writing their own filters.


Figure 1. DirectShow Filter Tree


Filtering device
A filter is a registered DirectShow class that performs many media processing tasks. These tasks include:

Get source information (for example, get media streams)
Analysis (for example, performing packet read, detach, and format on a stream)
Transformations (e.g., decoding WMA and MPEG-4 audio and video streams)
Rendering (for example, generating audio PCM or video RGB/YUV output at the appropriate time, passing data to DirectSound and DirectDraw)
Filters use several types of interfaces, such as pins, counters, transporter, and clock interfaces, to perform their tasks. The filter implements and opens many interfaces. FGM can use these interfaces to create, connect, and control trees. Filters often implement Ibasefilter interfaces that contain the following methods:

Run, stop, and pause the filter state.
Recover filter and vendor information.
Get and set reference clock.
Restores filter state information.
Enumerates the filter leads.
Locating pins when rebuilding filter tree

Now that you've introduced so much, your hand may have restless. 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_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, Pause, stop?
Simple These functions look literal also know.

M_objmediacontrol.run ()//Play

M_objmediacontrol.pause ()/Pause

M_objmediacontrol.stop ()/Stop

OK, how do we control the time schedule?
//
private void Timer1_Tick (object sender, System.EventArgs e)
{
if (M_currentstatus = = mediastatus.running)
{
Updatestatusbar ();
}
}

See the above Updatestatusbar (); This is to let it not 100ms update the status bar. 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";
        Case MediaStatus.Stopped:statusBarPanel1.Text = "Stopped";
   }

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 problem program how to know it played out????
It's going to be a bit of a hassle. Well, think about what you can do. By the way, Windows is message driven. Well, look for something.
Well, some ec_complete this. Remember "WndProc" it?? Yes, my old friend. This time we must
To overwrite it to capture the Ec_complete message. This message is DirectShow notify the parent form, playback 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);
}
Well, it's all over, and now it's time to do something to find a film to enjoy your own results.



Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.