Loading and Playing Midi Audio - Java Media

Java examples for Media:Audio

Description

Loading and Playing Midi Audio

Demo Code

import java.io.File;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;

import javax.sound.midi.InvalidMidiDataException;
import javax.sound.midi.MidiSystem;
import javax.sound.midi.MidiUnavailableException;
import javax.sound.midi.Sequence;
import javax.sound.midi.Sequencer;

public class Main {
  public static void main(String[] argv) throws Exception {
    try {//from w  w  w. j  av a  2s .  c om
      // From file
      Sequence sequence = MidiSystem.getSequence(new File("midifile"));

      // From URL
      sequence = MidiSystem.getSequence(new URL("http://hostname/midifile"));

      // Create a sequencer for the sequence
      Sequencer sequencer = MidiSystem.getSequencer();
      sequencer.open();
      sequencer.setSequence(sequence);

      // Start playing
      sequencer.start();
    } catch (MalformedURLException e) {
    } catch (IOException e) {
    } catch (MidiUnavailableException e) {
    } catch (InvalidMidiDataException e) {
    }
  }
}

Related Tutorials