Abstract:Learn how to use the DirectShow control in Microsoft Visual C #. NET and develop a media player. Follow the procedure described in this article to create a simple Visual C # application for playing digital audio and video.
Introduction
Microsoft Visual C # is the most popular programming language in the world, using the latest version of Visual C #.. NET, you can quickly and effectively develop applications based on Windows Forms, or embed Microsoft Windows Media? Player 9 Series ActiveX controls add new, interesting, and useful functions to applications.
The DirectShow control is a standard ActiveX control that provides a large number of functions. The DirectShow control provides the following functions:
· Advanced playback of digital media files and streaming media.
· Use the playlist function.
· Playback of DVDs and CDs.
· Access the Media Library in Windows Media Player ).
· Metadata processing function.
· Supports subtitles.
· Supports audio in multiple languages.
· Control network connectivity and access statistics.
Next, let's take a look at the goal of constructing this Media Player. Once the goal is determined, the amount of code and the complexity of the program are determined. In this article, media players must achieve the following goals:
· It is a simple AWT application driven by menus.
· Contains a "file" menu. the File menu contains three menu items:
· "Open" is used to open a media file.
· "Loop" indicates whether to play the video once (default) or repeatedly.
· "Exit" and exit the program.
· It can run on multiple platforms.
· Core functions are implemented through the JMF (Java Media Framework) API.
Follow the steps described in this article to create a basic application based on Windows Forms and embed the Player control in it. The example application you created has the following features:
· Create an instance of the DirectShow control.
· The main interoperability assembly of Windows Media Player provides the Component Object Model (COM) interoperability.
· Allows users to open and play Windows Media files, especially files with the extension. wma or. wmv.
· Create a transfer control button for users to play, pause, and stop digital media content.
· Display the title of the current digital media file.
· Demonstrate how to use the Player object model, including examples of using attributes, methods, and events.
My program only tells you how to use DirectShow to make a player in C,
In this program, we need to solve some minor problems:
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...
Displays the application you want to create, where a digital audio file named "Melow" is being played and visualized.
· This article assumes that you already have some knowledge about the integrated development environment of Visual C # and Visual Studio. NET.
Preparations
Before creating an application, you need to install necessary software and register the primary interoperability assembly (QuartzTypeLib ).
Here is a brief introduction to the DirectShow interface:
We need to use the DirectShow component provided by DiectX to play the video screen and audio files. you can use this interface to conveniently play shared images and sound files. all you need to do is install the DirectShow interface, use its functions, and configure the correct interface parameters.
Unfortunately,. NET does not officially support DirectX. Yes, maybe you heard that DirectX9 supports it, right? Yes, but we haven't gotten the best results since the final version was finalized. 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.
Start Project
After installing necessary software and registering QuartzTypeLib, you can start Visual C # And start creating a project for the sample application. Next I will introduce the operation steps of this process.
Create a project
To create an empty project, follow these steps:
1. Start Visual Studio. NET and click New Project ).
2. in the Visual C # Projects (Visual C # project) folder, click Windows Application, type the name of the new project (preferably DirectShow), and click OK ).
Visual C # use the default Windows form "Form1" to create a new project.
3. This name does not have any special significance or use. Therefore, change the form name to frmPlayMedia in the Properties window and the form text to "media player ".
Add a reference to DirectShow in the project
Follow these steps to add a reference to DirectShow in the project:
1. Open the Visual Studio toolbox and click Components to display the panel.
2. Right-click the Panel and click Customize Toolbox to display the dialog box.
3. On the COM Components (COM component) tab, select Interop. QuartzTypeLib. dll. (If Interop. QuartzTypeLib. dll is not listed for some reason, Click Browse [Browse] and find the file QuartzTypeLib. dll .)
4. Click OK to close the dialog box.
To use DirectShow in code, you need to add a line of code to reference the DirectShow namespace. At the top of the form code window, add the following code before all declaration statements:
The using statement must be after all Options statements (not used in this project) and before all other code. After the statement is added.
Develop applications
After creating a Player control instance connected to the Framework through PIA, you can add other elements to the form and write the code to complete the actual operation.
Add Windows Forms Control
1. in the View menu, click Designer or click View Designer in Solution Explorer to switch to Form Designer.
2. Add a file, playback, and information menu to the form.
3. In the Windows Forms Panel of the toolbox, add a toolbar, a status bar, and an image imageList to your form.
4. In the Properties window, change the toolbar name to toolBar1 and add four Buttons on Buttons. Change the status bar name to statusBar1 and add three panels respectively.
5. On the panel of the toolbox, add a panel version panel1.
6. Add a timer timer1.
7. Adjust the layout of controls in the form to meet your needs and facilitate your use. Is the form layout completed in Visual Studio Designer.
Write code
How to open the media file you want?
The first step is to write the code to open the Windows Media File in frmPlayMedia. To automatically switch to the Code view and edit the Code of the Click event handler (menuItem2_Click) that opens the menu, double-Click the "toolbar" button on the form. Add the following code to the event handler:
Remember, "file-> open..." Yes, almost everyone using windows will do this. How can this be done?
Let's take a 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 ()) { .... } |
Look, it's easy, 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. Explains 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 use the Mic