I'm trying to write a simple loop sound Alerts every five seconds
How can i add it please
tweak for cydia
Tweak.xm template
if (lowenabled) {
if ([batString isEqualToString:lowbatPercent_String]) {
if (lowplayer) {
return;
}
if (lowsound == 0)
soundFilePath = [NSString stringWithFormat:#"%#/Super Mario.mp3", bundlePrefix];
else if (lowsound == 1)
soundFilePath = [NSString stringWithFormat:#"%#/Super Mario 1.mp3", bundlePrefix];
soundFileURL = [NSURL fileURLWithPath:soundFilePath];
lowplayer = [[AVAudioPlayer alloc] initWithContentsOfURL:soundFileURL error:nil];
lowplayer.numberOfLoops = 3;
[lowplayer play];
} else {
if (lowplayer) {
lowplayer = nil;
}
}
}
}
%end
Include to fire timer:
NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:5.0 target:self selector:#selector(playSound:) userInfo:nil repeats:YES];
[timer fire];
Put method in the same class:
- (void)playSound {
// the code you included here that plays the sound
}
Docs for NSTimer's fire method
Related
Hello I recently added a timer in my app so when the user clicks the balloon it starts the timer but I also have a tap counter hooked to the button as well. So heres my issue when I press the balloon multiple times the tap counter works and so does the timer but when start to tap faster the timer becomes stuck in a loop and when I stop tapping it, it counts down past zero into negatives really fast
How do I fix this?
- (IBAction)yourbuttonClicked1:(UIButton *)sender
{
//start a background sound
NSString *soundFilePath = [[NSBundle mainBundle] pathForResource:#"tap" ofType:
#"wav"];
NSURL *fileURL = [[NSURL alloc] initFileURLWithPath:soundFilePath ];
myAudioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:fileURL error:nil ];
[myAudioPlayer play];
//for Switch view
ViewController2 *nextView = [[ViewController2 alloc] initWithNibName:#"ViewController"
bundle: nil];
[self.navigationController pushViewController:nextView animated:YES];
This is where the timer code is I think it might be conflicting with the tap counter?
myTime = 60;
countdownTimer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self
selector:#selector(countDown) userInfo:nil repeats:YES];
}
-(void)countDown {
myTime = myTime;
countdownLabel.text = [NSString stringWithFormat:#"%i", myTime];
if (myTime == 0) {
[countdownTimer invalidate];
}
}
This is where the timer code ends!
- (void) saveNumberOfTaps:(int)numberOfTaps {
[[NSUserDefaults standardUserDefaults] setValue:[NSNumber numberWithInt:numberOfTaps]
forKey:#"numberOfTaps"];
NSLog(#"Saved numberOfTaps: %i", numberOfTaps);
}
- (int) getNumberOfTaps {
if ([[[NSUserDefaults standardUserDefaults] objectForKey:#"numberOfTaps"] intValue]) {
int numberOfTaps = [[[NSUserDefaults standardUserDefaults]
objectForKey:#"numberOfTaps"] intValue];
NSLog(#"Getting numberOfTaps: %i", numberOfTaps);
return numberOfTaps;
}
else {
NSLog(#"No taps saved yet");
return 0;
}
}
Try to modify your timer initialization in such way:
myTime = 60;
[countdownTimer invalidate];
countdownTimer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self
selector:#selector(countDown) userInfo:nil repeats:YES];
[[NSRunLoop currentRunLoop] addTimer:countdownTimer forMode:NSRunLoopCommonModes];
Check your -countDown method:
myTime = myTime;
Is it typo?
I'm trying to display the count of a timer in a UILabel. The timer works fine but I'm not able to update the label. The NSTimer is set up like this in Class1.
- (void)startTimer {
int timerCount = 5;
timer = [NSTimer scheduledTimerWithTimeInterval:4.0
target:self
selector:#selector(timerDecrement)
userInfo:nil
repeats:YES];
[timer fire];
}
- (void)timerDecrement {
Class2 *cls2 = [[Class2 alloc] init];
timerCount = timerCount-1;
[cls2 updateTimeLeftLabel];
}
+ (int)getTimerCount {
return timerCount;
}
In Class2:
- (void)viewDidLoad {
if (timeLeftLabel == nil) {
timeLeftLabel = [[UILabel alloc] initWithFrame:CGRectMake(200, 70, 120, 30)];
timeLeftLabel.text = #"Time left";
[self.view addSubview:timeLeftLabel];
}
- (void)updateTimeLeftLabel {
NSLog(#"%#", [NSString stringWithFormat:#"%i",
[Class1 getTimerCount]]);
timeLeftLabel.text = [NSString stringWithFormat:#"Time left: %i",
[Class1 getTimerCount]];
}
The correct timer value is logged but the UILabel isn't updated at all. What's wrong?
Try to make timerCount as a #property of your class or static.
You create a new instance of the Class2 and do not present it. You should store instance of Class2 somewhere and work with it.
I needed to update count on my UIButton so i have done like this
-(void)viewDidLoad {
self.myLeagueTimer = [NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:#selector(updateCountdown) userInfo:nil repeats: YES];
}
- (int)timeIndervalRemain {
NSDate *currentTime = [NSDate date];
return (TIME_INTERVAL - [currentTime timeIntervalSinceDate:self.startTime]);
}
-(void) updateCountdown {
if ([self timeIndervalRemain] > 0) {
self.title = [NSString stringWithFormat:#"%d", [self timeIndervalRemain]];
} else {
[self timeExpire];
}
}
-(void) timeExpire {
if (_myLeagueTimer) {
[_myLeagueTimer invalidate];
_myLeagueTimer = nil;
}
}
You can simply pass the time left to the method updating the UILabel as a parameter:
- (void)updateTimeLeftLabel:(NSInteger)timeLeft {
timeLeftLabel.text = [NSString stringWithFormat:#"Time left: %i",
timeLeft];
}
- (void)timerDecrement {
Class2 *cls2 = [[Class2 alloc] init];
timerCount = timerCount-1;
[cls2 updateTimeLeftLabel:timerCount];
}
This question already has an answer here:
Closed 10 years ago.
Possible Duplicate:
How to call function in every “X” minute?
How to periodically call a certain function?
- (void)viewDidLoad
{
[super viewDidLoad];
[self checkAlert];
}
-(void)checkAlert{
// Server output Alert Note
NSString *alert_note_hostStr = #"http://www.loxleyintranet.com/MobileApplication/xml/alert_note.php";
NSData *alert_note_dataURL = [NSData dataWithContentsOfURL:[NSURL URLWithString:alert_note_hostStr]];
NSString *alert_note_serverOutput = [[NSString alloc] initWithData:alert_note_dataURL encoding:NSASCIIStringEncoding];
if ([alert_note_serverOutput isEqualToString:#"0"]) {
alertImage.hidden = YES;
alertLabel.hidden = YES;
underMainBG.hidden = YES;
alertLabel.text = [NSString stringWithFormat:#"No Alert"];
}else{
alertLabel.text = [NSString stringWithFormat:#"You've Got Note (%#)" ,alert_note_serverOutput];
}
}
How can I call [self checkAlert]; every x minutes or seconds?
Use [NSTimer scheduledTimerWithTimeInterval:60.0 target:self
selector:#selector(checkAlert) userInfo:nil repeats:YES];.
[NSTimer scheduledTimerWithTimeInterval:1 target:self selector:#selector(myTimerTick:) userInfo:nil repeats:YES]; // the interval is in seconds...
...then your myTimerTick: method should look like this..
-(void)myTimerTick:(NSTimer *)timer
{
if(some_contiditon)
{
// Do stuff...
}
else
{
[timer invalidate]; //to stop and invalidate the timer.
}
}
Hi I'm trying to use NSTimer to create a countdown which and view this using a label. the problem is the label goes down in two's and i don't know why. any ideas?
heres my code
- (void)viewDidLoad
{
NSLog(#"%#", self.chosenTime);
[self startGame];
[super viewDidLoad];
NSString *timeString = self.chosenTime;
self.timer = [timeString intValue];
self.countdown.text = timeString;
// Do any additional setup after loading the view.
}
- (void)startGame
{
[self introCountdown];
[self performSelector:#selector(goAnimation) withObject:nil afterDelay:3.0];
NSString *timeString = self.chosenTime;
self.timer = [timeString intValue];
[self performSelector:#selector(startCountdown) withObject:nil afterDelay:4.0];
}
- (void)startCountdown
{
//self.countdownTimer = [NSTimer timerWithTimeInterval:1 target:self selector:#selector(decrementTimer:) userInfo:nil repeats:YES];
self.countdownTimer = [NSTimer scheduledTimerWithTimeInterval:1.00 target:self selector:#selector(decrementTimer:) userInfo:nil repeats:YES];
}
- (void)decrementTimer:(NSTimer *)timer
{
if(self.timer > 0)
{
self.timer = self.timer - 1;
self.countdown.text = [NSString stringWithFormat:#"%d", self.timer];
}
else
{
[self.countdownTimer invalidate];
self.countdownTimer = nil;
}
}
This code seems to be correct. May be you are changing label's text from somewhere else?
Sorry for the misinterpretation, the code is correct, it seems a UI problem or handling label from somewhere else, attach your source with xib's if possible
I am using NSTimer to make a stopwatch. I would like it to continue working if the user switches to a different app, but right now it only works when the app is running. I figure that this is probably pretty simple to fix by recording a timestamp when the timer starts, but I'm not really sure how to reconcile that with the ability to pause/restart the timer in the middle.
My code:
-(IBAction)start;
{
if(sharedInstance.timer == nil){
sharedInstance.timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:#selector(showActivity) userInfo:nil repeats:YES];
sharedInstance.timerRunning = YES;
}
}
-(IBAction)stop;
{
[sharedInstance.timer invalidate];
sharedInstance.timer = nil;
sharedInstance.timerRunning = NO;
}
-(void)showActivity;
{
sharedInstance.elapsedTime += 1;
int hours = sharedInstance.elapsedTime / 3600 ;
int minutes = sharedInstance.elapsedTime / 60 - hours * 60;
int seconds = sharedInstance.elapsedTime - hours * 3600 - minutes * 60;
sharedInstance.timeString = [NSString stringWithFormat:#"%02d:%02d:%02d", hours, minutes, seconds];
self.readout.text = sharedInstance.timeString;
}
-(void)viewDidLoad {
sharedInstance = [DataSingleton sharedInstance];
if (sharedInstance.timeString==nil) {
sharedInstance.timeString=#"00:00:00";
}
self.readout.text = sharedInstance.timeString;
[sharedInstance.timer invalidate];
sharedInstance.timer = nil;
if(sharedInstance.timerRunning) {
[self start];
}
[super viewDidLoad];
}
-(void)reset {
[self stop];
sharedInstance.elapsedTime = 0;
sharedInstance.timeString = #"00:00:00";
self.readout.text = sharedInstance.timeString;
}
Every time you call showActivity you could get a new time stamp and subtract the old one from it getting the change in time. Then add that to elapsedTime instead of 1.