I've started to make a basic game in Java just as something to do.
Its all been going well, I have maps loading, drawing works fine etc.
I have it playing music for the maps and sound effects for stuff like collision.
However I feel like the music from MAP A(The one I am leaving) should fade into the music from MAP B(The map I am entering), is this possible?
The code im using to play the music is below:
try
{
if(clip != null)
{
clip.stop();
clip.close();
}
clip = AudioSystem.getClip();
URL a = this.getClass().getResource("/resource/sound/"+Filename + ".mid");
ais = AudioSystem.getAudioInputStream(a);
clip.open(ais);
clip.loop(999);
clip.start();
}
catch(Exception e)
{
}
As you can most probably see I am not very good in Java, but my game is going well so far and I am not, and I refuse to use an 'Engine' as I want complete control.
Any ideas?
Thanks
EDIT:
Thanks for all the answers, I have modified the code you gave me, but its not working =/
I have now made the code below, which is run on a new thread(hence the Runnable) so the music will fade while playing.
The code is below:
private Boolean _SwapMusic = false;
private String _Filename;
Thread soundThread;
Runnable r1 = new Runnable() {
public void run() {
while(true)
{
if(_SwapMusic)
{
_SwapMusic = false;
try
{
//Try to lower volume
for (int i = 0; i < 80; i ++) {
FloatControl gainControlA = (FloatControl) clip.getControl(FloatControl.Type.MASTER_GAIN);
gainControlA.setValue(i * -1f);
Thread.sleep(10);
}
}
catch(Exception e){ System.out.print("Lower volume error: " + e + " ");}
try{
clip.stop();
clip.close();
}
catch (Exception e) { System.out.print("clip.stop/close error: " + e + " ");}
try
{
clip = AudioSystem.getClip();
URL a = this.getClass().getResource("/resource/sound/"+ _Filename + ".mid");
ais = AudioSystem.getAudioInputStream(a);
clip.open(ais);
FloatControl gainControlB = (FloatControl) clip.getControl(FloatControl.Type.MASTER_GAIN);
gainControlB.setValue(-80f);
clip.loop(999);
clip.start();
}
catch(Exception e){ System.out.print("Play error: " + e + " ");}
try
{
//Try to higher volume
for (int i = -80; i < 0; i ++) {
FloatControl gainControlC = (FloatControl) clip.getControl(FloatControl.Type.MASTER_GAIN);
gainControlC.setValue(i);
Thread.sleep(3);
}
}
catch(Exception e){
System.out.print("Higher volume error: " + e + " ");
}
}
}
}
};
Just to make this clear what I am trying to make music from MAP A fade completely before the music for MAP B begins.
Thanks for all help so far.