Playback a Video File - Windows C DLL

This tutorial shows how to playback a video file in a Windows C DLL application using the LEADTOOLS Multimedia SDK.

Overview  
Summary This tutorial covers how to use the IltmmPlay interface to playback a video file in a Windows C DLL application.
Completion Time 20 minutes
Visual Studio Project Download tutorial project (18 KB)
Platform Windows C DLL Application
IDE Visual Studio 2017, 2019
Development License Download LEADTOOLS
Try it in another language

Required Knowledge

Get familiar with the basic steps of creating a project by reviewing the Add References and Set a License tutorial, before working on the Playback a Video File - Windows C DLL tutorial.

Create the Project and Add LEADTOOLS References

Start with a copy of the project created in the Add References and Set a License tutorial. If you do not have that project, create it by following the steps in that tutorial.

In order to load and play back video files, LEADTOOLS requires additional references. Add the required Multimedia library reference by opening the pre-compiled header file, either pch.h or stdafx.h depending on the Visual Studio version used, and add the following line:

// Add LEADTOOLS Multimedia references 
#import "C:\LEADTOOLS22\Bin\CDLL\x64\ltmm22x.dll" no_namespace, named_guids 

Note

For a complete list of which DLL files are required for your application, refer to Files to be Included With Your Application.

Set the License File

The License unlocks the features needed for the project. It must be set before any toolkit functionality is called. For details, including tutorials for different platforms, refer to Setting a Runtime License.

There are two types of runtime licenses:

Note

Adding LEADTOOLS references and setting a license are covered in more detail in the Add References and Set a License tutorial..

Add Initialization and Cleanup Code

With the project created, the references added, and the license set, coding can begin.

The steps below are for Visual Studio 2019; they could be different for other versions of Visual Studio.

In the Solution Explorer, double-click the resources file (.rc).

Expand the menu in the resources tree and double-click the menu resource to open it in the designer interface. In the empty item below the Exit item, click and type &Play. Drag the new item above Exit. This should cause the item's ID to become ID_FILE_PLAY.

Navigate to the main CPP file of the project, which contains the WndProc() function for the main window. Add the following declaration to the global variables near the top:

// Global Variables: 
IltmmPlay * pPlay = NULL; 

Go to the WndProc function, which should have a case WM_DESTROY inside it. Add a new case WM_CREATE if it is not present and add the following code under it:

case WM_CREATE: 
{ 
   HRESULT hr = CoInitialize(NULL); 
   if FAILED(hr) 
   { 
      MessageBox(hWnd, TEXT("Error starting up COM library..\nAborting"), TEXT("LEADTOOLS Demo"), MB_ICONERROR); 
      return -1; 
   } 
   hr = CoCreateInstance(CLSID_ltmmPlay, NULL, CLSCTX_INPROC_SERVER, IID_IltmmPlay, (void**)&pPlay); 
   if FAILED(hr) 
   { 
      MessageBox(hWnd, TEXT("Error creating Play control..\nAborting"), TEXT("LEADTOOLS Demo"), MB_ICONERROR); 
      return -1; 
   } 
   pPlay->VideoWindowFrame = (long)hWnd; 
   break; 
} 

Modify the case that handles WM_DESTROY in the WndProc() function to become:

case WM_DESTROY: 
   CoUninitialize(); 
   PostQuitMessage(0); 
   break; 

Add the Play Video Code

Inside the WndProc function, under the switch (wmId) statement, add a new ID_FILE_PLAY case and add the code below to play the selected video file:

// In WndProc(), under "case WM_COMMAND:" 
switch (wmId) 
{ 
   case ID_FILE_PLAY: 
   { 
      TCHAR szFileName[260] = TEXT(""); // file name  
      pPlay->AutoStart = TRUE; 
      pPlay->VideoWindowSizeMode = ltmmSizeMode::ltmmNormal; 
      if (!GetLoadingFileName(hWnd, szFileName, sizeof szFileName)) 
         break; 
      pPlay->sourcefile = szFileName; 
   } 
      break; 
   // Keep rest of the code as is 

Add a new function to the CPP project file named GetLoadingFileName(HWND hwnd, TCHAR* pszFileName, DWORD nLen). This function will be called inside the ID_FILE_PLAY case, as shown above. The GetLoadingFileName() function can be any function that fills the szFileName variable with a valid image file name (including full path, if needed). Add the code below to display a File Open dialog to obtain the file you want to playback.

bool GetLoadingFileName(HWND hwnd, TCHAR* pszFileName, DWORD nLen) 
{ 
   OPENFILENAME OpenFileName = { 0 }; 
   OpenFileName.lStructSize = sizeof(OpenFileName); 
   OpenFileName.hwndOwner = hwnd; 
   OpenFileName.lpstrFile = pszFileName; 
   OpenFileName.nMaxFile = nLen; 
   OpenFileName.lpstrFilter = TEXT("All\0*.*\0"); 
   OpenFileName.nFilterIndex = 1; 
   OpenFileName.lpstrFileTitle = NULL; 
   OpenFileName.nMaxFileTitle = 0; 
   OpenFileName.lpstrInitialDir = NULL; 
   OpenFileName.Flags = OFN_PATHMUSTEXIST | OFN_FILEMUSTEXIST; 
 
   // Show the File Open dialog box  
   if (!GetOpenFileName(&OpenFileName)) 
      return false; 
   return true; 
} 

Run the Project

Run the project by pressing F5, or by selecting Debug -> Start Debugging.

If the steps are followed correctly, the application runs and enables the user to open and play any multimedia file that is supported by the LEADTOOLS Multimedia SDK.

Wrap-up

This tutorial covered how to add the necessary references to create an instance of the Play control, and use it for multimedia playback.

See Also

Help Version 22.0.2021.10.18
Products | Support | Contact Us | Intellectual Property Notices
© 1991-2021 LEAD Technologies, Inc. All Rights Reserved.

Products | Support | Contact Us | Intellectual Property Notices
© 1991-2021 LEAD Technologies, Inc. All Rights Reserved.