我似乎無法讓樂器改變。我切換儀器的值,但在輸出上沒有任何不同。無論我嘗試什麼樣的價值,我只能得到一架鋼琴樂器。以下是簡單的代碼。有沒有人有什麼建議?還是我錯過了儀器對象的基礎?Java MIDI合成器 - 無法更換樂器
import javax.sound.midi.*;
//import javax.sound.*;
public class Drum {
static int instrument = 45;
static int note = 100;
static int timbre = 0;
static int force = 100;
public static void main(String[] args) {
Synthesizer synth = null;
try {
synth = MidiSystem.getSynthesizer();
synth.open();
}
catch (Exception e) {
System.out.println(e);
}
Soundbank soundbank = synth.getDefaultSoundbank();
Instrument[] instr = soundbank.getInstruments();
synth.loadInstrument(instr[instrument]); //Changing this int (instrument) does nothing
MidiChannel[] mc = synth.getChannels();
mc[4].noteOn(note, force);
try { Thread.sleep(1000); }
catch(InterruptedException e) {}
System.out.println(instr[instrument].getName());
synth.close();
}
}
你不能只加載一個樂器,你也必須發送一個程序改變信息。我不是Java程序員,所以我不知道該怎麼做,但這是你必須做的。請參閱http://download.oracle.com/javase/1.4.2/docs/api/javax/sound/midi/Synthesizer.html#loadInstrument(javax.sound.midi.Instrument) – Brad 2011-02-03 00:36:09