Link to home
Start Free TrialLog in
Avatar of mhdhallak
mhdhallak

asked on

Plaing MIDI using API

Without using the MCI Control, how do i play a midi file using API declaration?
ASKER CERTIFIED SOLUTION
Avatar of mcrider
mcrider

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of mhdhallak
mhdhallak

ASKER

If you have read thier article...
The problem with it is that the application cannot do anything until the midi is over and I don't want it to be like that. I want the midi song to accompany the running of my application.

How do i do that?
The only API Declaration you need its

   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
mciSendString

In order to open a midi file use

  mciSendString "open " & App.Path & "\musicfile.mid alias tune", vbNullString, 0, 0

Where musicfile.mid its your MIDI file..
 and to play it use:

  mciSendString "play tune from 0", vbNullString, 0, 0

To stop the music use:
 
  mciSendString "stop tune", bNullString, 0, 0

And to close the file:

    mciSendString "close tune", vbNullString, 0, 0
Take out the wait in the MCISENDSTRING PLAY statement.


For example, create a new project and add two command buttons to Form1. Then paste the following code into the DECLARATIONS SECTION of Form1 and run it...

When you click command1, the MIDI will play and when you click command2 it will stop...

Cheers!


THE CODE:

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

' Modify the value of the constant "Song" with your path
' to "canyon.mid".
Private Const Song As String = "C:\Windows\Media\Canyon.MID"
Private Sub Command1_Click()
    Dim ret As Long
    ret = mciSendString( _
    "open " & Song & " type sequencer alias canyon", 0&, 0, 0)
    ret = mciSendString("play canyon", 0&, 0, 0)
End Sub
Private Sub Command2_Click()
    ret = mciSendString("stop canyon", 0&, 0, 0)
    ret = mciSendString("close canyon", 0&, 0, 0)
End Sub
ok guys the midi API is working fine for me.
but how can I make it loop whenever it's finsihed?

I need the answer as soon as possible.
ret = mciSendString("play canyon repeat", 0&, 0, 0)

Cheers!