如何在 Java 的新线程中播放 .MIDI 文件? [英] How to play a .MIDI file in a new thread in Java?

查看:25
本文介绍了如何在 Java 的新线程中播放 .MIDI 文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在用 Java 重新制作游戏的一部分,我需要知道如何播放 MIDI 声音文件.最好不涉及导入任何外部库.它还必须可以在新线程中运行,以便我可以将各个声音叠加在背景歌曲上.

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.

感谢您的想法和时间.

推荐答案

此代码同时播放两个 MIDI 轨道(第一个对话框关闭后,第二个序列立即开始).没有显式创建线程,但我想如果将它们包装在 Thread 对象中,它的工作方式会大致相同.

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;
    }

}

这篇关于如何在 Java 的新线程中播放 .MIDI 文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆