Using The MCI API
This article will show you how to play almost any type of multimedia file using the Window API only. It will show you how to manipulate a variety of commands that in turn will allow you to create professional standard applications.
AI
AI Summary: This codebase represents a historical implementation of the logic described in the metadata. Our preservation engine analyzes the structure to provide context for modern developers.
Source Code
<p><font face="Courier New, Courier, mono" size="2" color="#000000"> Lets start out by the two most important API declares. These will allow you to manipulate any multimedia file and return error's directly from the API.</font></p> <p><font face="Courier New, Courier, mono" size="2" color="#000000"><br> Ok, the only way to show you how to open multimedia files is to jump straight in. The examples are pretty self explanitory, so don't worry too much.</font></p> <p><font face="Courier New, Courier, mono" size="2" color="#000000"><br> Public 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 Long</font></p> <p><font face="Courier New, Courier, mono" size="2" color="#000000"><br> Public Declare Function mciGetErrorString Lib "winmm.dll" Alias "mciGetErrorStringA" (ByVal dwError As Long, ByVal lpstrBuffer As String, ByVal uLength As Long) As Long</font></p> <p><font face="Courier New, Courier, mono" size="2" color="#000000"><br> 'This is just an API call to get the short name of the path you specify. The MCI uses short path formats<br> Public Declare Function GetShortPathName Lib "kernel32" Alias "GetShortPathNameA" (ByVal lpszLongPath As String, ByVal lpszShortPath As String, ByVal cchBuffer As Long) As Long</font></p> <p><font face="Courier New, Courier, mono" size="2" color="#000000"><br> 'This constant is just a string that you pass to the MCI so it knows what file your want manipulated<br> Const Alias As String = "Media"</font></p> <p><font face="Courier New, Courier, mono" size="2" color="#000000"><br> 'This function will return the error of specified MCI error<br> Private Function GetMCIError(lError As Long) As String<br> Dim sBuffer As string 'We need this to store the returned error</font><font face="Courier New, Courier, mono" size="2" color="#000000"><br> sBuffer = String$(255, Chr(0)) 'This fills out buffer with null characters so the MCI has something to write the error on</font><font face="Courier New, Courier, mono" size="2" color="#000000"><br> mciGetErrorString lError, sReturn, Len(sReturn)<br> sBuffer = Replace$(sBuffer, Chr(0), "")<br> End Function</font></p> <p><font face="Courier New, Courier, mono" size="2" color="#000000"><br> Private Function OpenMP3(FileName As String) As String<br> Dim lResult As Long 'The return value of the MCI command<br> Dim sBuffer As String 'The Buffer used to get the short path, we use it in the same way as mciGetErrorString<br> sBuffer = String$(255, Chr(0))<br> GetShortPathName FileName, sBuffer, Len(sBuffer)<br> sBuffer = Replace$(sBuffer, Chr(0), "")<br> lResult = mciSendString("OPEN " & FileName & " TYPE MPEGVideo ALIAS " & Alias, 0, 0, 0)<br> If lResult Then 'There was an error<br> 'We make our function return the MCI error<br> OpenMP3 = GetMCIError(lResult)<br> Exit Function<br> Else 'There was no error<br> 'Set the timeformat of the file to milliseconds so when we send a request to get the length of the file or the curent playing position it will return in something we can understand<br> mciSendString "SET " & Alias & " TIME FORMAT TMSF", 0, 0, 0<br> End Function</font></p> <p><font face="Courier New, Courier, mono" size="2" color="#000000"><br> Private Sub CloseMP3()<br> 'We dont need an error code for this becuase if it dosent close then there isnt much we can do about it<br> mciSendString "CLOSE " & Alias, 0, 0, 0<br> End Sub</font></p> <p><font face="Courier New, Courier, mono" size="2" color="#000000"><br> Private Sub PlayMP3(Optional lPosition As Long)<br> 'We dont really need an error return code for this becuase if the file is playable the MCI would not have opened it in the first place<br> 'The lPosition tells the MCI to play the MP3 from a certain position (in milliseconds)<br> mciSendString "PLAY " & Alias & " FROM " & lPosition, 0, 0, 0<br> End Sub</font></p> <p><font face="Courier New, Courier, mono" size="2" color="#000000"><br> They are the basics of playing media files. I thought I'd show you an MP3 file becuase they are more fun. Now you have the basics you can incorporate it with the lst below. Below is a list of all the stuff you can do with the MCI.<br> All commands follow the same pattern e.g.</font></p> <p><font face="Courier New, Courier, mono" size="2" color="#000000"><br> mciSendString "You command string" & Your Alias & " Aditional Commands", 0, 0, 0<br> If you are requesting a return value remember you must use a buffer</font></p> <p><font face="Courier New, Courier, mono" size="2" color="#000000"><br> <b>Command Strings</b><br> "PAUSE"<br> "STOP"<br> "SEEK" Same as PLAY ALIAS FROM<br> "OPEN AS CDAUDIO" opens it for a CD Audio<br> "OPEN AS MPEGVideo" Opens ANY MPEG File<br> "SETAUDIO ALIAS LEFT VOLUME TO NUMBER"<br> "SETAUDIO ALIAS RIGHT VOLUME TO NUMBER"<br> "STATUS ALIAS LENGTH"<br> "STATUS ALIAS POSITION" </font> </p>
Original Comments (3)
Recovered from Wayback Machine