Advertisement

Unable to get midi file to play from JAR

Started by January 22, 2008 01:16 PM
-1 comments, last by sroutliffe 16 years, 10 months ago
I have written a simple JAVA program using, netbeans 6.0 IDE (I also tried netbeans 5.5), which loads and plays a midi file. My OS is Windows XP Professional. The file plays just fine when I run the program in the IDE. When I create a JAR file the program runs and other sound files (.wav) play correctly but the midi file will not play. The program contains exceptions to check that the file is found and loaded correctly. None of the exceptions fire and when I call the isRunning() method of the sequencer it returns true. The following is a listing of the program: public class Main { public static void main(String[] args) { midiPlayer player = new midiPlayer(); player.play(); } } import java.io.File; import java.io.IOException; import javax.sound.midi.InvalidMidiDataException; import javax.sound.midi.MetaEventListener; import javax.sound.midi.MetaMessage; import javax.sound.midi.MidiSystem; import javax.sound.midi.MidiUnavailableException; import javax.sound.midi.Receiver; import javax.sound.midi.Sequence; import javax.sound.midi.Sequencer; import javax.sound.midi.Synthesizer; import javax.sound.midi.Transmitter; import javax.swing.JOptionPane; public class midiPlayer implements MetaEventListener { private static final int END_OF_TRACK_MESSAGE = 47; private Sequencer sequencer; public midiPlayer() { try { sequencer = MidiSystem.getSequencer(); if (sequencer == null) { System.out.println("Cannot get a sequencer"); JOptionPane.showMessageDialog( null, "Cannot get a sequencer"); return; } sequencer.open(); sequencer.addMetaEventListener(this); if (!(sequencer instanceof Synthesizer)) { System.out.println("Linking the MIDI sequencer and synthesizer"); Synthesizer synthesizer = MidiSystem.getSynthesizer(); Receiver synthReceiver = synthesizer.getReceiver(); Transmitter seqTransmitter = sequencer.getTransmitter(); seqTransmitter.setReceiver(synthReceiver); } } catch(MidiUnavailableException ex) { sequencer = null; JOptionPane.showMessageDialog( null, ex); } } public Sequence getSequence(String fileName) { try { return MidiSystem.getSequence(new File(fileName)); } catch (InvalidMidiDataException ex) { ex.printStackTrace(); JOptionPane.showMessageDialog( null, ex); return null; } catch (IOException ex) { ex.printStackTrace(); JOptionPane.showMessageDialog( null, ex); return null; } } public void play() { Sequence sequence = getSequence("Sounds/flightofbee.mid"); try { sequencer.setSequence(sequence); sequencer.start(); } catch(InvalidMidiDataException ex) { ex.printStackTrace(); JOptionPane.showMessageDialog( null, ex); } } public void stop() { if(sequencer != null && sequencer.isOpen()) { sequencer.stop(); sequencer.i } } public void meta(MetaMessage event) { if(event.getType() == END_OF_TRACK_MESSAGE) { if(sequencer != null && sequencer.isOpen()) { //play(); //sequencer.start(); sequencer = null; } } } } Has anyone run into this problem before? Are there any known solutions? Any help would be most appreciated.

This topic is closed to new replies.

Advertisement