Example usage for javax.sound.midi Sequencer isRunning

List of usage examples for javax.sound.midi Sequencer isRunning

Introduction

In this page you can find the example usage for javax.sound.midi Sequencer isRunning.

Prototype

boolean isRunning();

Source Link

Document

Indicates whether the Sequencer is currently running.

Usage

From source file:Main.java

public static void streamMidiSequence(URL url)
        throws IOException, InvalidMidiDataException, MidiUnavailableException {
    Sequencer sequencer = null; // Converts a Sequence to MIDI events
    Synthesizer synthesizer = null; // Plays notes in response to MIDI events

    try {/*w  w  w.  ja  v a2  s .  c  om*/
        // Create, open, and connect a Sequencer and Synthesizer
        // They are closed in the finally block at the end of this method.
        sequencer = MidiSystem.getSequencer();
        sequencer.open();
        synthesizer = MidiSystem.getSynthesizer();
        synthesizer.open();
        sequencer.getTransmitter().setReceiver(synthesizer.getReceiver());

        // Specify the InputStream to stream the sequence from
        sequencer.setSequence(url.openStream());

        // This is an arbitrary object used with wait and notify to
        // prevent the method from returning before the music finishes
        final Object lock = new Object();

        // Register a listener to make the method exit when the stream is
        // done. See Object.wait() and Object.notify()
        sequencer.addMetaEventListener(new MetaEventListener() {
            public void meta(MetaMessage e) {
                if (e.getType() == END_OF_TRACK) {
                    synchronized (lock) {
                        lock.notify();
                    }
                }
            }
        });

        // Start playing the music
        sequencer.start();

        // Now block until the listener above notifies us that we're done.
        synchronized (lock) {
            while (sequencer.isRunning()) {
                try {
                    lock.wait();
                } catch (InterruptedException e) {
                }
            }
        }
    } finally {
        // Always relinquish the sequencer, so others can use it.
        if (sequencer != null)
            sequencer.close();
        if (synthesizer != null)
            synthesizer.close();
    }
}

From source file:tvbrowser.extras.reminderplugin.ReminderPlugin.java

/**
 * Plays a sound.//w w  w .  ja  v  a2s  . c om
 *
 * @param fileName
 *          The file name of the sound to play.
 * @return The sound Object.
 */
public static Object playSound(final String fileName) {
    try {
        if (StringUtils.endsWithIgnoreCase(fileName, ".mid")) {
            final Sequencer sequencer = MidiSystem.getSequencer();
            sequencer.open();

            final InputStream midiFile = new FileInputStream(fileName);
            sequencer.setSequence(MidiSystem.getSequence(midiFile));

            sequencer.start();

            new Thread("Reminder MIDI sequencer") {
                @Override
                public void run() {
                    setPriority(Thread.MIN_PRIORITY);
                    while (sequencer.isRunning()) {
                        try {
                            Thread.sleep(100);
                        } catch (Exception ee) {
                            // ignore
                        }
                    }

                    try {
                        sequencer.close();
                        midiFile.close();
                    } catch (Exception ee) {
                        // ignore
                    }
                }
            }.start();

            return sequencer;
        } else {
            final AudioInputStream ais = AudioSystem.getAudioInputStream(new File(fileName));

            final AudioFormat format = ais.getFormat();
            final DataLine.Info info = new DataLine.Info(SourceDataLine.class, format);

            if (AudioSystem.isLineSupported(info)) {
                final SourceDataLine line = (SourceDataLine) AudioSystem.getLine(info);

                line.open(format);
                line.start();

                new Thread("Reminder audio playing") {
                    private boolean stopped;

                    @Override
                    public void run() {
                        byte[] myData = new byte[1024 * format.getFrameSize()];
                        int numBytesToRead = myData.length;
                        int numBytesRead = 0;
                        int total = 0;
                        int totalToRead = (int) (format.getFrameSize() * ais.getFrameLength());
                        stopped = false;

                        line.addLineListener(new LineListener() {
                            public void update(LineEvent event) {
                                if (line != null && !line.isRunning()) {
                                    stopped = true;
                                    line.close();
                                    try {
                                        ais.close();
                                    } catch (Exception ee) {
                                        // ignore
                                    }
                                }
                            }
                        });

                        try {
                            while (total < totalToRead && !stopped) {
                                numBytesRead = ais.read(myData, 0, numBytesToRead);

                                if (numBytesRead == -1) {
                                    break;
                                }

                                total += numBytesRead;
                                line.write(myData, 0, numBytesRead);
                            }
                        } catch (Exception e) {
                        }

                        line.drain();
                        line.stop();
                    }
                }.start();

                return line;
            } else {
                URL url = new File(fileName).toURI().toURL();
                AudioClip clip = Applet.newAudioClip(url);
                clip.play();
            }
        }

    } catch (Exception e) {
        e.printStackTrace();
        if ((new File(fileName)).isFile()) {
            URL url;
            try {
                url = new File(fileName).toURI().toURL();
                AudioClip clip = Applet.newAudioClip(url);
                clip.play();
            } catch (MalformedURLException e1) {
            }
        } else {
            String msg = mLocalizer.msg("error.1", "Error loading reminder sound file!\n({0})", fileName);
            JOptionPane.showMessageDialog(UiUtilities.getBestDialogParent(MainFrame.getInstance()), msg,
                    Localizer.getLocalization(Localizer.I18N_ERROR), JOptionPane.ERROR_MESSAGE);
        }
    }
    return null;
}