I've attached the code that handles the music playing part. I've set it up to where in main, I set up a "Scene_Title" object, and inside this class, the track object (as well as other objects specific to the title screen like sprites etc...). The Scene_Title object gets continuously updated from main, but none of this really matters right now since it just gets stuck in the Track class until the file is finished. I was looking around, and saw a topic that talked about multi threading, is this what I would need to do so that the compiler can focus on other things while it plays out the track? And how do you tell the compiler to multi thread something? Thanks for any feed back.
Also, I've tried removing the while loop, and just loading all the bit data in one go. I was reading something that said you could do this, and it would simply remember your request and load in pieces of it at a time, but I still had the same problem when I did this.
import java.io.File;
import java.io.IOException;
import javax.sound.sampled.*;
public class Track {
public Track(String name) throws IOException {
//Get a mixer
Mixer.Info[] mixerinfo = AudioSystem.getMixerInfo();
Mixer mixer = AudioSystem.getMixer(mixerinfo[0]);
//Get the file
String separator = System.getProperty("file.separator");
String rootdir = System.getProperty("user.dir");
String trackPath = rootdir + separator + "resources" + separator + "music" +
separator + name;
AudioFileFormat format;
try {
format = AudioSystem.getAudioFileFormat(new File(trackPath));
//get a stream for my file
AudioInputStream stream = AudioSystem.getAudioInputStream(new File(trackPath));
SourceDataLine line;
DataLine.Info info2 = new DataLine.Info(SourceDataLine.class, format.getFormat());
//DataLine.Info info = new DataLine.Info(Clip.class, format.getFormat());
try {
line = (SourceDataLine) mixer.getLine(info2);
line.open();
// get the total length of the sound file in bytes
long length = format.getByteLength();
int bufferSize = line.getBufferSize();
long total = 0;
boolean stopped = false;
line.start();
int nBytesRead = 0;
byte[] bytes = new byte[bufferSize];
try {
while (nBytesRead != -1){
nBytesRead = stream.read(bytes, 0, bytes.length);
if (nBytesRead >= 0){
line.write(bytes, 0, nBytesRead);
}
}
} catch (IOException e){
e.printStackTrace();
return;
} finally {
line.drain();
line.close();
}
} catch (LineUnavailableException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (UnsupportedAudioFileException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}