Random Sound

Hi all!

I am pretty new to the JMonkeyEngine and wanted to play randomly one of 6 scary sound in my game.

A random number ® is chosen everytime the user presses a key and the sound will be played…

but always when the game TRIES to play the sound, the whole Screen freezes and i have to stop the proces in the monkeyplatform…



Here the code snippet:



public void onAction(String binding, boolean value, float tpf) {

r = (int) Math.round(Math.random() * 100);

switch ® {

case 1:

scaryAudio = new AudioNode(assetManager, “Sounds/scarySound0.ogg”, false);

scaryAudio.setLooping(false);

scaryAudio.setVolume(2);

scaryAudio.play();



case 2:

scaryAudio = new AudioNode(assetManager, “Sounds/scarySound1.ogg”, false);

scaryAudio.setLooping(false);

scaryAudio.setVolume(2);

scaryAudio.play();



case 6:

scaryAudio = new AudioNode(assetManager, “Sounds/scarySound2.ogg”, false);

scaryAudio.setLooping(false);

scaryAudio.setVolume(2);

scaryAudio.play();



case 3:

scaryAudio = new AudioNode(assetManager, “Sounds/scarySound3.ogg”, false);

scaryAudio.setLooping(false);

scaryAudio.setVolume(2);

scaryAudio.play();



case 4:

scaryAudio = new AudioNode(assetManager, “Sounds/scarySound4.ogg”, false);

scaryAudio.setLooping(false);

scaryAudio.setVolume(2);

scaryAudio.play();



case 5:

scaryAudio = new AudioNode(assetManager, “Sounds/scarySound5.ogg”, false);

scaryAudio.setLooping(false);

scaryAudio.setVolume(2);

scaryAudio.play();



default:



}

if (binding.equals(“Left”)) {

if (value) {

left = true;

} else {

left = false;

}

} else if (binding.equals(“Right”)) {

if (value) {

right = true;

} else {

right = false;

}

} else if (binding.equals(“Up”)) {

if (value) {

up = true;

} else {

up = false;

}

} else if (binding.equals(“Down”)) {

if (value) {

down = true;

} else {

down = false;

}

} else if (binding.equals(“Jump”)) {

player.jump();

}

}



what’s the mistake???

I think the sounds are never played, because the random limit is too long 100. Also, the problem might be into your ScaryAudio class. Try to code a bit more OO :). Does it cause any exception?

you have no breaks in your switch statement, also as glauco says why does it go to up 100?

I would do:

[java]

Random random = new Random(); //create this once and reuse it in your onAction

r = random.nextInt(5) + 1; //random integer between 1 and 5 (inclusive)[/java]

You also play a sound whether the key is pressed or released (at least twice per press) and you don’t check to see if the last sound is already playing (maybe that’s ok but dangerous if you let too many go).



As it stands, because of the Java problems, every time a key is pressed at least 2 sounds will be played and up to 12 if r is 1. More if you hold the key down, I think… since you will get multiple presses.

why does it go to up 100?

Because i don't want to play a sound EVERY step the player takes.

you have no breaks in your switch statement

Yes, i forgot them, pasted them in, but that seems not to be the problem.

Does it cause any exception?

No there is no exception

You also play a sound whether the key is pressed or released (at least twice per press) and you don’t check to see if the last sound is already playing (maybe that’s ok but dangerous if you let too many go).

Yes, maybe this is the main problem but: how can i "reduce" this?

I try to check out if the sound is playing with: if (scaryAudio.getStatus() != AudioNode.Status.Playing) {
But the screen still freezes...

I tested it several times again and one time it played a sound and froze some seconds after...
I also check out which number "r" is, and the whole application stops when it's the first time one of the mentioned numbers that cause a sound
(I am not sure if that was good english language, hope you understand everything)

Thanks for the fast answers, nice to have such an active community! :D

Hmmm. The scaryAudio is an object, not a class xD. I was sleep last night xD.


Yes, maybe this is the main problem but: how can i “reduce” this?


Javadocs are your best friends. Don't be afraid!



(I am not sure if that was good english language, hope you understand everything)


understand everything :). Nice english :).
1 Like

Well, I now think the problem is definitely not that there are too many sounds called.

Most time the whole Application crashes when the first sound is loaded.



-The sound files exist

-The path is correct

-Sometimes the game plays some sounds before it freezes, so the files are not damaged.

-There is no exception

-The sound files are not loaded too often



Everything seems to be perfect…

But then this happens:

http://i.imgur.com/vusuF.png



I will have a look at the javadoc of AudioNode and onAction, hope i will find something :slight_smile:



But I actually have no idea what’s wrong…

Alright, problem is solved:



Some of the files didn’t work.

I know i I said they would all work, and in fact they all did (could play them all with audacity) but not in the MonkeyEngine, God knows why.

I just sortet the not-working-sounds (half of all) out and got other ones…

So i still don’t know what the problem was, but it wasn’t any you could find in my code…



Thank you all for helping!!!

:slight_smile:

Afaik there are several codecs for ogg files, we only support “vorbis”. Maybe the non working files are encoded with another codec?

Quick suggestion. Instead of:

[java]if (binding.equals(“Left”)) {

if (value) {

left = true;

} else {

left = false;

}

}[/java]



Use:

[java]if (binding.equals(“Left”)) {

left = value;

}[/java]

And so on for the other keypresses.

or just



[java]

left = binding.equals(“Left”) && value;

[/java]







EDIT: It will remove a bunch of lines xD.