Sound won't play

i have a function that takes the position of the mouse and changes the sound that plays depending on where it is. However, it is weird because the function works fine if I move it from the bottom of my screen up but not the opposite way. So when I move the mouse downwards, there is nothing playing. I tried doing it the way it is below and also removing all the “.stop()” functions and I get the same result. I don’t know how to fix this so any help would be great!

if(position.y > 100 && position.y < 200){
            sound1.play();
        } else if(position.y > 201 && position.y < 300){
            sound1.stop();
            sound2.play();
        }else if(position.y > 301 && position.y < 400){
            sound1.stop();
            sound2.stop();
            sound3.play();
        }else if(position.y > 401 && position.y < 500){
            sound1.stop();
            sound2.stop();
            sound3.stop();
            sound4.play();
        }else if(position.y > 501 && position.y < 600){
            sound1.stop();
            sound2.stop();
            sound3.stop();
            sound4.stop();
            sound5.play();
        }else if (position.y > 601 && position.y < 700){
            sound1.stop();
            sound2.stop();
            sound3.stop();
            sound4.stop();
            sound5.stop();
            sound6.play();
    }

Hi!

The ofSoundPlayer::play() is meant to be used as a trigger. So, since you are continuously calling it, you are continuously reseting the position of the audio track.

It does plays sound when you move from bottom to top, because when you jump from one condition to another, you never stop the last sound. Example, when you move from the last if statement to the previous, you never stop the sound6.

There are lot of ways to do want you want. Like checking if its playing or changing the volume. Check the documentation for ofSoundPlayer for more info.

Also, you are already playing around with 6 sounds. What happens if you want even more? :slight_smile: You’ll need a vector. Check this intro to vectors on the ofBook. If you have any questions regarding vectors, please create a new topic.

Have fun!