Plays a midi file. - Java javax.sound.midi

Java examples for javax.sound.midi:MidiSystem

Description

Plays a midi file.

Demo Code


//package com.java2s;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;

import java.io.InputStream;

import javax.sound.midi.MidiSystem;
import javax.sound.midi.Sequencer;

public class Main {
    /**//from w w w.  ja  v a2s. c o  m
     * Plays a midi file. Useful for alerts
     */
    public static void playSound() {
        try {
            InputStream inputStream = new BufferedInputStream(
                    new FileInputStream(new File("midifile.mid")));
            Sequencer sequencer = MidiSystem.getSequencer();
            sequencer.open();
            sequencer.setSequence(inputStream);
            sequencer.start();
            Thread.sleep(5000);
            sequencer.stop();
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }
}

Related Tutorials