Random rand = new Random(); try { Synthesizer synth = MidiSystem.getSynthesizer(); synth.open(); int note = rand.nextInt(128); synth.getChannels()[0].noteOn(note, 80); } catch (MidiUnavailableException e) { e.printStackTrace(); }
/** * Gets a transmitter for this device (which is also added to the internal list * * @see javax.sound.midi.MidiDevice#getTransmitter() * * @return a transmitter for this device * */ @Override public Transmitter getTransmitter() throws MidiUnavailableException { throw new MidiUnavailableException("CoreMidiDestination has no sources (Transmitters)"); }
System.err.println("Problem obtaining MIDI device which supposedly exists:" + e.getMessage());
@EventHandler public void onJoin(PlayerJoinEvent event) { MidiJingleSequencer sequencer; try { File file = new File(CommandBook.inst().getDataFolder(), "intro.mid"); if (file.exists()) { sequencer = new MidiJingleSequencer(file, false); getJingleNoteManager().play(event.getPlayer().getName(), sequencer); } } catch (MidiUnavailableException e) { CommandBook.logger().log(Level.WARNING, "Failed to access MIDI: " + e.getMessage()); } catch (InvalidMidiDataException e) { CommandBook.logger().log(Level.WARNING, "Failed to read intro MIDI file: " + e.getMessage()); } catch (FileNotFoundException ignored) { } catch (IOException e) { CommandBook.logger().log(Level.WARNING, "Failed to read intro MIDI file: " + e.getMessage()); } }
SwingUtilities.invokeLater(new Runnable() { public void run() { try { Synthesizer synth = MidiSystem.getSynthesizer(); synth.open(); } catch (MidiUnavailableException e) { e.printStackTrace(); } JFrame frame = new JFrame("MIDI Swing Solution 1?"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true); } });
/** * Creates and returns a MIDI Receiver for use with this Device * * @return the created receiver * * @see javax.sound.midi.MidiDevice#getReceiver() * */ @Override public Receiver getReceiver() throws MidiUnavailableException { throw new MidiUnavailableException("CoreMidiSource has no receivers"); }
+ e.getMessage()); } catch (InvalidMidiDataException e) { throw new CommandException("Failed to read intro MIDI file: "
e.printStackTrace(); e.printStackTrace(); e.printStackTrace(); e.printStackTrace(); e1.printStackTrace(); e.printStackTrace(); e.printStackTrace(); e.printStackTrace(); e1.printStackTrace();
/** Creates a new Transmitter object associated with this instance. * In this implementation, an unlimited number of Transmitters * per MidiDevice can be created. */ public Transmitter getTransmitter() throws MidiUnavailableException { if (! getUseTransmitter()) { throw new MidiUnavailableException("Transmitters are not supported by this device"); } return new TTransmitter(); }
+ e.getMessage()); } catch (InvalidMidiDataException e) { throw new CommandException("Failed to read intro MIDI file: "
/** Creates a new Receiver object associated with this instance. * In this implementation, an unlimited number of Receivers * per MidiDevice can be created. */ public Receiver getReceiver() throws MidiUnavailableException { if (! getUseReceiver()) { throw new MidiUnavailableException("Receivers are not supported by this device"); } return new TReceiver(); }
e.printStackTrace(); } finally { if (sequencer.isOpen()) {
/** Creates a new Receiver object associated with this instance. * In this implementation, an unlimited number of Receivers * per MidiDevice can be created. */ public Receiver getReceiver() throws MidiUnavailableException { if (! getUseReceiver()) { throw new MidiUnavailableException("Receivers are not supported by this device"); } return new TReceiver(); }
/** Creates a new Transmitter object associated with this instance. * In this implementation, an unlimited number of Transmitters * per MidiDevice can be created. */ public Transmitter getTransmitter() throws MidiUnavailableException { if (! getUseTransmitter()) { throw new MidiUnavailableException("Transmitters are not supported by this device"); } return new TTransmitter(); }
/** * Opens the Core MIDI Device * * @throws MidiUnavailableException if the MIDI system cannot be used * */ @Override public void open() throws MidiUnavailableException { if (isOpen.compareAndSet(false, true)) { try { // Create the input port if not already created if (input.get() == null) { input.set(CoreMidiDeviceProvider.getMIDIClient().inputPortCreate("Core Midi Provider Input")); } // And connect to it input.get().connectSource(this); // Get the system time in microseconds startTime = this.getMicroSecondTime(); } catch (CoreMidiException e) { e.printStackTrace(); throw new MidiUnavailableException(e.getMessage()); } } }
import javax.sound.midi.*; import java.io.IOException; import java.io.InputStream; import java.io.FileInputStream; import java.io.FileNotFoundException; public class MidiPlayer{ public static void main(String[] args) { try { Sequencer sequencer = MidiSystem.getSequencer(); if (sequencer == null) throw new MidiUnavailableException(); sequencer.open(); FileInputStream is = new FileInputStream("music.mid"); Sequence Seq = MidiSystem.getSequence(is); sequencer.setSequence(Seq); sequencer.start(); } catch (Exception e) { e.printStackTrace(); } } here is a sample code that shows you how to play MIDI files in your java program, hope it helps