MFC MCIWndCreate & Play Video Files

1. Introduction to MCI & MCIWndCreate

In this article, we will see how we play an AVI video file in MFC Application. To play the AVI file, we will use the Media Control Interface (MCI)  function and its API function MCIWndCreate. The Animate control that ships with MFC can play the AVI files, but you will not get any sound when the video is playing. OK. Let us start with this simple and quick example that plays the video file comp.avi. The AVI file that I am packing with this example has no sound information, so replace that with any video file, which has sound information in it. Note, we call the MCIWndCreate API from the MCI interface embedded into the MFC dialog.

2. Start the Project

First, we create a dialog based Visual C++ MFC application and name it as AVIFilePlay. After this step, we need to access the functions from the MCI Library. To provide access to the library, we add Vfw32.lib in the Additional Dependencies Entry of the project settings. The below screenshot shows where we should provide the access to this external library Vfw32.lib:

Setting the Additional Dependencies Library
Setting the Additional Dependencies Library

Once the required library access is provided to the project we can start our coding.

3. Code Implementation

3.1 Include Header File

Add the required header to the Dialog Class Implementation file. In our case, we add the header file in the AviFilePlayDlg.cpp.

Note that our project is aware of the header file Vfw.h as in the previous step, we provided access to the required library file.

3.2 Create Media Playback Window Using MCIWndCreate

Next, in the OnInitDialog function implementation, by passing the window handle of our dialog to the function MCIWndCreate, we create the media control interface (MCI) window. Note, the file which we like to play is staying in the D:\Temp directory. We can play the file from any path by pointing out the fully qualified path to the windows media AVI file. The call to the function MCIWndCreate() on success returns the window handle and we store that handle in the hVideoWindow. Below is the code:

3.3 MCIWndPlay of VFW32.Lib

Finally, we make a call to the MCI function MCIWndPlay(). The MCIWndplay() function requires a window handle, which is compatible for playing the window media file (AVI in our case). Below is the code for it:

When you run the application the video play occurs as shown below:

MFC Dialog Playing Media File Using MCIWndCreate
MFC Dialog Playing Media File Using MCIWndCreate

Source Code: Download MFC Dialog Playing Media File From Google Drive


Do you like this Example? Please comment about it for others!!

This site uses Akismet to reduce spam. Learn how your comment data is processed.