Visual C # video playback using DirectX

Source: Internet
Author: User
Many people first came to the idea that DirectX is mostly played through games. The reason for installing and upgrading DirectX is nothing more than meeting the needs of game operation. Direct X is a multimedia API function library launched by Microsoft for the Windows platform. It provides standard interfaces to interact with graphics cards, sound cards, and input devices. Without this set of standard API function libraries, you need to write different code for each combination of graphics cards and sound cards and for each type of keyboard, mouse, and game lever. This does not return to the previous Dos era. To solve this problem, Microsoft launched DirectX. DirectX abstracts a set of general commands from specific hardware and converts them into specific hardware commands. In this way, the development language can call the unified standard Direct X function library to operate each video card, sound card combination, and each type of keyboard, mouse, game rod, and other multimedia.

  
1. Introduction to Direct x sdk 9.0 installation and Class Library:

  
. Net FrameWork SDK does not contain Direct x sdk, so in order to smoothly complete this article,
You must first download and install the Direct x sdk. Specific: http://download.microsoft.com/download/a/c/d/acdfb557-266f-4af4-8673-6ab8009b4ddd/dxsdk_apr2005.exe. This version is an English version launched in 2005. This does not prevent the following program implementation. After Direct X is installed, "C: \ WINDOWS \ Microsoft. NET \ Managed DirectX ", the Directory should contain nine DLL files and nine XML files. The nine DLL files correspond to 10 namespaces in DirectX. In programming, namespaces are used to provide support for input devices, sounds, network playback, and graphics. The namespace defined in Direct x sdk 9.0 and its main functions are shown in Table 01:

Namespace Description
Microsoft. DirectX Public class and Mathematical Structure
Microsoft. DirectX. Direct3D 3D graphics and Assistant Library
Microsoft. DirectX. DirectDraw Direct Draw graphics API. This is an old-style namespace and does not need to be used now.
Microsoft. DirectX. DirectPlay Network API for multi-player games
Microsoft. DirectX. DirectSound Sound support
Microsoft. DirectX. DirectInput Input Device Support (for example, mouse and game lever)
Microsoft. DirectX. AudioVideoPlayback Play the video and audio (for example, play the animation file of the video on the computer)
Microsoft. DirectX. Diagnostics Troubleshooting
Microsoft. DirectX. Security Access Security
Microsoft. DirectX. Security. Permissions Access Security Permissions

       
Table 01: namespace defined in Direct X SDK9.0 and its main functions

   
Direct X is rich in content. The following describes only a small application, that is, using Visual C # To call the SDK in Direct X to play video files, only the namespace "Microsoft. directX. audioVideoPlayback ". Microsoft. DirectX. AudioVideoPlayback namespace defines three classes: "Audio", "Video", and "TextureRenderEventArgs ". The first two categories are the most commonly used to support audio and video. Common attributes defined in the Microsoft. DirectX. AudioVideoPlayback namespace and their specific functions can be found in Table 02:

Attribute Description
Audio Obtains the audio objects in a video file for subsequent audio playback.
Caption Gets or sets the name of the video to be played on the Form.
CurrentPosition Obtains or sets the current position of the video to be played.
DefaultSize Obtains the default video size for video playback.
Fullscreen Obtains or sets whether the video file is played in full screen mode.
IsCursorHidden Obtain the mouse status when playing a video: Hide or show
Owner Obtains or sets the host component for video playback.
Paused Checks whether the current playback status is paused.
Playing Checks whether the current playback status is in the playing status.
SeekingCaps Obtain search performance
Size Retrieves and sets the size of the video for playback.
State Obtains the current playback status.
Stopped Obtains whether the current playback status is stopped.
StopPosition Obtains the stop position of a video.

    
Table 02: common attributes defined in the Microsoft. DirectX. AudioVideoPlayback namespace and their specific functions

  
Common methods defined in the Microsoft. DirectX. AudioVideoPlayback namespace and their specific functions can be found in Table 03:

Method Description
HideCursor Hide the mouse of the current video
Open Load new files to the Video object
Pause Set to paused.
Play Set to playback status
SeekCurrentPosition Search and transfer to the specified playing position
SeekStopPosition Set a new stop location
ShowCursor Displays the mouse of the current video.
Stop Set to stop playing
Video Initialize a new Video instance.

    
Table 03: common attributes defined in the Microsoft. DirectX. AudioVideoPlayback namespace and their specific functions

  
After learning the basic knowledge above, we will introduce the specific implementation of Visual C # Calling Direct X to play video files.

  
II. Environment for program design, debugging, and running in this article:

  
(1). Microsoft Windows 2003 Chinese Enterprise Edition.

  
(2). Visual Studio. Net 2003 enterprise build,. Net FrameWork SDK 1.1 version 4322.

  
Iii. Visual C # video playback using Direct X

  
Visual C # using Direct X to implement video playback is difficult:

  
1. Learn how to introduce the Dirext X function library in Visual C.

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

  
3. Set Basic playback commands: Play, stop, pause, and so on.

  
The following describes how to play a video using Visual C # Direct X:

  
1. Start Visual Studio. Net.

  
2. Select File, new, and project. The new project dialog box is displayed.

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

  
4. Set template to Windows application ].

  
5. In the "name" text box, enter "use DriectX for media playback in Visual C ].

6. Enter the "E: \ VS. NET Project" in the "location" text box, and click "OK. In this case, in "E: \. the. NET project "directory creates a folder named" using DriectX in Visual C # For media playback, it stores all the files in the "using DriectX for media playback in Visual C #" project.

  
7. Select solution manager | reference, right-click, and select Add reference, as shown in Figure 01:


Figure 01: Add reference

8. select the Add reference menu, and the Add reference dialog box is displayed. As shown in Figure 02, add "Microsoft. directX. after AudioVideoPlayback, click OK to use Microsoft. directX. audioVideoPlayback "file succeeded. This is because Visual Studio. net does not contain "Microsoft. directX. audioVideoPlayback. dll "file, but the program uses" Microsoft. directX. audioVideoPlayback "namespace, so you must use" Microsoft. directX. the file where AudioVideoPlayback is located.

  
9. set Visual Studio. switch the current window of Net to the Form1.cs window, drag the following components to the design form from the Windows Forms components tab in the toolbox, and perform the corresponding operations:

   
An OpenFileDialog component is used to select the video to be played, a panel component is used as the host component for video playback, and four buttons are used to open, play, pause, and stop the video respectively. Drag the four components into the design window and double-Click them. Then, the system generates the processing code corresponding to the Click events of the four components in Form1.cs.

  
10. set Visual Studio. the current window of Net switches to the code editing window of Form1.cs. In the code section of the Form1.cs header introducing the namespace, replace the imported 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;
// Introduce the Direct X namespace to be used for Video Playback

  
11. Add the following code to the code area of Form1.cs class. The following Code creates a Global Video instance:

Private Video MyVideo = null;
// Create a Video instance

  
12. The following code replaces the InitializeComponent process generated by the system. The following code initializes the components added to the form and the created global variables, and defines the Click events of the four Button components and the Load events of Form1:

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. button#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 = "paused ";
 
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 = "select the AVI file to be played ";
 
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 = "using DriectX in Visual C # For media playback ";
 
This. Load + = new System. EventHandler (this. form#load );
 
This. ResumeLayout (false );
}

  
Now, the interface design and function implementation of the [Use DriectX for media playback in Visual C #] project have been completed, as shown in Figure 03:


Figure 03: UI design of the project [using DriectX for media playback in Visual C #]

13. use the following code to replace the processing code corresponding to the Click Event of the button1 component in Form1.cs. The following code is used to open the selected video file and display the start frame on the defined Panel component:

Private void button#click (object sender, System. EventArgs e)
{
 
OpenFileDialog1.InitialDirectory = Application. StartupPath;
 
If (openFileDialog1.ShowDialog () = DialogResult. OK)
 
{
  
// Record the size of the panel component
  
Int height = panel1.Height;
  
Int width = panel1.Width;
  
// If an opened Video file exists, release it.
  
If (MyVideo! = Null)
  
{
   
MyVideo. Dispose ();
  
}
  
// Open a new Video file
  
MyVideo = new Video (openFileDialog1.FileName );
  
// Allocate the Video file to the created Panel component.
  
MyVideo. Owner = panel1;
  
// Redefines the size of the record panel component
  
Panel1.Width = width;
  
Panel1.Height = height;
  
// Play the first frame of the AVI file, mainly to display it in the panel
  
MyVideo. Play ();
  
MyVideo. Pause ();
 
}
 
// Determine the status of buttons in the form
 
If (MyVideo = null)
 
{
  
Button2.Enabled = false;
  
Button3.Enabled = false;
  
Button4.Enabled = false;
 
}
 
Else
 
{
  
Button2.Enabled = true;
  
Button3.Enabled = true;
  
Button4.Enabled = true;
 
}
}

  
14. Use the following code to replace the processing code corresponding to the Click Event of the button2 component in Form1.cs. The following code functions to play the currently opened video file:

Private void button2_Click (object sender, System. EventArgs e)
{
 
If (MyVideo! = Null)
 
{
  
MyVideo. Play ();
 
}
}

  
15. Use the following code to replace the processing code corresponding to the Click Event of the button3 component in Form1.cs. The following code is used to pause playing the currently opened video file:

Private void button3_Click (object sender, System. EventArgs e)
{
 
If (MyVideo! = Null)
 
{
  
MyVideo. Pause ();
 
}
}

  
16. Use the following code to replace the processing code corresponding to the Click Event of the button4 component in Form1.cs. The following code stops playing the currently opened video file:

Private void button4_Click (object sender, System. EventArgs e)
{
 
If (MyVideo! = Null)
 
{
  
MyVideo. Stop ();
 
}
}

  
17. After the Click event of button4, add the following code to initialize the button component in the Form:

// Initialize the status of buttons in the 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. Now, after the above steps are completed correctly and all are saved, all the work of the [Use DriectX in Visual C # For media playback] project is completed. Click the shortcut key [F5] to run the program. Figure 04 shows the program running interface obtained when the video file is played:


Figure 04: running interface of the project [using DriectX for media playback in Visual C #]

  
Iv. Summary:

   
The emergence of Direct X indeed solved a lot of underlying work of programmers and handed over the work of various complicated hardware to Direct X. Although Direct X has evolved from an unmanaged version to the current managed version, the debate persists, however, more and more developers use hosted Direct X or use managed and unmanaged Direct X development together, and prove the vitality of managed Direct X from another aspect. This article introduces Visual C # using an example to call an unmanaged Direct X method for video playback. This is only a small application for hosted Direct X. Finally, I hope this article will be helpful to those who want to know and learn about how to call Direct X to write game programs in Visual C.

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.