001package bradleyross.music.imported; 002import java.io.File; 003import java.io.IOException; 004import java.io.InputStream; 005// import javax.sound.midi.MidiDevice; 006import javax.sound.midi.Sequencer; 007import javax.sound.midi.Sequence; 008import javax.sound.midi.MidiSystem; 009import javax.sound.midi.InvalidMidiDataException; 010import javax.sound.midi.MidiUnavailableException; 011// import javax.sound.midi.*; 012// import java.io.*; 013/* 014 * http://blog.taragana.com/index.php/archive/how-to-play-a-midi-file-from-a-java-application/ 015 */ 016/** 017 * Plays a midi file provided on command line. 018 * 019 * <p>The first and only parameter is the name of the file to be played.</p> 020 * 021 * <p>This is based on the GitHub Gist item <a href="http://gist.github.com/indy/360540" 022 * target-"_blank">indy/play_midi_filejava</a>. It was mentioned in 023 * <a href="http://blog.taragana.com/index.php/archive/how-to-play-a-midi-file-from-a-java-application/" 024 * target="_blank"> 025 * http://blog.taragana.com/index.php/archive/how-to-play-a-midi-file-from-a-java-application/</a></p> 026 * @author Bradley Ross 027 * 028 */ 029 030public class MidiPlayer { 031 /** 032 * Main driver program. 033 * 034 * <p>The code for verifying that it is a MIDI file needs to be improved.</p> 035 * 036 * @param args The first and only argument is the name of the file to be played 037 */ 038 protected String args[]; 039 public MidiPlayer(String value[]) { 040 args = value; 041 } 042 public static void main(String args[]) { 043 MidiPlayer instance = new MidiPlayer(args); 044 instance.run(); 045 } 046 protected void run () { 047 String file; 048 Sequence sequence = null; 049 // Argument check 050 if(args.length == 0) { 051 /* 052 * Instead of exiting if the name of a MIDI file is not provided, 053 * the code was modified to play a MIDI file contained in the jar 054 * file instead. 055 */ 056 try { 057 InputStream in = MidiPlayer.class.getClassLoader().getResourceAsStream("AGNICRT.MID"); 058 sequence = MidiSystem.getSequence(in); 059 } catch (InvalidMidiDataException e) { 060 e.printStackTrace(); 061 } catch (IOException e) { 062 e.printStackTrace(); 063 } 064 } else { 065 try { 066 file = args[0]; 067 068 if(!file.toLowerCase().endsWith(".mid") && !file.toLowerCase().endsWith(".midi")) { 069 helpAndExit(); 070 } 071 072 File midiFile = new File(file); 073 if(!midiFile.exists() || midiFile.isDirectory() || !midiFile.canRead()) { 074 helpAndExit(); 075 } 076 sequence = MidiSystem.getSequence(new File(file)); 077 }catch (InvalidMidiDataException e) { 078 e.printStackTrace(); 079 } catch (IOException e) { 080 e.printStackTrace(); 081 } 082 } 083 // Play once 084 try { 085 Sequencer sequencer = MidiSystem.getSequencer(); 086 sequencer.open(); 087 sequencer.setSequence(sequence); 088 // sequencer.open(); 089 sequencer.start(); 090 System.out.println("Starting Sequencer"); 091 while(true) { 092 if(sequencer.isRunning()) { 093 try { 094 Thread.sleep(1000); // Check every second 095 } catch(InterruptedException ignore) { 096 break; 097 } 098 } else { 099 break; 100 } 101 } 102 // Close the MidiDevice & free resources 103 sequencer.stop(); 104 sequencer.close(); 105 } catch(MidiUnavailableException mue) { 106 System.out.println("Midi device unavailable!"); 107 mue.printStackTrace(); 108 } catch(InvalidMidiDataException imde) { 109 System.out.println("Invalid Midi data!"); 110 imde.printStackTrace(); 111 } 112 } 113 /** Provides help message and exits the program */ 114 private static void helpAndExit() { 115 System.out.println("Usage: java MidiPlayer midifile.mid"); 116 System.exit(1); 117 } 118} 119 120 121