Millisecond timer in Cocos2d - objective-c

I have the following code:
-(void)update:(ccTime)delta {
totalTime += delta * 10;
currentTime = (int)totalTime;
if (myTime < currentTime) {
myTime = currentTime;
[timeLabel setString:[NSString stringWithFormat:#"Time: \n%i:%02i:%02i.%i", myTime/36000,(myTime/600)%60,(myTime/10)%60,myTime%10]];
}
}
This code is working good, is there a way to add milliseconds though? And if so, could I have it to the thousandths place: 1:23:45.678

There's no need to use Cocoas2d for this, just use standard Cocoa.
#property CFTimeInterval startTime; // put this in your *.h file
-(void)update:(ccTime)delta {
if (fabs(self.startTime - 0) < 0.1) { // if startTime is close to 0, set it to the current time (cannot do == comparisons with a CFTimeInterval)
self.startTime = CFAbsoluteTimeGetCurrent();
}
CFTimeInterval timeSinceStart = CFAbsoluteTimeGetCurrent() - self.startTime;
int hours = floor(timeSinceStart / 3600);
int minutes = floor(timeSinceStart / 60) - (hours * 60);
double seconds = timeSinceStart - (hours * 3600) - (minutes * 60);
NSLog(#"%02i:%02i:%06.3f", hours, minutes, seconds);
[timeLabel setString:[NSString stringWithFormat:#"Time: \n%02i:%02i:%06.3f", hours, minutes, seconds]];
}

Related

Objective-C: How Can I Format a float value to Only Display The Decimal, Without The Full Figure, or Decimal Point?

There are MANY Q&A's for limiting to only 2 decimal places, to the point of over saturation.
However, I would like to format my float to only get the decimal value.
I'm making a stopwatch, and currently have this...
#implementation HudLayer
{
CCLabelTTF *_label;
float timer;
}
-(void)update:(ccTime)delta { if (_isTimerActive)
{
timer += delta;
NSNumber *theDouble = [NSNumber numberWithDouble:timer];
float miliseconds = timer;
int inputSeconds = [theDouble intValue];
int hours = inputSeconds / 3600;
int minutes = ( inputSeconds - hours * 3600 ) / 60;
NSString *theTime = [NSString stringWithFormat:#"%.2d:%.2d:%.2f", hours, minutes, miliseconds];
[_label setString:[NSString stringWithFormat:#"Time: %#", theTime]];
}
}
However, the issue I'm having is that the timer reads out put on to the label showing the full seconds...
00:14:865.35
Instead it should just be:
00:14:05.35
HH:MM:SS.ms
My first thought was to just drop the decimal from the outputted float, and manually calculate seconds as i do hours and minutes...
Any advice? Thanks...
Try following
timer += delta;
float float_seconds = timer;
int seconds = float_seconds;
int hours = seconds / 3600;
int minutes = seconds / 60 % 60;
NSString *theTime = [NSString stringWithFormat:#"%.2d:%.2d:%05.2f", hours, minutes, float_seconds - ( seconds / 60) * 60];
NSLog(theTime);
[_label setString:[NSString stringWithFormat:#"Time: %#", theTime]];

Getting Video Duration from ALAsset

Hi I'm new to objective C. I'm using AGImagePicker to pick images and video files from phone library and displaying them in GridView. but now I want to get duration of video file to display it with video thumbnail. I don't know how to get video duration from ALAsset. Anybody Please. Any type of answer would be appreciated.
1)To get the Media Duration with the Help of ALAssetPropertyDuration
double value = [[asset valueForProperty:ALAssetPropertyDuration] doubleValue]; //Find the Duraion
[cell.lblMediaLength setText:[self timeFormatted:value]];
2)Method that convert the Total seconds to Hour:min:sec
- (NSString *)timeFormatted:(double)totalSeconds
{
NSTimeInterval timeInterval = totalSeconds;
long seconds = lroundf(timeInterval); // Modulo (%) operator below needs int or long
int hour = 0;
int minute = seconds/60.0f;
int second = seconds % 60;
if (minute > 59) {
hour = minute/60;
minute = minute%60;
return [NSString stringWithFormat:#"%02d:%02d:%02d", hour, minute, second];
}
else{
return [NSString stringWithFormat:#"%02d:%02d", minute, second];
}
}
For Swift
func timeFormatted(totalSeconds: Float64) -> String {
let timeInterval: NSTimeInterval = totalSeconds
let seconds: Int = lround(timeInterval)
var hour: Int = 0
var minute: Int = Int(seconds/60)
let second: Int = seconds % 60
if minute > 59 {
hour = minute / 60
minute = minute % 60
return String(format: "%02d:%02d:%02d", hour, minute, second)
}
else {
return String(format: "%02d:%02d", minute, second)
}
}
If asset is an ALAsset:
NSNumber *number = [asset valueForProperty:ALAssetPropertyDuration];
This will return an NSNumber. If you want the value:
double value = [[asset valueForProperty:ALAssetPropertyDuration] doubleValue];
Here is how I get the duration.
AVAsset *testAsset = [AVAsset assetWithURL:url];
return testAsset.duration;

Error with timer mm:ss

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;
}

CMTime seconds output

This may seem ridiculous, but how can I output the seconds of CMTime to the console in Objective-C? I simply need the value divided by the timescale and then somehow see it in the console.
NSLog(#"seconds = %f", CMTimeGetSeconds(cmTime));
Simple:
NSLog(#"%lld", time.value/time.timescale);
If you want to convert in hh:mm:ss format then you can use this
NSUInteger durationSeconds = (long)CMTimeGetSeconds(audioDuration);
NSUInteger hours = floor(dTotalSeconds / 3600);
NSUInteger minutes = floor(durationSeconds % 3600 / 60);
NSUInteger seconds = floor(durationSeconds % 3600 % 60);
NSString *time = [NSString stringWithFormat:#"%02ld:%02ld:%02ld", hours, minutes, seconds];
NSLog(#"Time|%#", time);
All answers before this one do not handle NaN case:
Swift 5:
/// Convert CMTime to TimeInterval
///
/// - Parameter time: CMTime
/// - Returns: TimeInterval
func cmTimeToSeconds(_ time: CMTime) -> TimeInterval? {
let seconds = CMTimeGetSeconds(time)
if seconds.isNaN {
return nil
}
return TimeInterval(seconds)
}
If you just want to print a CMTime to the console for debugging purposes use CMTimeShow:
Objective-C
CMTime time = CMTimeMakeWithSeconds(2.0, 60000);
CMTimeShow(time);
Swift
var time = CMTimeMakeWithSeconds(2.0, 60000)
CMTimeShow(time)
It will print the value, timescale and calculate the seconds:
{120000/6000 = 2.0}
CMTime currentTime = audioPlayer.currentItem.currentTime;
float videoDurationSeconds = CMTimeGetSeconds(currentTime);

unused variable warning when variable is actually being used

I am given a warning in Xcode that a variable I am using, is not being used:
- (void)timerTicked:(id)sender {
[timerButton setTitle:[self timerIsActive] ? #"Stop timer" : #"Start Timer" forState:UIControlStateNormal];
if([self timerIsActive]) {
NSTimeInterval interval = [[NSDate date] timeIntervalSinceDate:timeEntry.startDate];
double seconds = (int) interval % 60;
double minutes = (int) interval / 60.0;
double hours = (int) interval / 60.0 / 60.0;
[timerLabel setText:[NSString stringWithFormat:#"%.0f hours %.0f minutes %.0f seconds", hours, minutes, seconds]];
}
}
The variable reported not being used is interval.
Why is interval being reported as unused?
It works fine for me. Try to clean the code and Analyze using shift commond B. -- AV