This site requires JavaScript, please enable it in your browser!
Greenfoot back
Vellyxenya
Vellyxenya wrote ...

2016/2/14

Need a tip for a sound

Vellyxenya Vellyxenya

2016/2/14

#
Hi I have a sound that lasts like 1 sec but I just need the first milliseconds (it's for a laser and the original sound faint at the end) I tired something like this:
long n = System.currentTimeMillis();
                        if(System.currentTimeMillis()>100)
                        {
                            sound.stop();
                            sound.play();
                        }
But it doesn't work, it waits until the sound is finished to play it again, help me please ^^
danpost danpost

2016/2/14

#
Currently, line 1 is setting the variable 'n' to the current system time in milliseconds (which, by the way, is some large value -- much larger than 100). Then, you are not even using that variable, but getting the system time again in line 2, which will always return a true condition. However, this should cause your GreenfootSound 'sound' object to stop and restart at the beginning -- and, if it is not doing so, maybe more information is needed (posting the entire class may help). You probably do not need to work with system time on this issue as the cycles of acts is usually enough to get a sense of time. One hundred milliseconds is one-tenth of a second which is approximately 5 or 6 act cycles (usually). So, an instance timer field can be used to count the necessary act cycles to determine when to stop the sound.
Vellyxenya Vellyxenya

2016/2/14

#
How come that I forgot to use my n-variable?? However I tried the following code:
soundCounter++;
        if(soundCounter%5 == 0)
        {
            sound.stop();
            sound.play();
        }
and it still wait the sound to come to end before it reinitialize it. I'm doing tests so the classe is empty (there's only the act method) and the: GreenfootSound sound = new GreenfootSound("Laser.wav");
danpost danpost

2016/2/14

#
It might have something to do with you stopping and starting it on the same act cycle. Try:
if (soundCounter == 0)
{
    sound.play();
}
soundCounter++;
if (soundCounter == 6)
{
    sound.stop();
    soundCounter = 0;
}
Vellyxenya Vellyxenya

2016/2/14

#
It finally works! thank you very much!
Vellyxenya Vellyxenya

2016/2/14

#
You know when you start a sound there's quite always that little sound that you barely hear it that only lasts like 0.01 sec... Do you have any idea to still have a perfectly continuous sound? (for a long duration laser)
Vellyxenya Vellyxenya

2016/2/15

#
It's ok, it doesn't do that awkward sound anymore with a high-pitched one. I got one last problem: I have a background music that runs non-stop, but whenever there's another sound (like shooting sounds) the volume of the background music turns down, is there a way to have always the same volume for the background music please even if there's also another sound running??
You need to login to post a reply.