Wednesday, June 26, 2013

[ MFC ] - Using MCIWndCreate to Embed Video Player in your application

1. Introduction


In this article, we will see how do we play an AVI video file. To play the AVI file we are going to use the Media Control Interface function (MCI). The Animate control that ships with MFC pack 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 short example that plays the video file comp.avi. The Avi file that I am packing with this example has no sound information, so you should replace that with any video file, which has sound information in it.



2. Start the Example


Create a Dialog based Visual C++ MFC application and name the application as AVIFilePlay. Once the project is created, we need to access the functions from the MCI Library. To provide access to the library add Vfw32.lib in the additional dependencies entry of the Project settings. The below screenshot shows where you should provide the access to the external library:


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



3. Coding for Playing the AVI File


1) Add the required header to the Dialog Class Implementation file. In our case, I added the header file in the AviFilePlayDlg.cpp.

//Sample 01: Required Header
#include "Vfw.h"

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.


2) Next, in the OnInitDialog function implementation, by passing the window handle of our dialog to the function MCIWndCreate, the media control interface (MCI) window is created. Note that the file that we like to play is available in the D:\Temp directory. You can play the file from any path by specifying the fully qualified path to the windows media Avi file. The call to the function MCIWndCreate on success, return the window handle and that handle is stored in the hVideoWindow. Below is code:

//Sample 02: Create Media Control Interface Window
CAVIFilePlayApp* pApp = (CAVIFilePlayApp*) AfxGetApp();
HWND hVideoWindow = MCIWndCreate(this->m_hWnd,  pApp->m_hInstance,
       NULL, _T("D:\\Temp\\Comp.Avi"));


3) Finally, we make a call to the MCI function MCIPlay. The MCIplay function requires a window handle, which is compatible for playing the window media file (Avi in out case). Below is the code for it:

//Sample 03: Play the Video
MCIWndPlay(hVideoWindow);

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


Source Code: Download




No comments:

Post a Comment

Leave your comment(s) here.

Like this site? Tell it to your Firend :)