Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
218 views
in Technique[技术] by (71.8m points)

avaudioplayer - How to play a .MIDI file in a new thread in Java?

I am remaking part of a game in Java, and I need to know how to play the MIDI sound files. Preferably it would not involve importing any external libraries. It must also be runnable in a new thread, so that I can stack the individual sounds over the background song.

Thanks for your thoughts and time.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

This code plays two MIDI tracks at the same time (the 2nd sequence starts as soon as the 1st dialog is dismissed). No threads are explicitly created, but I imagine it would work much the same if they were wrapped in a Thread object.

import java.net.URL;
import java.util.Locale;
import javax.sound.midi.MidiDevice;
import javax.sound.midi.MidiSystem;
import javax.sound.midi.MidiUnavailableException;
import javax.sound.midi.Receiver;
import javax.sound.midi.Sequence;
import javax.sound.midi.Sequencer;
import javax.sound.midi.Transmitter;
import javax.swing.JOptionPane;
import org.apache.commons.lang.StringUtils;

class PlayMidi {

    public static boolean useExternalSynth = false;

    public static void main(String[] args) throws Exception {
        MidiDevice receivingDevice = getReceivingDevice();
        receivingDevice.open();

        URL url1 = new URL("http://pscode.org/media/EverLove.mid");

        Sequence sequence1 = MidiSystem.getSequence(url1);
        Sequencer sequencer1 = MidiSystem.getSequencer(false);
        Transmitter tx1 = sequencer1.getTransmitter();
        Receiver rx1 = receivingDevice.getReceiver();
        tx1.setReceiver(rx1);

        sequencer1.open();
        sequencer1.setSequence(sequence1);

        URL url2 = new URL("http://pscode.org/media/AftrMdnt.mid");

        Sequence sequence2 = MidiSystem.getSequence(url2);
        Sequencer sequencer2 = MidiSystem.getSequencer(false);
        Transmitter tx2 = sequencer2.getTransmitter();
        Receiver rx2 = receivingDevice.getReceiver();
        tx2.setReceiver(rx2);

        sequencer2.open();
        sequencer2.setSequence(sequence2);

        sequencer1.start();
        JOptionPane.showMessageDialog(null, "Everlasting Love");
        sequencer2.start();
        JOptionPane.showMessageDialog(null, "After Midnight");
    }

    private static MidiDevice getReceivingDevice()
        throws MidiUnavailableException {
        for (MidiDevice.Info mdi: MidiSystem.getMidiDeviceInfo()) {
            MidiDevice dev = MidiSystem.getMidiDevice(mdi);
            if (dev.getMaxReceivers() != 0) {
                String lcName =
                    StringUtils.defaultString(mdi.getName())
                               .toLowerCase(Locale.ENGLISH);
                if (lcName.contains(useExternalSynth? "usb": "java")) {
                    return dev;
                }
            }
        }
        return null;
    }

}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...