/** * Saves the MIDI data from a pattern into a file. * * @param pattern the pattern to save * @param file the File to save the pattern to. Should include file extension, such as .mid */ public void saveMidi(Pattern pattern, File file) throws IOException { Sequence sequence = getSequence(pattern); int[] writers = MidiSystem.getMidiFileTypes(sequence); if (writers.length == 0) return; MidiSystem.write(sequence, writers[0], file); }
/** * Exports to a file. * * @param outputFileName The output file name * @throws InvalidFileFormatException If the file name doesn't end in .mid or .midi */ public void exportToFile(String outputFileName) throws InvalidFileFormatException { // Check for a valid file format. if (!outputFileName.endsWith(".mid") && !outputFileName.endsWith(".midi")) { String msg = "File names must end in .mid or .midi"; throw new InvalidFileFormatException(msg); } // Find a supported file type, and export the file. int[] types = MidiSystem.getMidiFileTypes(sequence); try { File outputFile = new File(outputFileName); MidiSystem.write(sequence, types[0], outputFile); } catch (IOException ex) { ex.printStackTrace(); System.exit(1); } }
public boolean saveMidiFile(File file) { try { int[] fileTypes = MidiSystem.getMidiFileTypes(sequence); if (fileTypes.length == 0) { System.out.println("Can't save sequence"); return false; } else { if (MidiSystem.write(sequence, fileTypes[0], file) == -1) { throw new IOException("Problems writing to file"); } return true; } } catch (SecurityException ex) { ex.printStackTrace(); return false; } catch (Exception ex) { ex.printStackTrace(); return false; } }