sound
Play Midi audio
With this example we shall show you how to play MIDI audio files in a Java Desktop Application. This is very useful when you want to embed a simple audio player in your application.
In short, playing MIDI audio files requires that you:
- Obtain the default
Sequencerconnected to a default device usingMidiSystem.getSequencer(). - Create a stream from a file to the MIDI file.
- Sets the current sequence on which the sequencer operates using
sequencer.setSequence. - Finally, start playback of the MIDI data in the currently loaded sequence using
sequencer.start()
Here is the code:
package com.javacodegeeks.snippets.desktop;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import javax.sound.midi.MidiSystem;
import javax.sound.midi.Sequencer;
public class PlayMidiAudio {
public static void main(String[] args) throws Exception {
// Obtains the default Sequencer connected to a default device.
Sequencer sequencer = MidiSystem.getSequencer();
// Opens the device, indicating that it should now acquire any
// system resources it requires and become operational.
sequencer.open();
// create a stream from a file
InputStream is = new BufferedInputStream(new FileInputStream(new File("midifile.mid")));
// Sets the current sequence on which the sequencer operates.
// The stream must point to MIDI file data.
sequencer.setSequence(is);
// Starts playback of the MIDI data in the currently loaded sequence.
sequencer.start();
}
}
This was an example on how to play MIDI audio.


Hello ! Thanks for the code. But, this doesn’t work for me. When I run this class, it just keeps running in an infinite loop without playing the .mid audio. Can you please let me know where all I can debug the issue.