Welcome to NexusFi: the best trading community on the planet, with over 150,000 members Sign Up Now for Free
Genuine reviews from real traders, not fake reviews from stealth vendors
Quality education from leading professional traders
We are a friendly, helpful, and positive community
We do not tolerate rude behavior, trolling, or vendors advertising in posts
We are here to help, just let us know what you need
You'll need to register in order to view the content of the threads and start contributing to our community. It's free for basic access, or support us by becoming an Elite Member -- see if you qualify for a discount below.
-- Big Mike, Site Administrator
(If you already have an account, login at the top of the page)
How do you play a sound only once while a condition is true over multiple bars?
Greetings!
Can anyone explain how to play a sound file once while x is true. Not once per bar, just once until x is false, then the sound file would play only the first time x is true again. No matter how many bars x is true, it only plays a sound the very first time in a sequence where x is true.
I tried this below but it still sounds once per bar, not just once, while x (a rising stoch indicator) is true over multiple bars.
In this kind of situation I create another boolean that flips when the sound has been played.
For example:
private bool SoundPlayed = false;
if (CONDITIONS ARE TRUE
&& SoundPlayed == false)
{
PlaySound;
SoundPlayed = true;
)
Then, when CONDITIONS are no longer true:
SoundPlayed = false;
This may require another if() statement if there are other things you want to happen independent of whether the sound was played or not, but the above concept will do the job. Just make sure to flip SoundPlayed back to false at the appropriate time.