Play a MIDI file

Play a MIDI file

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:

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:

CommandString = "Your MCI command here"RetVal = mciSendString(CommandString, vbNullString, 0, 0)

First you must open the MIDI device with

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:

CommandString = "Play MidiFile wait"

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

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

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:

CommandString = "Set MidiFile time format milliseconds"

With asynchronous executions, you can pause / resume and stop playback with’ pause playbackCommandString = “Pause MidiFile”‘ resume a paused playbackCommandString = “Resume MidiFile”‘ stop playbackCommandString = “Stop MidiFile”When you have finished to use the MIDI device, remember to close it with

CommandString = "Close MidiFile"

Share the Post:
XDR solutions

The Benefits of Using XDR Solutions

Cybercriminals constantly adapt their strategies, developing newer, more powerful, and intelligent ways to attack your network. Since security professionals must innovate as well, more conventional endpoint detection solutions have evolved

AI is revolutionizing fraud detection

How AI is Revolutionizing Fraud Detection

Artificial intelligence – commonly known as AI – means a form of technology with multiple uses. As a result, it has become extremely valuable to a number of businesses across

AI innovation

Companies Leading AI Innovation in 2023

Artificial intelligence (AI) has been transforming industries and revolutionizing business operations. AI’s potential to enhance efficiency and productivity has become crucial to many businesses. As we move into 2023, several

data fivetran pricing

Fivetran Pricing Explained

One of the biggest trends of the 21st century is the massive surge in analytics. Analytics is the process of utilizing data to drive future decision-making. With so much of

kubernetes logging

Kubernetes Logging: What You Need to Know

Kubernetes from Google is one of the most popular open-source and free container management solutions made to make managing and deploying applications easier. It has a solid architecture that makes

ransomware cyber attack

Why Is Ransomware Such a Major Threat?

One of the most significant cyber threats faced by modern organizations is a ransomware attack. Ransomware attacks have grown in both sophistication and frequency over the past few years, forcing

data dictionary

Tools You Need to Make a Data Dictionary

Data dictionaries are crucial for organizations of all sizes that deal with large amounts of data. they are centralized repositories of all the data in organizations, including metadata such as