Visual C # uses DirectX to achieve video playback

Source: Internet
Author: User
Tags reference visual studio
visual| Video Playback

Many people first contact with DirectX is through the game, as for installation, upgrade DirectX is nothing more than to meet the needs of game operation. Direct x is actually a library of Windows platform Multimedia API functions, which provides a standard interface to interact with graphics cards, sound cards, input devices, and so on. Without this set of standard API libraries, you would need to write different code for each video card, for each combination of sound cards, and for each type of keyboard, mouse, and joystick. This is not back to the previous DOS era. To solve this problem, Microsoft has launched DirectX. DirectX abstracts from specific hardware and converts a set of generic instructions to specific commands for hardware. In this way, the development language can manipulate each video card, each combination of sound card and each type of keyboard, mouse and joystick, by invoking the unified standard Direct X function library.

I. Direct X SDK 9.0 Installation and Class Library introduction:

The. Net FrameWork SDK does not contain the direct X SDK, so for the sake of completing the following article,
You must first download and install the direct X SDK. The specific download address is: Http://download.microsoft.com/download/a/c/d/acdfb557-266f-4af4-8673-6ab8009b4ddd/dxsdk_apr2005.exe. This version is the English version of the 20,054 month launch. This does not prevent the following programs from being implemented. After you install Direct X, there is a directory with "C:\WINDOWS\Microsoft.NET\Managed DirectX" in which there should be nine DLL files and nine XML files in the directory. Nine DLL files roughly correspond to the 10 namespaces in DirectX. In programming, you use the namespaces to provide support for input devices, sounds, network playback, graphics, and so on. The namespace defined in Direct X SDK 9.0 and its primary role are detailed as shown in table 01:

Name space Describe
Microsoft.directx Public classes and mathematical structures
Microsoft.DirectX.Direct3D 3D Graphics and helper libraries
Microsoft.DirectX.DirectDraw Direct Draw Graphics API. This is a legacy namespace and you don't need to use it now.
Microsoft.DirectX.DirectPlay Network APIs for multiplayer games
Microsoft.DirectX.DirectSound Voice support
Microsoft.DirectX.DirectInput Input device support (for example, mouse and joystick)
Microsoft.DirectX.AudioVideoPlayback Play video and audio (for example, play the respective video animation files on your computer)
Microsoft.DirectX.Diagnostics Troubleshoot
Microsoft.DirectX.Security Access security
Microsoft.DirectX.Security.Permissions Access security permissions

Table 01:direct the defined namespaces in X SDK9.0 and their primary role

Direct x is very informative, and this is just one of the small applications that uses Visual C # to invoke the SDK in direct X to play a video file as an example, using only namespaces in the program Microsoft.DirectX.AudioVideoPlayback ". Three classes are defined in the Microsoft.DirectX.AudioVideoPlayback namespace: "Audio", "video" and "Texturerendereventargs". The first two classes are the most common and are used to support audio and video, respectively. The common properties defined in the Microsoft.DirectX.AudioVideoPlayback namespace and their specific effects are shown in table 02:

Property Describe
Audio Gets the audio object in the video file that can be used for subsequent audio playback
Caption Gets or sets the name of the video to play on the form
CurrentPosition Gets or sets the current location of the playback video
DefaultSize Get the default video size to play video
Fullscreen Gets or sets whether the video file plays in Full-screen mode
Iscursorhidden Gets the state of the mouse when the video is played: hidden or displayed
Owner Gets or sets the host component for video playback
Paused Gets whether the current playback state is paused
Playing Gets whether the current playback state is playing.
Seekingcaps Gets whether performance can be searched
Size Retrieves and sets the size of the for playback.
State Get the current playback status
Stopped Gets whether the current playback state is in a stopped state
Stopposition Gets the stop playback location of the video that is playing

Table 02:microsoft.directx.audiovideoplayback Common attributes defined in namespaces and their specific functions

The common methods defined in Microsoft.DirectX.AudioVideoPlayback namespaces and their specific effects are shown in table 03:

Method Describe
Hidecursor Hide the mouse that is currently playing video
Open Load a new file into a video object
Pause Set to suspend playback status.
Play Set to playback status
Seekcurrentposition The search goes to the established playback location
Seekstopposition To set a new stop position
ShowCursor Displays the mouse that currently plays the video
Stop Set to stop playback status
Video Initializes a new video instance

Table 03:microsoft.directx.audiovideoplayback Common attributes defined in namespaces and their specific functions

With these basics in the background, let's begin by introducing the specific implementation of Visual C # calling Direct X to play video files.

  two. The program design, commissioning and operation Environment of this article:

(1) Microsoft company Windows 2003 Chinese Enterprise Edition.

(2). Visual Studio. NET 2003 Enterprise build,. NET FrameWork SDK 1.1 version number 4322.

Three Visual C # implements video playback using Direct X


Visual C # uses direct X to achieve video playback difficulties in the following points:

1. Master the method of introducing the Dirext x function library in Visual C #.

2. Set the host component for direct X video playback.

3. Basic Playback Command settings: Play, stop, pause, etc.

Here's how Visual C # implements video playback with Direct x:

1. Start visual Studio. Net.

2. When you select the menu "file" | "new" | "Project", The New Project dialog box pops up.

3. Set the project type to Visual C # project.

4. Set the template to Windows application.

5. In the Name text box, enter use DriectX in Visual C # to implement media playback.

6. Enter the E:\VS in the text box in the location. NET project, and then click the OK button. So in "E:\VS." NET project directory, you create a folder called "Media playback using DriectX in Visual C #" that contains all of the files in the Visual C # use DRIECTX for media playback project.

7. Select Solution Manager | references, right-click, and select the Add Reference menu, as shown in Figure 01:


Figure 01: Adding a reference


8. After selecting the Add Reference menu, pop-up the Add Reference dialog box, as shown in Figure 02, in the Selected Components column, after adding "Microsoft.DirectX.AudioVideoPlayback", click "OK" button, reference " Microsoft.DirectX.AudioVideoPlayback "file succeeded. This is because visual Studio. NET, there is no "Microsoft.DirectX.AudioVideoPlayback.dll" file in the default compilation environment, and the "Microsoft.DirectX.AudioVideoPlayback" namespace is used in the program. So to refer to the file where "Microsoft.DirectX.AudioVideoPlayback" is located in the compilation environment.


9. Take visual Studio. NET to the Form1.cs window, and from the Windows Forms Components tab in the Toolbox, drag the following components into the design form and perform the appropriate actions:

A OpenFileDialog component that selects the video to play; a panel component that acts as the host component of the video playback, and four button buttons that perform video opening, playback, pausing, and stopping operations, respectively. And when these four components are dragged into the design window and double-click them respectively, the system will produce the processing code for the four component click events in Form1.cs respectively.

10. Take visual Studio. NET's current window switches to the Form1.cs Code editing window, and in Form1.cs's header, the code area that introduces the namespace, replaces the introduction of the namespace code automatically generated by the system in Form1.cs with the following code:

Using System;
Using System.Drawing;
Using System.Collections;
Using System.ComponentModel;
Using System.Windows.Forms;
Using System.Data;
Using Microsoft.DirectX.AudioVideoPlayback;
Direct X namespace to be used for introducing video playback


11. Add the following code in the code area of the Form1.cs class, the following code is a video instance that creates a global use:

Private video myvideo = null;
Create a video instance


12. Replace the system-generated InitializeComponent process with the following code. The following code initializes and defines the click events and Form1 load events for the components that are joined to the form, as well as the global variables created, for four button components:

private void InitializeComponent ()
{
This.panel1 = new System.Windows.Forms.Panel ();
This.button1 = new System.Windows.Forms.Button ();
This.button2 = new System.Windows.Forms.Button ();
This.button3 = new System.Windows.Forms.Button ();
This.button4 = new System.Windows.Forms.Button ();
This.openfiledialog1 = new System.Windows.Forms.OpenFileDialog ();
This. SuspendLayout ();
This.panel1.Dock = System.Windows.Forms.DockStyle.Top;
This.panel1.Location = new System.Drawing.Point (0, 0);
This.panel1.Name = "Panel1";
This.panel1.Size = new System.Drawing.Size (540, 346);
This.panel1.TabIndex = 0;
This.button1.Location = new System.Drawing.Point (62, 380);
This.button1.Name = "Button1";
This.button1.Size = new System.Drawing.Size (80, 38);
This.button1.TabIndex = 1;
This.button1.Text = "open";
This.button1.Click + = new System.EventHandler (This.button1_click);
This.button2.Location = new System.Drawing.Point (165, 380);
This.button2.Name = "Button2";
This.button2.Size = new System.Drawing.Size (81, 38);
This.button2.TabIndex = 1;
This.button2.Text = "Play";
This.button2.Click + = new System.EventHandler (This.button2_click);
This.button3.Location = new System.Drawing.Point (268, 380);
This.button3.Name = "Button3";
This.button3.Size = new System.Drawing.Size (80, 38);
This.button3.TabIndex = 1;
This.button3.Text = "Pause";
This.button3.Click + = new System.EventHandler (This.button3_click);
This.button4.Location = new System.Drawing.Point (371, 380);
This.button4.Name = "Button4";
This.button4.Size = new System.Drawing.Size (81, 38);
This.button4.TabIndex = 1;
This.button4.Text = "Stop";
This.button4.Click + = new System.EventHandler (This.button4_click);
THIS.OPENFILEDIALOG1.DEFAULTEXT = "avi";
This.openFileDialog1.Filter = "Video file |*.avi| |";
This.openFileDialog1.Title = "Please choose to play AVI file";
This. AutoScaleBaseSize = new System.Drawing.Size (6, 14);
This. ClientSize = new System.Drawing.Size (540, 461);
This. Controls.Add (This.button1);
This. Controls.Add (THIS.PANEL1);
This. Controls.Add (This.button2);
This. Controls.Add (This.button3);
This. Controls.Add (THIS.BUTTON4);
This. FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog;
This. MaximizeBox = false;
This. Name = "Form1";
This. Text = "Use DriectX to achieve media playback in Visual C #";
This. Load + = new System.EventHandler (this. Form1_Load);
This. ResumeLayout (FALSE);
}


The preliminary work on the interface design and function realization of the project using DRIECTX for media playback in Visual C # is complete, as shown in Figure 03 of the Design interface:


Figure 03: Interface Design for the project "media playback using DriectX in Visual C #"

13. Replace the processing code for the Click event of the Button1 component in Form1.cs with the following code, which is the function of opening the selected video file and displaying the first frame on the defined Panel component:

private void Button1_Click (object sender, System.EventArgs e)
{
Openfiledialog1.initialdirectory = Application.startuppath;
if (openfiledialog1.showdialog () = = DialogResult.OK)
{
Record the size of a panel component
int height = Panel1. Height;
int width = Panel1. Width;
If there is an open video file, release it
if (Myvideo!= null)
{
Myvideo.dispose ();
}
Open a new video file
Myvideo = new Video (openfiledialog1.filename);
Assign the video file to the panel component you created
Myvideo.owner = Panel1;
Redefined by the size of the recorded panel component
Panel1. width = width;
Panel1. Height = height;
Play the first frame of an AVI file, mainly to display in a panel
Myvideo.play ();
Myvideo.pause ();
}
To determine the status of each button in a form
if (Myvideo = null)
{
Button2. Enabled = false;
Button3. Enabled = false;
Button4. Enabled = false;
}
Else
{
Button2. Enabled = true;
Button3. Enabled = true;
Button4. Enabled = true;
}
}

14. Replace the processing code for the Click event of the Button2 component in Form1.cs with the following code to play the currently open video file:

private void Button2_Click (object sender, System.EventArgs e)
{
if (Myvideo!= null)
{
Myvideo.play ();
}
}

15. Replace the processing code for the Click event of the Button3 component in Form1.cs with the following code, which pauses playback of the currently open video file:

private void Button3_Click (object sender, System.EventArgs e)
{
if (Myvideo!= null)
{
Myvideo.pause ();
}
}

16. Replace the processing code for the Click event of the Button4 component in Form1.cs with the following code, and the following code stops playing the currently open video file:

private void Button4_Click (object sender, System.EventArgs e)
{
if (Myvideo!= null)
{
Myvideo.stop ();
}
}

17. After the click event of the Button4, add the following code, which is the ability to initialize the button component of the form form:

Initialize the status of each button in a form
private void Form1_Load (object sender, System.EventArgs e)
{
if (Myvideo = null)
{
Button2. Enabled = false;
Button3. Enabled = false;
Button4. Enabled = false;
}
Else
{
Button2. Enabled = true;
Button3. Enabled = true;
Button4. Enabled = true;
}
}

18. At this point, all of the work on the "use DriectX for media playback in Visual C #" project is complete after the steps are completed correctly and saved. When you click the shortcut "F5" to run the program, figure 04 is the program run interface that you get when you play the video file:


Figure 04: The running interface of the project "using DRIECTX to implement media playback in Visual C #"

   Four. Summary:

The advent of direct X did solve many of the programmer's low-level work, handing the work of the various tumultuous hardware to direct X. Although Direct X has evolved from an unmanaged version to a current managed version, the controversy persists, but more and more developers are developing their software with managed Direct X or using managed and unmanaged direct X in combination, as well as proving the viability of managed Direct X from another aspect. This article combines an example to specifically describe how Visual C # calls unmanaged Direct X to implement video playback, which is just one small application for managed Direct X. Finally, I hope this article will help friends who want to learn about calling Direct X to write a game program in Visual C #.



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.