HATCHET
asked on
(50 pts) Playing a MIDI file using WinMM.dll API
I use the following API to play .WAV (Wave) files :
' Declaration in a module :
Public Declare Function sndPlaySound Lib "winmm.dll" Alias "sndPlaySoundA" (ByVal lpszSoundName As String, ByVal uFlags As Long) As Long
' Code used to play sound :
sndPlaySound ByVal CStr(WAVEs.Path & "\" & WAVEs), 1
I know that the WinMM.dll has the functionality to play .MIDI files as well, but I can't figure out how to do it. Please provide code similar to the above code on how to play a .MID (MIDI) file using an API from this or a similar .DLL.
HATCHET
' Declaration in a module :
Public Declare Function sndPlaySound Lib "winmm.dll" Alias "sndPlaySoundA" (ByVal lpszSoundName As String, ByVal uFlags As Long) As Long
' Code used to play sound :
sndPlaySound ByVal CStr(WAVEs.Path & "\" & WAVEs), 1
I know that the WinMM.dll has the functionality to play .MIDI files as well, but I can't figure out how to do it. Please provide code similar to the above code on how to play a .MID (MIDI) file using an API from this or a similar .DLL.
HATCHET
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Well, I usually find declarations in the examples suplied by people on the internet.. sometimes from Micro$oft, etc...
btw, here is the declare for mciSendString()
Private Declare Function mciSendString Lib "winmm.dll" Alias _
"mciSendStringA" (ByVal lpstrCommand As String, ByVal _
lpstrReturnString As Any, ByVal uReturnLength As Long, ByVal _
hwndCallback As Long) As Long
Hope this helps :)
..-=ViKtOr=-..
btw, here is the declare for mciSendString()
Private Declare Function mciSendString Lib "winmm.dll" Alias _
"mciSendStringA" (ByVal lpstrCommand As String, ByVal _
lpstrReturnString As Any, ByVal uReturnLength As Long, ByVal _
hwndCallback As Long) As Long
Hope this helps :)
..-=ViKtOr=-..
Private Sub Command1_Click()
On Error GoTo ErrHandler
CommonDialog1.CancelError = True
CommonDialog1.Filter = "MIDI Sequence (*.mid; *.rmi)|*.mid; *.rmi|WAV Sound (*.wav)|*.wav|MP3 (*.mp3)|*.mp3"
CommonDialog1.ShowOpen
If CommonDialog1.filename <> "" Then
Mediaplayer1.filename = CommonDialog1.filename
Mediaplayer1.Autostart = False
Mediaplayer1.Stop
Form1.Caption = CommonDialog1.FileTitle
End If
Exit Sub
ErrHandler:
'If an error occurs, exit the procedure.
End Sub
Private Sub Command2_Click()
Mediaplayer1.Play
End Sub
'Assumes:1) Press Ctrl + T
2) Find "Microsoft Common Dialog Control 5.0(or 6.0)," and "Windows Media Player," and put checkmarks in the checkboxes.
3) Add the two new controls to the form, and add two CommandButtons.
4) Set the caption of the first CommandButton
On Error GoTo ErrHandler
CommonDialog1.CancelError = True
CommonDialog1.Filter = "MIDI Sequence (*.mid; *.rmi)|*.mid; *.rmi|WAV Sound (*.wav)|*.wav|MP3 (*.mp3)|*.mp3"
CommonDialog1.ShowOpen
If CommonDialog1.filename <> "" Then
Mediaplayer1.filename = CommonDialog1.filename
Mediaplayer1.Autostart = False
Mediaplayer1.Stop
Form1.Caption = CommonDialog1.FileTitle
End If
Exit Sub
ErrHandler:
'If an error occurs, exit the procedure.
End Sub
Private Sub Command2_Click()
Mediaplayer1.Play
End Sub
'Assumes:1) Press Ctrl + T
2) Find "Microsoft Common Dialog Control 5.0(or 6.0)," and "Windows Media Player," and put checkmarks in the checkboxes.
3) Add the two new controls to the form, and add two CommandButtons.
4) Set the caption of the first CommandButton
Intequam,
>>how to play a .MID (MIDI) file using an API
>>how to play a .MID (MIDI) file using an API
lol
sorry
i'll try to help
sorry
i'll try to help
hehe, that's great, but you gotta read the questions more carefully ;-)) well, i do the same mistake quite often myself ;))
-vik-
-vik-
I thought i am so smart to know the hall question from the title ,or lets say half the title , cause the title here is so clear , and indicates that i am not smart
:o)
:o)
hehe ;o)
well, Hatchet, did you get it to work?
-vik-
well, Hatchet, did you get it to work?
-vik-
ASKER
viktornet,
Sorry took so long for me to respond, been real busy lately.
About your code, I tried it at home and at work. At home, I put in all the code you mentioned, declared and called the .DLL correctly, but it doesn't work. It doesn't give any errors, but it just doesn't play any sounds. I went into Windows Explorer and could play other .MID files using Microsoft's Media Player, so I know that the problem isn't my system, the speakers, the sound card, or connections between them.
I tried it at work and it works fine! I have no idea why. I did change the file it was playing at home, and didn't at work. Could that be it? Are there limitations on the syntax or name of the Device_ID ? Do you have to pass it a string value for the path of the MIDI or can you pass it a function to locate the path which then returns a string? What am I doing wrong ?
HATCHET
Sorry took so long for me to respond, been real busy lately.
About your code, I tried it at home and at work. At home, I put in all the code you mentioned, declared and called the .DLL correctly, but it doesn't work. It doesn't give any errors, but it just doesn't play any sounds. I went into Windows Explorer and could play other .MID files using Microsoft's Media Player, so I know that the problem isn't my system, the speakers, the sound card, or connections between them.
I tried it at work and it works fine! I have no idea why. I did change the file it was playing at home, and didn't at work. Could that be it? Are there limitations on the syntax or name of the Device_ID ? Do you have to pass it a string value for the path of the MIDI or can you pass it a function to locate the path which then returns a string? What am I doing wrong ?
HATCHET
ASKER
I FOUND THE PROBLEM !!
With this code, for some reason it does not allow a 32bit / long file path. You need to pass it a path with no spaces, or odd characters, etc. If you do this, the code works fine.
I'll post the code I used for my project in a little bit, and will also post the points for viktornet. Thanks again ! =D
HATCHET
With this code, for some reason it does not allow a 32bit / long file path. You need to pass it a path with no spaces, or odd characters, etc. If you do this, the code works fine.
I'll post the code I used for my project in a little bit, and will also post the points for viktornet. Thanks again ! =D
HATCHET
hehe, thanks :))
ASKER
viktornet,
Thanks again for the help on this. I got the code I needed. It's A LOT more complex to play a MIDI file than it is to play a WAVE. Below is the code I used:
__________________________ __________ __________ __________ __________ __
Option Explicit
' Variable Declares
Private Device_ID As String
' Function Declares
Private Declare Function mciSendString Lib "winmm.dll" Alias "mciSendStringA" (ByVal lpstrCommand As String, ByVal lpstrReturnString As Any, ByVal uReturnLength As Long, ByVal hwndCallback As Long) As Long
Private Declare Function GetShortPathName Lib "kernel32" Alias "GetShortPathNameA" (ByVal lpszLongPath As String, ByVal lpszShortPath As String, ByVal cchBuffer As Long) As Long
Private Sub cmdBrowse_Click()
On Error Resume Next
With CD1
.CancelError = False
.DefaultExt = ".mid"
.DialogTitle = " Locate MIDI File To Open"
'.InitDir = "C:\"
.Filter = "MIDI File ( *.mid, *.rmi )|*.mid;*.rmi"
.ShowOpen
If .filename = "" Then
Exit Sub
Else
txtPath.Text = .filename
End If
End With
End Sub
Private Sub cmdClose_Click()
' End the program
Unload Me
End Sub
Private Sub cmdPlay_Click()
On Error Resume Next
Dim Length As Long
Dim Short_Path As String
Dim Long_Path As String
' Check if the path is valid
If txtPath.Text = "" Then
Exit Sub
ElseIf UCase(Right(txtPath.Text, 4)) <> ".MID" And UCase(Right(txtPath.Text, 4)) <> ".RMI" Then
Exit Sub
End If
' Stop playing and close the MIDI sequencer device
mciSendString "close " & Device_ID, 0&, 0, 0
' Get the 16bit file path because 32bit file paths don't work
Long_Path = txtPath.Text
Short_Path = Space(1024)
Length = GetShortPathName(Long_Path , Short_Path, Len(Short_Path))
Short_Path = Left(Short_Path, Length)
' Code used to play sound
mciSendString "open " & Short_Path & " type sequencer alias " & Device_ID, 0&, 0, 0
mciSendString "play " & Device_ID, 0&, 0, 0
' The following will play the song and lock up the program until
' it's done. This can be useful, but is annoying when programming
'mciSendString "play " & Device_ID & " wait", 0&, 0, 0
End Sub
Private Sub cmdStop_Click()
On Error Resume Next
' Stop playing and close the MIDI sequencer device
mciSendString "close " & Device_ID, 0&, 0, 0
End Sub
Private Sub Form_Load()
' Set the Device ID
Device_ID = "MIDI"
End Sub
Thanks again for the help on this. I got the code I needed. It's A LOT more complex to play a MIDI file than it is to play a WAVE. Below is the code I used:
__________________________
Option Explicit
' Variable Declares
Private Device_ID As String
' Function Declares
Private Declare Function mciSendString Lib "winmm.dll" Alias "mciSendStringA" (ByVal lpstrCommand As String, ByVal lpstrReturnString As Any, ByVal uReturnLength As Long, ByVal hwndCallback As Long) As Long
Private Declare Function GetShortPathName Lib "kernel32" Alias "GetShortPathNameA" (ByVal lpszLongPath As String, ByVal lpszShortPath As String, ByVal cchBuffer As Long) As Long
Private Sub cmdBrowse_Click()
On Error Resume Next
With CD1
.CancelError = False
.DefaultExt = ".mid"
.DialogTitle = " Locate MIDI File To Open"
'.InitDir = "C:\"
.Filter = "MIDI File ( *.mid, *.rmi )|*.mid;*.rmi"
.ShowOpen
If .filename = "" Then
Exit Sub
Else
txtPath.Text = .filename
End If
End With
End Sub
Private Sub cmdClose_Click()
' End the program
Unload Me
End Sub
Private Sub cmdPlay_Click()
On Error Resume Next
Dim Length As Long
Dim Short_Path As String
Dim Long_Path As String
' Check if the path is valid
If txtPath.Text = "" Then
Exit Sub
ElseIf UCase(Right(txtPath.Text, 4)) <> ".MID" And UCase(Right(txtPath.Text, 4)) <> ".RMI" Then
Exit Sub
End If
' Stop playing and close the MIDI sequencer device
mciSendString "close " & Device_ID, 0&, 0, 0
' Get the 16bit file path because 32bit file paths don't work
Long_Path = txtPath.Text
Short_Path = Space(1024)
Length = GetShortPathName(Long_Path
Short_Path = Left(Short_Path, Length)
' Code used to play sound
mciSendString "open " & Short_Path & " type sequencer alias " & Device_ID, 0&, 0, 0
mciSendString "play " & Device_ID, 0&, 0, 0
' The following will play the song and lock up the program until
' it's done. This can be useful, but is annoying when programming
'mciSendString "play " & Device_ID & " wait", 0&, 0, 0
End Sub
Private Sub cmdStop_Click()
On Error Resume Next
' Stop playing and close the MIDI sequencer device
mciSendString "close " & Device_ID, 0&, 0, 0
End Sub
Private Sub Form_Load()
' Set the Device ID
Device_ID = "MIDI"
End Sub
ASKER
No worries, I'll give you the points once you answer the question successfully. =]
I can't test the code you just gave me because you didn't put a Declare statement for the function : mciSendString
Do you have, or know where I can get, a complete listing of the functions of the most commonly used .DLL's and how to use them? Or do I have to go buy a book or something? I've got a help file that tells you EVERYTHINK you could possibly want to know about every function, constant, and variable in the Win32 SDK. I'd like to get the same for USER32.DLL, GDI.DLL, KERNEL32.DLL, WINMM.DLL, etc. (this isn't required info to answer the question, just wondering. =] )
HATCHET