我對Java的MIDI功能瞭解不多。事實上,它完全令我感到困惑。然而,我想要做的是只需構建一個簡單的應用程序,將播放一個音符。MIDI初學者 - 需要播放一個音符
如何使用Java Sound播放單個MIDI音符?
網絡上對此的支持幾乎是不存在的,我完全處於虧損狀態。
我對Java的MIDI功能瞭解不多。事實上,它完全令我感到困惑。然而,我想要做的是只需構建一個簡單的應用程序,將播放一個音符。MIDI初學者 - 需要播放一個音符
如何使用Java Sound播放單個MIDI音符?
網絡上對此的支持幾乎是不存在的,我完全處於虧損狀態。
我知道這是一個非常古老的問題,但作爲一名新手程序員,我很難搞清楚如何做到這一點,所以我想我會分享下面的hello-world-style程序,它會獲得Java播放單個midi音符以幫助其他人入門。
import javax.sound.midi.*;
public class MidiTest{
public static void main(String[] args) {
try{
/* Create a new Sythesizer and open it. Most of
* the methods you will want to use to expand on this
* example can be found in the Java documentation here:
* https://docs.oracle.com/javase/7/docs/api/javax/sound/midi/Synthesizer.html
*/
Synthesizer midiSynth = MidiSystem.getSynthesizer();
midiSynth.open();
//get and load default instrument and channel lists
Instrument[] instr = midiSynth.getDefaultSoundbank().getInstruments();
MidiChannel[] mChannels = midiSynth.getChannels();
midiSynth.loadInstrument(instr[0]);//load an instrument
mChannels[0].noteOn(60, 100);//On channel 0, play note number 60 with velocity 100
try { Thread.sleep(1000); // wait time in milliseconds to control duration
} catch(InterruptedException e) { }
mChannels[0].noteOff(60);//turn of the note
} catch (MidiUnavailableException e) {}
}
}
上述代碼主要是通過剪切,粘貼和混淆幾個在線教程中的代碼創建的。這裏是我找到的最有幫助的教程:
http://www.ibm.com/developerworks/library/it/it-0801art38/ 這是一個很好的教程,可能有你正在尋找的一切;不過,起初它可能有點壓倒性。
http://patater.com/gbaguy/javamidi.htm 功能非工作代碼由15歲的孩子寫的。這是令人驚訝的 - 我發現的最有用的東西。
祝你好運。 乾杯。
到目前爲止您已閱讀過哪些教程,以及它不起作用? – 2013-05-09 13:26:47
瀏覽從[Java聲音信息。頁面(http://stackoverflow.com/tags/javasound/info)。還有一個應該播放MIDI樂曲的短音源。 – 2013-05-09 13:29:18
Sierra和Bates的「Head First Java」提供了示例代碼。 – 2013-05-10 19:09:56