This article is a reminder of how to play MIDI files using the Java Sound API. This is a rudimentary article that can be played / stopped for the time being.
Here is the sample source code. It's a simple program that plays MIDI when you enter the path of a MIDI file using console input. Please forgive me for the exception handling that is insanely applicable. m (__) m
SamplePlayer.java
import java.io.File;
import java.io.IOException;
import java.util.Scanner;
import javax.sound.midi.InvalidMidiDataException;
import javax.sound.midi.MidiSystem;
import javax.sound.midi.MidiUnavailableException;
import javax.sound.midi.Sequence;
import javax.sound.midi.Sequencer;
public class SamplePlayer {
public static void main(String[] args) {
//Hardware to play MIDI data/An instance of a software device.
Sequencer sequencer = null;
try {
//Get the default Sequencer connected to the device.
sequencer = MidiSystem.getSequencer();
//Open the device and acquire resources.
sequencer.open();
}
catch (MidiUnavailableException e) {
e.printStackTrace();
}
//Get the MIDI file path from the console input.
Scanner scanner = new Scanner(System.in);
System.out.print("MIDI file path>> ");
String path = scanner.next();
try {
//MIDI data from MIDI files(Sequence object)Get.
File file = new File(path);
Sequence sequence = MidiSystem.getSequence(file);
//Set the acquired MIDI data in the sequencer.
sequencer.setSequence(sequence);
}
catch (InvalidMidiDataException e) {
e.printStackTrace();
}
catch (IOException e) {
e.printStackTrace();
}
//Sequencer playback
sequencer.start();
//Waiting for key input
System.out.println("Enter an appropriate character string to finish.");
System.out.print(">>");
scanner.next();
scanner.close();
//Sequencer stop
sequencer.stop();
//Close the sequencer and release the resources you were using.
sequencer.close();
}
}
Gets the objects needed for MIDI playback. The code below will automatically get the default device provided by the API. (If you just want to play it, the default is enough.)
** * [1] and [2] throw "MidiUnavailableException", so be sure to handle the exception. ** **
Sequencer sequencer = null;
try {
// [1]Get the default Sequencer connected to the device.
sequencer = MidiSystem.getSequencer();
// [2]Open the device and acquire resources.
sequencer.open();
}
catch (MidiUnavailableException e) {
e.printStackTrace();
}
Get MIDI data from a MIDI file. The acquired MIDI data is managed by an object called "Sequence".
Set the acquired object in the sequencer.
** * As before, [1] and [2] throw "MidiUnavailableException", so be sure to handle the exception. ** **
try {
// [1]MIDI data from MIDI files(Sequence object)Get.
File file = new File(path);
Sequence sequence = MidiSystem.getSequence(file);
// [2]Set the acquired MIDI data in the sequencer.
sequencer.setSequence(sequence);
}
catch (InvalidMidiDataException e) {
e.printStackTrace();
}
catch (IOException e) {
e.printStackTrace();
}
After that, you can control play / stop by calling play (start) and stop (stop) of the sequencer.
//Sequencer playback
sequencer.start();
//Sequencer stop
sequencer.stop();
Be careful not to forget to execute close () at the end of the program.
sequencer.close();
Recommended Posts