Hi so I am trying to use this code to read from a text file, the text file is supposed to define a pitch by checking if the string of the next line starts with either stop, play, or duration and then pass it to synth so it can play.
Does anyone have any idea why it's causing Errors and not working?
The code and an example text file are as follows:
public class MyTuneRunnable implements Runnable {
//method start
public void run(){
Thread thread = Thread.currentThread();
thread.getName();
try {
Synthesizer synth = MidiSystem.getSynthesizer();
synth.open();
MidiChannel[] channels = synth.getChannels();
File file = new File(Loader.instance().getConfigDir().getParentFile().getAbsolutePath()+"/"+"LoadTunes"+"/"+Config.tuneName+".txt");
try {
Scanner intLoader = new Scanner(file);
Scanner stringLoader = new Scanner(file);
while (intLoader.hasNextLine()&stringLoader.hasNextLine()) {
int i = intLoader.nextInt();
String s = stringLoader.next();
if (s.startsWith("play")){
channels[channel].noteOn( i, volume);
}
if (s.startsWith("stop")){
channels[channel].noteOff( i, volume);
}
if (s.startsWith("duration")){
Thread.sleep(i);
}
}
intLoader.close();
stringLoader.close();
}
catch (FileNotFoundException e) {
e.printStackTrace();
}
synth.close();
}
catch (Exception e) {
e.printStackTrace();
}
}
}
As for how the text file looks... this is an example:
0 This is a comment
0
play 60 This is a C note and it is set to play because of 'play <note number>'
0
duration 200 This is saying the currently playing notes will make sound
0
stop 60 This stops playing the C note because of the 'stop <note number>'