Multimedia Control If you've ever tried to find classes in the .NET Framework that allow you to play audio and video, you may have noticed the gaping hole where this functionality should be. If you think Microsoft has finally gotten around to fixing this glaring omission in version 2 of the framework, you’re only partially correct. While some basic audio capabilities will be provided (as described later in this article,) they still leave a lot to be desired. On the other hand, the free MediaPlayer component provided with this article demonstrates a more feature-rich solution to your multimedia needs-and it’s compatible with all versions of the .NET Framework. The Multimedia Control Interface (MCI) is an aging (but sturdy) standard implemented by Microsoft to provide a common way to send commands to the dizzying array of audio and video devices supported by Windows. Before this standard, every video card and sound card had their own custom APIs, which made multimedia development quite cumbersome. That functionality is implemented inside the Windows Mutli-Media Dynamic Link Library named WinMM.DLL, which became a standard part of Windows many years ago. | " | By harnessing the power of the Multimedia Control Interface (MCI) and the Windows API, it’s possible to get around the limitations of the .NET Framework and achieve rich media functionality.
| " |
By harnessing the power of the Multimedia Control Interface (MCI) and the Windows API, it’s possible to get around the limitations of the .NET Framework and achieve rich media functionality. Of course this kind of low-level coding comes with its perils, so it’s a good idea to encapsulate such intricacy into a component of its own, as demonstrated here. Since the .NET Framework does not provide the required functionality you must bypass it and send the commands directly to Windows. Listing 1 shows the declarations needed to implement the MCI’s API functions that the MediaPlayer component requires. mciSendString is the primary function used by the MediaPlayer component. It will execute all of the multimedia functionality. It accepts carefully formatted strings such as “play CD from 3 to 6,” which plays tracks 3 through 6 of the current audio CD. The MediaPlayer component encapsulates the various string options to provide a more modern, simplified, and error-resistant programmatic interface. This function returns an unsigned integer that will contain zero upon success. If the function call should fail it will return an error number. The mciGetErrorString function accepts the error number and returns details about the error. The mciSendString function doesn’t recognize long file names, so file names must be converted to their short forms before they are parameterized. The GetShortPathName function provides the conversion of long file names to short file names. | & | | | Fast Facts | | When working in-depth with audio and video files, it’s a good idea to ensure your sound and video drivers are up to date. You may want to encourage your end users to make sure they have updated drivers as well. Old, unreliable drivers are a leading cause of glitches and serious crashes in Windows. | |
|
| Listing 1: Declare the required Windows API functions | Private Declare Function mciSendString Lib _ "winmm.dll" Alias "mciSendStringA" _ (ByVal lpstrCommand As String, _ ByVal lpstrReturnString As String, _ ByVal uReturnLength As Long, _ ByVal hwndCallback As Long) As UInt32
Private Declare Function mciGetErrorString Lib "winmm" Alias _ "mciGetErrorStringA" _ (ByVal dwError As UInt32, _ ByVal lpstrBuffer As String, _ ByVal uLength As Long) As Long
Private Declare Function GetShortPathName _ Lib "kernel32" Alias "GetShortPathNameA" _ (ByVal lpszLongPathName As String, _ ByVal lpszShortPath As String, _ ByVal cchBuffer As Long) As Long
|
|