Visual Basic Programming Code Examples Visual Basic > Multimedia Animation and Sound Code Examples How to play a midi file How to play a midi file I am going to show you how to play a midi file in Visual Basic 5. We will start by creating a '.BAS' module. Go ahead and name this module 'modPlayMidi'. Once we have this module created we will go ahead and declare the API functions needed in order to play the midi files. We will do this in the General Declarations area of our new module. Go ahead and enter the General Declarations area and enter in these API functions. 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 Declare Function mciGetErrorString Lib "winmm.dll" _ Alias "mciGetErrorStringA" _ (ByVal dwError As Long, _ ByVal lpstrBuffer As String, _ ByVal uLength As Long) As Long Declare Function GetShortPathName Lib "kernel32" _ Alias "GetShortPathNameA" _ (ByVal lpszLongPath As String, _ ByVal lpszShortPath As String, _ ByVal cchBuffer As Long) As Long '--end code block The mciSendString API we use for opening, playing and closing the midi file. We use the mciGetErrorString when we encounter errors with the mciSendString command. Since the mciSendString doesn't like long filenames with spaces in it I've included the GetShortPathName API to convert a long pathname to a short one. Now that we have our API functions declared we can go ahead and begin creating the OpenMidi, PlayMidi and CloseMidi procedures. Public Sub OpenMidi() Dim sFile As String Dim sShortFile As String * 67 Dim lResult As Long Dim sError As String * 255 'Set the path and filename to open. I am using the 'mcitest.mid which I found in my VB5 directory in 'the sub folders samples\comptool\mci 'I just copied it to this projects folder. sFile = App.Path & "\mcitest.mid" 'The mciSendString API call doesn't seem to like' 'long filenames that have spaces in them, so we 'will make another API call to get the short 'filename version. lResult = GetShortPathName(sFile, sShortFile, _ Len(sShortFile)) sFile = Left$(sShortFile, lResult) 'Make the call to open the midi file and assign 'it an alias lResult = mciSendString("open " & sFile & _ " type sequencer alias mcitest", ByVal 0&, 0, 0) 'Check to see if there was an error If lResult Then lResult = mciGetErrorString(lResult, sError, 255) Debug.Print "open: " & sError End If End Sub Public Sub PlayMidi() Dim lResult As Integer Dim sError As String * 255 'Make the call to start playing the midi lResult = mciSendString("play mcitest", ByVal 0&, 0, 0) 'Check to see if there were any errors If lResult Then lResult = mciGetErrorString(lResult, sError, 255) Debug.Print "play: " & sError End If End Sub Public Sub CloseMidi() Dim lResult As Integer Dim sError As String * 255 'Make the call to close the midi file lResult = mciSendString("close mcitest", "", 0&, 0&) 'Check to see if there were any errors If lResult Then lResult = mciGetErrorString(lResult, sError, 255) Debug.Print "stop: " & sError End If End Sub '--end code block Lets see what these procedures do. The first procedure we entered was the OpenMidi procedure. The first thing the OpenMidi function does is set up the filename of the midi file that we are opening. Since the mciSendString doesn't like long filenames with spaces in it I found the GetShortPathName function which converts a long filename to a short one. Now that we have a filename that is compatible with the mciSendString API we can go ahead and open the midi file. The mciSendString take four parameters. The first parameter, lpstrCommand, is the command string. The second parameter, lpstrReturnString, is used by the mciSendString API to return a string to you. You will not need this unless you want to retrieve the status of the midi file. The third parameter, uReturnLength, tells the mciSendString how large the lpstrReturnString parameter is. The last parameter, hwndCallback, is used for receiving messages back from the mci device. We won't be using it here because it is a little beyond the scope of this sample. The parameter we want to focus on here is the lpstrCommand parameter. Here we pass a Command String to tell the mci device what to do. In this case we are opening the midi file. This command string is structured like this: open [filename] type sequencer alias [aliasname] What we are doing here is opening the file. We are also letting it know that the type of this file is a midi sequencer. And lastly we are giving this device an alias, otherwise we will have to make all other calls with the filename. Using an alias is just easier to read as you can see in the PlayMidi and CloseMidi procedures. Finaly the OpenMidi procedure checks the return status of the mciSendString call. If the return is greater than zero then an error has occured. When this happens I am just printing it to the debug window. The next two procedures are pretty easy to understand. In the PlayMidi we are making another call to the mciSendString but this time the command string is telling it to play the midi device we opened earlier. The close midi closes the midi file. Now to test these procedures will will place two command buttons on a form. I have called these buttons cmdPlay, and cmdStop. In the click event of these buttons we will call the midi procedures we just entered. Private Sub cmdPlay_Click() OpenMidi PlayMidi End Sub Private Sub cmdStop_Click() CloseMidi End Sub