Conditional beep sound not working

220 Views Asked by At

I am writing a code for an app that will "beep" when the current speed is more than the user set warning speed limit. The code shown below is written inside onLocationChanged(), but for some logical reason, it beeps only once and then stops, which tells me that it goes through the loop once and after which the pastTime and curTime loses track and the if condition is not logically true after and hence skipping the loop. The reason I wanted a delay of 5 seconds is to have enough time delay between the beeps and not have them overlap. I intialized pasTime with 0 at the very beginning of the activity. Any suggestion on a fix for this is appreciated. Also curTime = c.getTimeInMillis() each time there is a location change in the location listener.

if (Activity2.mySpeedmph > mySpeed_max & curTime > pastTime+5000)
{
    player = MediaPlayer.create(Activity2.this, R.raw.beep);  
    player.setLooping(false);     
    player.setVolume(100,100);         
    player.start();    
    pastTime = curTime;
}

This is a very specific problem and I couldn't find anything related to this. I know its a very simple issue for an expert.

1

There are 1 best solutions below

1
On

Not meaning to be condescending here (this answer sounds like your classic IT helpdesk "Have you tried turning it off and on again?"), just covering all bases.

Are you updating curTime? What you're describing is what you'd see if pastTime was originally set far in the past and curTime is set once outside the loop and not updated.

Failing that, you probably need to take some samples of the variables involved in that if statement (before the if) to see what values they're set to.

Or, force one of the conditions to be true beforehand (such as with Activity.mySpeedmph = mySpeed_max + 1; so as to check the operation of the other condition.