Close the MIDI sequencer (so that the program can exit instead of just hanging). - Java javax.sound.midi

Java examples for javax.sound.midi:Sequence

Description

Close the MIDI sequencer (so that the program can exit instead of just hanging).

Demo Code


//package com.java2s;

import javax.sound.midi.Sequencer;

public class Main {
    static Sequencer sequencer;

    /**/*from   w ww. ja  va2  s  .  c  o  m*/
     * Close the sequencer (so that the program can exit instead of just hanging).
     */
    public static void closeSequencer() {
        try {

            if (sequencer != null) {
                if (sequencer.isOpen()) {
                    stopPlaying();
                    sequencer.close();
                }
            }

        } catch (Exception e) {
            e.printStackTrace();
            System.exit(1);
        }
    }

    /** 
     * Stop playing a sequence.
     */
    public static void stopPlaying() {
        try {

            if (sequencer != null) {
                if (sequencer.isOpen()) {
                    if (sequencer.isRunning()) {
                        sequencer.stop();
                        sequencer.setTickPosition(0);
                    }
                }
            }

        } catch (Exception e) {
            e.printStackTrace();
            System.exit(1);
        }
    }
}

Related Tutorials