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 Sequencer connected to a default device using MidiSystem.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 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.;

	    // 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.

	    // Starts playback of the MIDI data in the currently loaded sequence.



This was an example on how to play MIDI audio.

Ilias Tsagklis

Ilias is a software developer turned online entrepreneur. He is co-founder and Executive Editor at Java Code Geeks.
Notify of

This site uses Akismet to reduce spam. Learn how your comment data is processed.

1 Comment
Newest Most Voted
Inline Feedbacks
View all comments
4 years ago

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.

Back to top button