You don't need an external method to loop a Midi Sequence. Based on your code above you should use the following:
import java.io.*;
import java.util.*;
import javax.sound.midi.*;
public class Midiplayer{
public static void main(String[] args) throws InvalidMidiDataException, IOException, MidiUnavailableException {
//Create scanner object
Scanner in= new Scanner(System.in);
//Request Loop Count
System.out.println("How Many Loops?");
int loops= in.nextInt();
//Retrieve the MIDI File
System.out.println("Please type in the exact location of your midi:");
String fileAndLocation= in.next();
Sequence myseq = MidiSystem.getSequence(new File(fileAndLocation));
// Create a sequencer for the sequence
final Sequencer sequencer = MidiSystem.getSequencer();
sequencer.open();
sequencer.setSequence(myseq);
sequencer.setLoopCount(loops);
//Exit message
System.out.println("Press 'ctrl' and 'c' on keyboard simultaneously to end sequence and program.");
// Start playback, repeats automatically
sequencer.start();
//Don't forget to close reader
in.close();
}
}
Do note though that the above does not create an infinite loop. My guess is that you need to set "loops" to 1 and increment it within a while statement that tests like the following:
while(loops>0){
//Start/restart Midi Sequence
sequencer.start();
//increment loops
loops++;
//Re-instantiate .setLoopCount()
sequencer.setLoopCount(loops);
}
If the loop determination is user based, then have the user input 0 in in the place of infinity so that:
if(loops==0){
//Declare and instantiate a boolean to test
boolean infinite=true;
//Make loops equal to 1 so that the later while statement initiates and repeates
loops++;
//Re-instantiate .setLoopCount()
sequencer.setLoopCount(loops);
}
if(infinite==true){
while(loops>0){
//Start/restart Midi Sequence
sequencer.start();
//increment loops
loops++;
//Re-instantiate .setLoopCount(), thus always adding a loop to setLoopCount(), making it infinite because loops will always be greater than zero
sequencer.setLoopCount(loops);
}
}
else{
//Starts sequence and repeats according to defined loop while loops>0 (so long as you have an error cather for loops<0)
sequencer.start();
}
To end the user interface for an infinite loop, at some point remind the user that in COMMAND PROMPT, 'ctrl'+'c' quits the program.
I hope this helps a lot of people (I based it off my own midi program, which works). However, do note that anything about an infinite loop I have not yet tested. It is only theory based on heavy analysis of the situation and the available classes and variables.
Do note that .setLoopCounter() is present in Java 1.4.1 and on.
Thank you and I am glad to help.