Where filename is the midi file. I want to load a full path (from local harddisk) into the getSequence but it doesn't accept except the filename itself. Sorry that I not good in Java and wish there is a solution for my need.
song = MidiS开发者_如何学Cystem.getSequence(getClass().getResourceAsStream(filename));
and the Stackoverflow's richtext toolbar is missing in my Firefox browse?
[Update]
try { song = MidiSystem.getSequence(new FileInputStream(filename)); sequencer = MidiSystem.getSequencer(); sequencer.setSequence(song); sequencer.open(); sequencer.addMetaEventListener(this); sequencer.start(); } catch (InvalidMidiDataException e) { System.out.println("Bad midi file: " + filename); System.exit(1); } catch (MidiUnavailableException e) { System.out.println("No sequencer available"); System.exit(1); } catch (IOException e) { System.out.println("Could not read: " + filename); System.exit(1); }
getRecourceAsStream()
is helpful only if the file is withing the classpath. If you have the file on the file system you should call:
song = MidiSystem.getSequence(new FileInputStream("/path/to/midi/file"));
try this:
song = MidiSystem.getSequence(getClass().getResourceAsStream("@filextension"));
or you could use filereader.
精彩评论