How to do to display current time of a video ? I am developing in Obj-C and I am using QTKit framework. I am wondering how to do for that QTMovieView notify continually my function "refreshCurrentTimeTextField". I found an Apple sample but it turned to be quite difficult to extract what I really need. (http://developer.apple.com/library/mac/#samplecode/QTKitMovieShuffler/Introduction/Intro.html)
Create an NSTimer that updates once every second:
[NSTimer scheduledTimerWithInterval:1.0 target:self selector:#selector(refreshCurrentTimeTextField) userInfo:nil repeats:YES]
And then create your timer callback function and turn the time into a proper HH:MM:SS label:
-(void)refreshCurrentTimeTextField {
NSTimeInterval currentTime;
QTMovie *movie = [movieView movie];
QTGetTimeInterval([movie currentTime], ¤tTime);
int hours = currentTime/3600;
int minutes = (currentTime/60) % 60;
int seconds = currentTime % 60;
NSString *timeLabel;
if(hours > 0) {
timeLabel = [NSString stringWithFormat:#"%02i:%02i:%02i", hours, minutes, seconds];
} else {
timeLabel = [NSString stringWithFormat:#"%02i:%02i", minutes, seconds];
}
[yourTextField setStringValue:timeLabel];
}
Related
I made a timer, but if I lock the screen, the timer stops ... how can I make it work when I block?
-(void)timeRun{
secondsCount = secondsCount - 1;
int minuts = secondsCount / 60;
int seconds = secondsCount - (minuts * 60);
NSString *timerOutput = [NSString stringWithFormat:#"%2d:%.2d", minuts, seconds];
TimerDisplay.text = timerOutput;
if (secondsCount == 0) {
[countdownTimer invalidate];
countdownTimer = nil;
AudioServicesPlaySystemSound(PlaySoundID);
}
}
Start the timer here
-(IBAction)Start:(id)sender{
secondsCount = perVrem;
countdownTimer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:#selector(timeRun) userInfo:nil repeats:YES];
}
Here I set the time 5 minutes
-(IBAction)fivemin:(id)sender{perVrem = 300;
TimerDisplay.text = [NSString stringWithFormat:#"05:00"];}
Difficult to answer without more context, but you may need
UIApplication.sharedApplication.idleTimerDisabled = NO;
or maybe you need to use
dispatch_after
or maybe you need to use the wall clock and just use your timer to update some UI.
Yours look like the latter, ie. use the wall clock and your timer just to update. If you count down 5 minutes accurately you'd have a start and end time and use a timer just to update the UI every now and again. So start becomes something like
-(IBAction)Start:(id)sender{
self.start = NSDate.date.timeIntervalSinceReferenceDate;
countdownTimer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:#selector(timeRun) userInfo:nil repeats:YES];
}
and timeRun something like
-(void)timeRun{
secondsCount = (x)( NSDate.date.timeIntervalSinceReferenceDate - self.start + 0.5 ); // Add a bit to get rounding right, depends on your types
...
where x will be e.g. int or long depending on secondsCount.
I'm new to objective C.
i've been playing around with a countdown for some time now and i cant seem to make it work.
i've connecting very thing in storyboard and the buttons react, but it seems like its just randomly counting down.
why i isnt it counting down like 10:00 to 09:59.
- (void)showActivity{
int currentTime = [time.text intValue];
int newTime = currentTime - 1;
int seconds = newTime % 60;
int minutes = (newTime / 60) % 60;
time.text = [NSString stringWithFormat:#"%02d:%02d", minutes, seconds];
}
If time.text is "10:00", then calling [time.text intValue] is just going to return the integer 10.
I'd recommend creating a separate variable (perhaps an NSTimeInterval) that keeps track of how many seconds are left, and then make the time text label just responsible for displaying that as minutes:seconds.
e.g.
#property NSTimeInterval time;
#property UILabel *timeLabel; // "time" in your original code
- (void)showActivity {
NSTimeInterval newTime = self.time - 1;
int minutes = floor(newTime / 60);
int seconds = round(newTime - (minutes * 60));
self.timeLabel.text = [NSString stringWithFormat:#"%02d:%02d", minutes, seconds];
self.time = newTime;
}
I've created a countdown timer and it works great.. but when it reaches zero it continues counting down.. so it shows -1, -2, -3 etc. How do I prevent it from doing this?
This is my code from the implementation file..
#implementation ViewController
-(IBAction)start {
myTicker = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:#selector(showActivity) userInfo:nil repeats:YES];
}
-(IBAction)stop {
[myTicker invalidate];
}
-(IBAction)reset {
time.text = #"0";
}
-(void)showActivity {
int currentTime = [time.text intValue];
int newTime = currentTime -1;
time.text = [NSString stringWithFormat:#"%d", newTime];
}
I'm guessing that I'm missing some code somewhere?
Quick fix is to change:
int newTime = currentTime - 1;
into:
int newTime = (currentTime > 0) ? currentTime - 1 : 0;
That will stop the countdown timer from going below zero. If that's all you need, that should suffice.
It won't turn off the timer or execute an action when it reaches zero. For that, you'll need to add something like the following block after that line above:
if ((currentTime > 0) && (newTime ==0)) {
// optionally turn off timer
fireZeroEvent ();
}
and provide the fireZeroEvent() function to do whatever you need to do.
I say optionally turn off the timer since you may want to leave it running so you can restart the countdown by just be setting time.text without have to recreate the timer.
For that reason, the fireZeroEvent() is only called on the transition from 1 to 0, not every time the timer fires after it reaches zero.
I have compiled the following code and there are no apparent runtime errors; however, the display freezes at 00:00:01 when I run it. It works if I only display the seconds attribute. Does anyone see an apparent oversight that I have missed in this code? I know there is a potential memory leak with the start button, but I will fix that eventually.
Thanks in advance.
#import "StopwatchViewController.h"
#implementation StopwatchViewController
- (IBAction)start{
//creates and fires timer every second
myTimer = [[NSTimer scheduledTimerWithTimeInterval:1 target:self selector:#selector(showTime) userInfo:nil repeats:YES]retain];
}
- (IBAction)stop{
[myTimer invalidate];
myTimer = nil;
}
- (IBAction)reset{
[myTimer invalidate];
time.text = #"00:00:00";
}
(void)showTime{
int currentTime = [time.text intValue];
int new = currentTime +1;
int secs = new;
int mins = (secs/60) % 60;
int hours = (mins/60);
time.text = [NSString stringWithFormat:#"%.2d:%.2d:%.2d",hours, mins, secs];
}
You're getting 0 from
int currentTime = [time.text intValue];
because the string that's in text:
#"00:00:00"
can't be converted to an int, so every time the timer fires, you add 1 to 0 and get 1, which you then display. The math would be inaccurate anyways, because minutes and seconds are "base-60"* -- you'd need to do the reverse of the math you perform for separating hours/minutes/seconds, in order to get the total seconds again. You could just make currentTime an ivar, and keep the total number of seconds in it.
*That's not really what it's called; I'm sure there's a specific word for it.
- (IBAction)start{
currentTime = 0;
//creates and fires timer every second
myTimer = [[NSTimer scheduledTimerWithTimeInterval:1 target:self selector:#selector(showTime) userInfo:nil repeats:YES]retain];
}
- (IBAction)stop{
[myTimer invalidate];
myTimer = nil;
}
- (IBAction)reset{
[myTimer invalidate];
time.text = #"00:00:00";
}
- (void)showTime{
currentTime++;
int secs = currentTime % 60;
int mins = (currentTime / 60) % 60;
int hour = (currentTime / 3600);
time.text = [NSString stringWithFormat:#"%.2d:%.2d:%.2d",hour, mins, secs];
}
i need a little help i have a method which gets value such as 50, it then assigns that value to trackDuration, so NSNumber *trackDuration = 50, i want the method to every second minus 1 from the value of trackDuration and update a label, the label being called duration.
Here's what i have so far;
- (void) countDown {
iTunesApplication *iTunes = [SBApplication applicationWithBundleIdentifier:#"com.apple.iTunes"];
NSNumber *trackDuration = [NSNumber numberWithDouble:[[iTunes currentTrack] duration]];
while (trackDuration > 0) {
trackDuration - 1;
int inputSeconds = [trackDuration intValue];
int hours = inputSeconds / 3600;
int minutes = ( inputSeconds - hours * 3600 ) / 60;
int seconds = inputSeconds - hours * 3600 - minutes * 60;
NSString *trackDurationString = [NSString stringWithFormat:#"%.2d:%.2d:%.2d", hours, minutes, seconds];
[duration setStringValue:trackDurationString];
sleep(1);
}}
Any help would be much appreciated, thanks in advanced, Sami.
This will block the main thread, and you are not assigning the value trackDuration, so it will always stay 50
trackDuration -1;
Should be:
trackDuration--; // or trackDuration -= 1;
Also I would do it like this:
- (void)startCountDown
{
[NSTimer scheduledTimerWithInterval:1.0f target:self selector:#selector(timerHit:) userInfo:nil repeats:YES];
}
- (void)timerHit:(NSTimer *)p_timer
{
if( trackDuration <= 1 && [p_timer isValid] )
[p_timer invalidate];
// track duration is an instance variable
trackDuration--;
// update LABEL
}
iOS 2.x or higher is required for NSTimer
Having this method run in a loop will make your app go unresponsive for the whole time it's running — you won't even see any UI updates. Instead, you should use an NSTimer with an interval of one second, and update the elapsed time when the timer fires.