我想在未来的项目中实现MIDI声音的播放,但我目前的实现似乎占据了大量的内存。只需播放一些随机音符,从大约60磅到近600磅就可以攀爬。
这就是我现在所拥有的:
public class Sounds implements Runnable {
boolean running = true;
public void run() {
while (running = true) {
Piano();
try {
Thread.sleep(2500);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public void Piano(){
Random rng = new Random();
int ranKey = rng.nextInt(127);
try {
Synthesizer synth = MidiSystem.getSynthesizer();
synth.open();
MidiChannel[] channels = synth.getChannels();
channels[0].noteOn(ranKey, 64);
} catch (MidiUnavailableException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
Runnable runframe = new Sounds();
Thread thread = new Thread(runframe);
thread.start();
}
}
现在,我还是一个初学者,所以这可能是一个完全错误的方法。
您怎么看?