Let's say I've a main GameLoop
Inside this loop I have my Game Updates
I'm handling my Events for Sprite collision testing with each Iteration
If collision is true, play audio file
Here's where the problem occurs
The Audio clip will either play Rapidly, while the game is frozen
or
It will play w/ delay like I want but the entire Game comes to a halt other than the Audio Clip.
I'm just looking for some tips on Threading Basically. As far as i'm aware it'll be the best way to handle this problem and I can't seem to get it running correctly.
Note I would extend Thread on main class but already extends Canvas, needed.
public Main()
{
boolean running = true;
while(running)
{
// check for collision (returns boolean)
// if true proceed to execute Entity.doLogic()
// this then activates the AudioClip class' .playAudioClip(this, path)
// the audio Clip is then played and once it's done it'll return
// returns and instantly goes back to playing again
// meanwhile the loop Freezes up on me.
}
}
And This is the actual Sound.class
public class Sounds
{
public void startSound()
{
String path = "path";
playAudioClip(game, path);
}
public void playAudioClip(String path)
{
try
{
Clip clip = AudioSystem.getClip();
clip.open(AudioSystem.getAudioInputStream(new File(path)));
clip.start();
}
catch(Exception e)
{
System.out.println("Problem loading audio file");
}
try{Thread.sleep(500);}catch(Exception ex){System.out.println("Problem with Sleep");};
}
}
I've tried the below and same situation (Calling it by s.start() and s.run() no difference) using .start() would throw err in thread, will recreate real quick and share)
public class Sounds extends Thread
{
@Override
public void run()
{
String path = "path";
playAudioClip(game, path);
}
public void playAudioClip(String path)
{
try
{
Clip clip = AudioSystem.getClip();
clip.open(AudioSystem.getAudioInputStream(new File(path)));
clip.start();
}
catch(Exception e)
{
System.out.println("Problem loading audio file");
}
try{Thread.sleep(500);}catch(Exception ex){System.out.println("Problem with Sleep");};
}
}
throws to console "java.lang.IllegalThreadStateException" invoking with start() only defining run() inside of this object
Multithreading in the wrong way prime example 243 max threads going at any point there