Quote:
Originally Posted by ChrisYates
As you can see from the following code I have a repeat set on an NSTimer, is there anyway for me to stop the repeat or somehow stop the fireMethod1 by the user tapping a button?
Code:
[NSTimer scheduledTimerWithTimeInterval:10.0 target:self selector:@selector(fireMethod1) userInfo:nil repeats:YES];
The FireMethod1 code is:
Code:
NSString *path = [[NSBundle mainBundle] pathForResource:@"StaticSound1" ofType:@"mp3"];
AVAudioPlayer* theAudio=[[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL];
self.staticSounds = theAudio;
[staticSounds play];
[theAudio release];
|
The way I generally do it is to declare my timer in .h
myTimer = [NSTimer scheduledTimer etc etc....
To stop it I do
Code:
[myTimer invalidate];
myTimer = nil;
If it crashes at times...do an IF stmt to check if the timer is nil...
Code:
if(myTimer != nil)
{
[myTimer invalidate];
myTimer = nil;
}
Stopping the Timer once it has been scheduled to fire, may not work this way..it will complete that last cycle as it is set and forget...So it will stop the Timer from repeating over and over, but that last cycle that is scheduled will still fire...and then it will stop. That is how I understand it. In other words, it will stop it, but not like a knife cutting through butter unless you are timing it in hundredths of a second.
Hope that helps...
You can also use a int counter to limit the number of times you want it to fire...each time it fires it increments by one until it reaches the number you need to stop the timer.
To stop a sound playing...
Code:
[staticSounds stop];
staticSounds = nil;
Again, use in IF stmt to test for nil.