Hi!
I've been extremely successful in making a 2D RPG using some of Bucky's Java Game Development tutorials from TheNewBoston, and generally just pushing my own envelope. Now I've run into a problem I CANNOT solve. I've tried for days now, and when you see what the problem is, you will understand my frustration on the matter.
I've tried to implement a simple MIDIPlayer (class-code supplied at the bottom). The thing works great, until I export to .jar, then no sound plays, but the game keeps chugging away. So, I read up on it, and apparently there's some problem with soundbanks, so I installed Java's deluxe soundbank, to no avail.
NOW, finally I've made it work...but ONLY if the MIDI-files are in the same folder as the .jar file, or a folder in that directory. So I read up on THAT, and it seems you're not supposed to use the standard filepath-method when loading streams (mainly audio) from a .jar file. ONLY if you're referencing an actual file in the folder of the .jar, as per example:
getSequence("SomeMIDIfile.mid");
or in a dedicated folder next to it, as per example:
getSequence("music/SomeMIDIfile.mid");
In fact, you're supposed to reference it using the URL class, and possibly converting that to a URI, and then use the path you can get from there. So I tried that in MANY different forms, and the weirdest thing happens!
The first couple of tries, I got a nullpointer exception, because I was trying to find the correct path-syntax, and when I finally got it, it gave me an error, stating: "Java cannot find the file at the specified location: " and then proceeds to write the correct path, just to mock me

SO, I'm not getting a nullpointer, because it does find the file...but it doesn't find the file...
The file has been imported into my project on the path: src/snm/music/midis/SomeMIDI.mid
These are the methods I've tried to use to get a usable path (tried ALL of them with every single combination of directories in the path above), and they all end up giving me that terrible error-message when I hit the right "spot".
getSequence(getClass().getResource("/snm/music/midis/SomeMIDIfile.mid").getPath());
getSequence(getClass().getClassLoader().getResource("/snm/music/midis/SomeMIDIfile.mid").getPath());
URL url = new URL("/snm/music/midis/SomeMIDIfile.mid");
getSequence(url.getPath());
As I said, I've also tried to convert the URL to a URI to circumvent the %20's which appear in place of spaces when getting a file-path. I've ALSO tried to rewrite the MIDIPlayer so getSequence doesn't work with File(), but with FileInputStream() instead, still to no avail. It is the same thing with wav-files. Haven't tried MP3s, but logically it should be the same.
I'm at wit's end...and I have exhausted Google. They called me and told me to stop searching for JAVA +MIDI +ARGH over and over again...something about their revenues.
PLEASE, can anyone cast some light on this subject?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151
| package snm.music.midiplayer;
import java.io.File; import java.io.IOException; import javax.sound.midi.*;
public class MidiPlayer implements MetaEventListener {
public static final int END_OF_TRACK_MESSAGE = 47;
private Sequencer sequencer; private boolean loop; private boolean paused; private Sequence worldMusic = getSequence("MainTheme.mid"); private Sequence fightMusic = getSequence("Fighting.mid");
public MidiPlayer() { try { sequencer = MidiSystem.getSequencer(); sequencer.open(); sequencer.addMetaEventListener(this); } catch ( MidiUnavailableException ex) { sequencer = null; } }
public Sequence getSequence(String filename) { try { return MidiSystem.getSequence(new File(filename)); } catch (InvalidMidiDataException ex) { ex.printStackTrace(); return null; } catch (IOException ex) { ex.printStackTrace(); return null; } }
public void playFight(){ play(fightMusic, true); } public void playWorldmap(){ play(worldMusic, true); }
public void play(Sequence sequence, boolean loop) { if (sequencer != null && sequence != null) { try { sequencer.setSequence(sequence); sequencer.start(); this.loop = loop; } catch (InvalidMidiDataException ex) { ex.printStackTrace(); } } }
public void meta(MetaMessage event) { if (event.getType() == END_OF_TRACK_MESSAGE) { if (sequencer != null && sequencer.isOpen() && loop) { sequencer.start(); } } }
public void stop() { if (sequencer != null && sequencer.isOpen()) { sequencer.stop(); sequencer.setMicrosecondPosition(0); } }
public void close() { if (sequencer != null && sequencer.isOpen()) { sequencer.close(); } }
public Sequencer getSequencer() { return sequencer; }
public void setPaused(boolean paused) { if (this.paused != paused && sequencer != null) { this.paused = paused; if (paused) { sequencer.stop(); } else { sequencer.start(); } } }
public boolean isPaused() { return paused; } } |