// Midi.java // Based on BasicGUI.java, this shows how to load midi files. // David Kosbie, 15-111/AB, Spring 2007 /* Standard disclaimer: As usual with sample code designed (often in class!) to demonstrate specific issues, the style may not be perfect (it is especially lacking comments and some top-down design), and there may even be a bug or two lurking in the code! This sample demonstrates the use of a very simple MidiPlayer class. This class only has a few static methods, allowing you to play or stop a midi file (and you can play it once or looping). For more advanced use, you can get the Sequencer object. See the Java API for details. ////////////////////////////////////////////// // MidiPlayer API (all statics) ////////////////////////////////////////////// class MidiPlayer { public static void play(String filename); public static void play(String filename, boolean loop); public static Sequencer getSequencer(); // for advanced use! public static void stop(); } */ import java.awt.*; import java.awt.event.*; import java.util.*; import javax.swing.*; // Additional imports for MidiPlayer: import java.io.*; import javax.sound.midi.*; public class Midi extends IntroCsApplication { public static void main(String[] args) { new Midi(); } JLabel nowPlaying; public void init() { add(nowPlaying = newCenteredLabel("No midi file playing yet!")); addButtons("Play Next", "Play Next (Looping)", "Stop"); } public void playNextMidiFile(boolean loop) { String midiFilename = getNextMidiFilename(); if (midiFilename == null) nowPlaying.setText("No midi files found!!!"); else { nowPlaying.setText("Now playing: " + midiFilename.toString() + " "); MidiPlayer.play(midiFilename,loop); } } public void stopPlayingMidi() { MidiPlayer.stop(); nowPlaying.setText("Midi player stopped!"); } public void onButtonPressed(String label) { if (label.equals("Play Next")) playNextMidiFile(false); else if (label.equals("Play Next (Looping)")) playNextMidiFile(true); else if (label.equals("Stop")) stopPlayingMidi(); } // find next ".mid" file in this directory // or return null if none are found public String getNextMidiFilename() { java.io.File directory = new java.io.File("."); String[] files = directory.list(); for (int i=0; i