import javax.sound.midi.spi.MidiFileReader; //导入方法依赖的package包/类
/**
* Obtains a MIDI sequence from the specified URL. The URL must point to
* valid MIDI file data for a file type recognized by the system.
*
* This operation can only succeed for files of a type which can be parsed
* by an installed file reader. It may fail with an
* {@code InvalidMidiDataException} even for valid files if no compatible
* file reader is installed. It will also fail with an
* {@code InvalidMidiDataException} if a compatible file reader is
* installed, but encounters errors while constructing the {@code Sequence}
* object from the file data.
*
* @param url the URL from which the {@code Sequence} should be constructed
* @return a {@code Sequence} object based on the MIDI file data pointed to
* by the URL
* @throws InvalidMidiDataException if the URL does not point to valid MIDI
* file data recognized by the system
* @throws IOException if an I/O exception occurs while accessing the URL
* @throws NullPointerException if {@code url} is {@code null}
*/
public static Sequence getSequence(final URL url)
throws InvalidMidiDataException, IOException {
Objects.requireNonNull(url);
List providers = getMidiFileReaders();
Sequence sequence = null;
for(int i = 0; i < providers.size(); i++) {
MidiFileReader reader = providers.get(i);
try {
sequence = reader.getSequence( url ); // throws IOException
break;
} catch (InvalidMidiDataException e) {
continue;
}
}
if( sequence==null ) {
throw new InvalidMidiDataException("could not get sequence from URL");
} else {
return sequence;
}
}