Atlanta Custom Software Development 

 
   Search        Code/Page
 

User Login
Email

Password

 

Forgot the Password?
Services
» Web Development
» Maintenance
» Data Integration/BI
» Information Management
Programming
  Database
Automation
OS/Networking
Graphics
Links
Tools
» Regular Expr Tester
» Free Tools

Play a MIDI file

Total Hit ( 4467)

Rate this article:     Poor     Excellent 

 Submit Your Question/Comment about this article

Rating


 


If you want to play a MIDI file from VB you have to use MCI functions. The main MCI function is mciSendString, that sends command strings to the system and execute them:

Click here to copy the following block
Declare Function mciSendString Lib "winmm" Alias "mciSendStringA" (ByVal _
  lpstrCommand As String, ByVal lpstrReturnString As String, _
  ByVal uReturnLength As Long, ByVal hwndCallback As Long) As Long

The first argument is the command string, lpstrReturnString receives return information (if needed), uReturnLength indicates the size, in characters, of lpstrReturnString, and hwndCallback is used for system notification. Remember that every command string described in this tip must be sent to the system with:

Click here to copy the following block
CommandString = "Your MCI command here"
RetVal = mciSendString(CommandString, vbNullString, 0, 0)

First you must open the MIDI device with

Click here to copy the following block
CommandString = "Open " + FileName + " type sequencer alias MidiFile"

Notice that FileName must be in the short format 8.3 characters; lpstrReturnString is null because you don't need any return information. "MidiFile" is an arbitrary name that you'll use in the next command strings as an alias of the filename. Finally you can play the MIDI file:

Click here to copy the following block
CommandString = "Play MidiFile wait"

The execution is synchronous, but you can play the file asynchronously and receive notifications with

Click here to copy the following block
CommandString = "Play MidiFile notify"
RetVal = mciSendString(CommandString, vbNullString, 0, MyForm.hWnd)

where MyForm is the form you are using. The system will send to your form a MM_MCINOTIFY message when the playback is finished. To intercept this message you have to create a custom window procedure and use subclassing.
By default playback begins at the current position and stops at the end of the content, but you can play only a part of the file with this statement

Click here to copy the following block
CommandString = "Play MidiFile from X to Y"

where X and Y are two different positions. It's often useful to specify X and Y in milliseconds, so before Play command you must set the time format with this command:

Click here to copy the following block
CommandString = "Set MidiFile time format milliseconds"

With asynchronous executions, you can pause / resume and stop playback with ' pause playback CommandString = "Pause MidiFile" ' resume a paused playback CommandString = "Resume MidiFile" ' stop playback CommandString = "Stop MidiFile" When you have finished to use the MIDI device, remember to close it with

Click here to copy the following block
CommandString = "Close MidiFile"



Submitted By : Nayan Patel  (Member Since : 5/26/2004 12:23:06 PM)

Job Description : He is the moderator of this site and currently working as an independent consultant. He works with VB.net/ASP.net, SQL Server and other MS technologies. He is MCSD.net, MCDBA and MCSE. In his free time he likes to watch funny movies and doing oil painting.
View all (893) submissions by this author  (Birth Date : 7/14/1981 )


Home   |  Comment   |  Contact Us   |  Privacy Policy   |  Terms & Conditions   |  BlogsZappySys

© 2008 BinaryWorld LLC. All rights reserved.