Demo program
Introduction;
For the first time I wrote an article on code introduction, please forgive me for my poor English. I have been in touch with English at school.
I hope everyone can understand the content of this article. If you have any questions, I 'd like to answer them. If any error is found, correct it.-Thank you for helping me learn English ;-).
Now start
This small program demonstrates that a DirectShow playback software developed by C # can play videos and sounds.
This program contains the following controls: mainmenu, toolbar, statusbar, panel, imagelist, and a timer.
These attributes are all filled in the attribute column, so I suggest you download this project, unless you are not a beginner.
On the other hand, the example program contains the DirectShow class used to play videos and sounds.
...
The program demonstrates the following content:
How to Use the openfiledialog class to select a media file on the disk.
How to activate the sang button in the toolbar.
Status Bar information.
How to use a timer control key.
How to Use the clock to control events.
In this way, use the main menu event.
How to Use toolbar events.
How to Use Window events.
How to Use DirectShow to play media.
Determine whether the media has been played.
User Interface
In addition to the three buttons for playing, the video and sound are stopped, and the menu is used to select the media file to be played, so before you want to play a media file, you must open the file in the menu "file-> open.... If the file is loaded successfully, you can play it by pressing the "play" button. During playback, the program displays the playback progress on the status bar. If the playback ends, you can press "play" to play the video again or select another file.
The "info" command of the menu is used to display the information dialog box.
About DirectShow:
We usually use the DirectShow class to play videos and sounds. It is a development tool in DirectX. You can use DirectShow to easily play videos and audio files. to use it, you only need to set the parameters and Methods correctly.
Unfortunately,. NET and C # do not include the official support platform in DirectX, so DirectShow is not included in DirectX 9. We use C # To develop DirectX by using the Visual Basic type library API version 7 to 8. This article describes how to use the DirectX VB class library in C.
Before developing. Net DirectShow, we need to create a reference to DirectShow com DLL. Copy "InterOP. quartztypelib. dll" to your project folder. In Visual Studio. NET, select and add in the project menu.
Press "Browse..." and select DirectShow com DLL.
Using quartztypelib;
Code:
How to Create DirectShow and select a media file?
After selecting "file-> open..." under the main menu, the "open file" dialog box is displayed, and you can select a media file. In C #, create an openfiledialog Class Object and use the showdialog () method.
Openfiledialog = new openfiledialog ();
Openfiledialog. Filter = "media files | *. mpg; *. Avi; *. wma; *. mov;" +
"*. Wav; *. MP2; *. MP3 | all files | *.*";
If (dialogresult. OK = openfiledialog. showdialog ())
{
.
.
.
After completing the preceding steps, we began to establish DirectShow and render the media file.
Follow these steps:
Create Image filtering Management (FGM)
Create Image filtering (through FGM)
Play images and return events
The following code demonstrates how to create image filtering management and image filtering:
Collapsecleanup ();
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;
Through cleanup (), we delete objects if they exist. When we can start rendering a file, we must create several new objects of filtergraphmanager to use the new method. The renderfile () method creates an image filter to render a specified file. The ibasicaudio class is used to set the sound size and quality. The ivideowindow class is used to set the window style and position. This function is appended. If you play a sound file, you call this method. However, the ivideowindow class is not required for playing audio files, so m_objvideowindow is set to null. The imediaevent and imediaeventex classes are used to listen for messages and send DirectShow information to the parent window. You can set the current position through the imediaposition class. The imediacontrol class is used to control the start and stop video and sound playback.
For more information about DirectShow, read the msdn documentation.
How to play media files?
Use the run () method of the imediacontrol class to start playing a video or sound file.
M_objmediacontrol.run ();
How to pause playback?
If you want to pause playing a video or sound file, use the pause () method of the imediacontrol class.
M_objmediacontrol.pause ();
How to stop playing?
Use the stop () method of the imediacontrol class to stop playing a video or sound.
M_objmediacontrol.stop ();
How can I get the playback progress and duration of a file?
When a media file is played, we specify the current playback progress and file length in the status bar. We read the current progress of the imediaposition class within ms and display the size in the status bar. To get the file length, we read the duration member variable of the imediaposition class.
Private void timerjavastick (Object sender, system. eventargs E)
{
If (m_currentstatus = mediastatus. Running)
{
Updatestatusbar ();
}
}
The time function calls the updatestatusbar () method every MS to display the current position and file progress.
Collapseprivate 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 ";
}
}
What happens when the playback ends?
To determine that the file is in the end state, we have rewritten the wndproc function to process the ec_complete message. When the file ends playing, The DirectShow message will be sent to the window.
Collapseprotected 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 );
}