Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

(50 pts) Playing a MIDI file using WinMM.dll API

Posted on 1999-07-03
14
Medium Priority
?
810 Views
Last Modified: 2011-10-03
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
0
Comment
Question by:HATCHET
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 4
  • 4
14 Comments
 
LVL 10

Accepted Solution

by:
viktornet earned 200 total points
ID: 1521765
where are the 50 points? ;-))

Dim ret As Integer

' The following will open the sequencer with the C:\WIN31\CANYON.MID
' file. Canyon is the device_id.

                      ret = mciSendString("open c:\windows\CANYON.MID _
                      type sequencer alias canyon", 0&, 0, 0)

                      ' The wait tells the MCI command to complete before returning control
                      ' to the application.

                      ret = mciSendString("play canyon wait", 0&, 0, 0)

                      ' Close CANYON.MID file and sequencer device

                      ret = mciSendString("close canyon", 0&, 0, 0)

..-=ViKtOr=-..
0
 
LVL 3

Author Comment

by:HATCHET
ID: 1521766
viktornet,

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
0
 
LVL 10

Expert Comment

by:viktornet
ID: 1521767
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=-..
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 7

Expert Comment

by:Inteqam
ID: 1521768
0
 
LVL 7

Expert Comment

by:Inteqam
ID: 1521769
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
0
 
LVL 10

Expert Comment

by:viktornet
ID: 1521770
Intequam,

>>how to play a .MID (MIDI) file using an API
0
 
LVL 7

Expert Comment

by:Inteqam
ID: 1521771
lol

sorry

i'll try to help

0
 
LVL 10

Expert Comment

by:viktornet
ID: 1521772
hehe, that's great, but you gotta read the questions more carefully ;-)) well, i do the same mistake quite often myself ;))

-vik-
0
 
LVL 7

Expert Comment

by:Inteqam
ID: 1521773
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)

 
0
 
LVL 10

Expert Comment

by:viktornet
ID: 1521774
hehe ;o)

well, Hatchet, did you get it to work?

-vik-
0
 
LVL 3

Author Comment

by:HATCHET
ID: 1521775
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
0
 
LVL 3

Author Comment

by:HATCHET
ID: 1521776
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
0
 
LVL 10

Expert Comment

by:viktornet
ID: 1521777
hehe, thanks :))
0
 
LVL 3

Author Comment

by:HATCHET
ID: 1521778
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

0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
Suggested Courses

660 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question