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 combinations for each type of video card and sound card and for each type of keyboard, mouse, and game lever.Code. 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 followingProgram. 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 graphic 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 files of each 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 |
obtain the audio objects in the video file for subsequent audio playback |
caption |
get or set the name of the video to be played on the form |
currentposition |
get or set the current position of the video to be played |
defaultsize |
get the default video size for playing a video |
fullscreen |
obtain or set whether the video file is played in full screen mode |
iscursorhidden |
the mouse state when obtaining the video to play: Hide or display |
owner |
obtains or sets the host component for video playback |
paused |
check whether the current playback status is paused |
playing |
check 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 |
get the current playback status |
stopped |
check whether the current playback status is stopped |
stopposition |
obtain the stop position of the video to be played |
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. BenArticleProgramming, debugging, and running environment:
(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:
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.
Error cause:
This problem occurs when you compile a very simple player program in vs2005. Compilation is OK. When I run debug, when constructing a video object, the system reports the "loaderlock was detected" error, and the program will die. In fact, this problem was also encountered when running a program in debug last time, but I did not study it last time and gave up directly. :) Symptom: loaderlock was detected message: DLL "c: \ windows \ Assembly \ GAC \ Microsoft. directX \ 1.0.2902.0 _ 31bf3856ad364e35 \ Microsoft. directX. DLL "is trying to execute managed code within the OS loader lock. Do not try to run the managed code in the dllmain or image initialization function. This will cause the application to be suspended. The error code is located as follows: // open a new video file myvideo = new video (openfiledialog1.filename); check the information online :. in net2.0, 42 powerful debugging assistants (MDA and loaderlock) are added. Loaderlock detects the running of hosted code on a thread with the loader lock of the operating system (the video running thread in the example above. This may cause deadlocks and may be used before the operating system loader initializes the DLL. Although this is necessary, it is too troublesome for development and debugging. Solution: 1. modify the Registry in HKEY_LOCAL_MACHINE \ SOFTWARE \ Microsoft \. add a string under netframework with a value of "0". However, in this case, all. net2.0 development does not get the benefits of MDA. 2. add a configuration file for the project. 3. directly modify the simplest method of Project Settings. In the "debug" menu ---- "exceptions" ---- "managed debugging assistants", uncheck "loaderlock".) debug, OK, all done