VBcoders Guest



Don't have an account yet? Register
 


Forgot Password?



Using The MCI API

by Barry French (1 Submission)
Category: Windows API Call/Explanation
Compatability: Visual Basic 3.0
Difficulty: Intermediate
Date Added: Wed 3rd February 2021
Rating: (4 Votes)

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.

Rate Using The MCI API

 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.




 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.




 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




 Public Declare Function mciGetErrorString Lib "winmm.dll" Alias "mciGetErrorStringA" 
 (ByVal dwError As Long, ByVal lpstrBuffer As String, ByVal uLength As Long) 
 As Long




 'This is just an API call to get the short name of the path you specify. The 
 MCI uses short path formats

 Public Declare Function GetShortPathName Lib "kernel32" Alias "GetShortPathNameA" 
 (ByVal lpszLongPath As String, ByVal lpszShortPath As String, ByVal cchBuffer 
 As Long) As Long




 'This constant is just a string that you pass to the MCI so it knows what file 
 your want manipulated

 Const Alias As String = "Media"




 'This function will return the error of specified MCI error

 Private Function GetMCIError(lError As Long) As String

 Dim sBuffer As string 'We need this to store the returned error

 sBuffer = String$(255, Chr(0)) 'This fills out buffer with null characters so 
 the MCI has something to write the error on

 mciGetErrorString lError, sReturn, Len(sReturn)

 sBuffer = Replace$(sBuffer, Chr(0), "")

 End Function




 Private Function OpenMP3(FileName As String) As String

 Dim lResult As Long 'The return value of the MCI command

 Dim sBuffer As String 'The Buffer used to get the short path, we use it in the 
 same way as mciGetErrorString

 sBuffer = String$(255, Chr(0))

 GetShortPathName FileName, sBuffer, Len(sBuffer)

 sBuffer = Replace$(sBuffer, Chr(0), "")

 lResult = mciSendString("OPEN " & FileName & " TYPE MPEGVideo 
 ALIAS " & Alias, 0, 0, 0)

 If lResult Then 'There was an error

 'We make our function return the MCI error

 OpenMP3 = GetMCIError(lResult)

 Exit Function

 Else 'There was no error

 '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

 mciSendString "SET " & Alias & " TIME FORMAT TMSF", 
 0, 0, 0

 End Function




 Private Sub CloseMP3()

 'We dont need an error code for this becuase if it dosent close then there isnt 
 much we can do about it

 mciSendString "CLOSE " & Alias, 0, 0, 0

 End Sub




 Private Sub PlayMP3(Optional lPosition As Long)

 '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

 'The lPosition tells the MCI to play the MP3 from a certain position (in milliseconds)

 mciSendString "PLAY " & Alias & " FROM " & lPosition, 
 0, 0, 0

 End Sub




 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.

 All commands follow the same pattern e.g.




 mciSendString "You command string" & Your Alias & " Aditional 
 Commands", 0, 0, 0

 If you are requesting a return value remember you must use a buffer




 Command Strings

 "PAUSE"

 "STOP"

 "SEEK" Same as PLAY ALIAS FROM

 "OPEN AS CDAUDIO" opens it for a CD Audio

 "OPEN AS MPEGVideo" Opens ANY MPEG File

 "SETAUDIO ALIAS LEFT VOLUME TO NUMBER"

 "SETAUDIO ALIAS RIGHT VOLUME TO NUMBER"

 "STATUS ALIAS LENGTH"

 "STATUS ALIAS POSITION"  

Download this snippet    Add to My Saved Code

Using The MCI API Comments

No comments have been posted about Using The MCI API. Why not be the first to post a comment about Using The MCI API.

Post your comment

Subject:
Message:
0/1000 characters