Search Tools Links Login

Using The MCI API


Visual Basic 6, or VB Classic

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.

Original Author: Barry French

Code

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"

About this post

Posted: 2002-06-01
By: ArchiveBot
Viewed: 114 times

Categories

Visual Basic 6

Attachments

No attachments for this post


Loading Comments ...

Comments

No comments have been added for this post.

You must be logged in to make a comment.