NSDate and AVPlayerItem.currentTime - ios

i know there are a lot of topics, but i canĀ“t find a solution for my problem.
i have an AVPlayerItem and i want the currentTime-property (CMTime) convert to a readable format for my music player.
this is my code:
NSDate *seconds = [[NSDate alloc] initWithTimeIntervalSinceNow:CMTimeGetSeconds(self.playerItem.currentTime)];
NSDateFormatter *timeFormatter = [[NSDateFormatter alloc] init];
[timeFormatter setDateFormat:#"HH:mm:ss"];
self.currentTimeLabel.text = [NSString stringWithFormat:#"%#", [timeFormatter stringFromDate:seconds]];
it works but it adds 1 hour to the played Time. how can i subtract that hour?
thx!

try this code,
swift code:
delclare
var playerVal = AVPlayer()
then call below method where you want,
func updateTime() {
let currentTime = Float(CMTimeGetSeconds(playerVal.currentTime()))
let minutes = currentTime/60
let seconds = currentTime - minutes * 60
startValue.text = NSString(format: "%.2f:%.2f", minutes,seconds) as String
}
objective-C code:
delclare
AVPlayer playerVal;
then call below method where you want,
- (void)updateTime {
Float currentTime = CMTimeGetSeconds([playerVal currentTime]);
Float minutes = currentTime/60;
Float seconds = (currentTime - minutes) * 60;
startValue.text = [NSString stringWithFormat:#"%.2f:%.2f", minutes,seconds];
}
its working for me, hope its helpful

thx, i refined it a bit:
float currentTime = CMTimeGetSeconds([self.audioStreamPlayer currentTime]);
int seconds = (int) currentTime;
int minutes = (int) currentTime/60;
int hours = (int) ((currentTime/60)/60);
NSString *hoursString = [NSString stringWithFormat:#"%d",hours];
NSString *minutesString = [NSString stringWithFormat:#"%d",minutes];
NSString *secondsString = [NSString stringWithFormat:#"%d",seconds % 60];
if (hoursString.length == 1) {
hoursString = [NSString stringWithFormat:#"0%d", hours];
}
if (minutesString.length == 1) {
minutesString = [NSString stringWithFormat:#"0%d",minutes];
}
if (secondsString.length == 1) {
secondsString = [NSString stringWithFormat:#"0%d", seconds % 60];
}
self.currentTimeLabel.text = [NSString stringWithFormat:#"%#:%#:%#", hoursString, minutesString, secondsString];
works!

Related

Convert float 1.88 to string HH:MM

I wish to display HH:MM at a UILabel when I adjust a custom slider. Currently my custom slider is returning me float values, for example, 2.89, 24.87... I wish to take the float value say 24.87 and change it to 24:52 I got everything working at the following code but I think it is not the most efficient way. Can anyone improve it? Thank you
- (void)slideValueChanged:(id)control
{
NSLog(#"Slider value changed: (%.2f,%.2f)",
_rangeSlider.lowerValue, _rangeSlider.upperValue);
lblStart.text = [NSString stringWithFormat:#"Start Time : %#", [self floatToTime:_rangeSlider.lowerValue]];
lblEnd.text = [NSString stringWithFormat:#"End Time : %#",[self floatToTime:_rangeSlider.upperValue]];
}
- (NSString*)floatToTime:(float)floatTime {
NSInteger iHour = floatTime;
CGFloat floatMin = floatTime - iHour;
NSString *sHour = [NSString stringWithFormat:#"%li", (long)iHour];
if (floatMin == 0.99) { //=== When the float is 0.99, convert it to 0, if not 60*0.99 = 59.4, will never get to 0
floatMin = 0;
}else{
floatMin = floatMin * 60;
}
NSInteger iMin = floatMin; //=== Take the integer part of floatMin
NSString *sMin = [[NSString alloc] init];
if (iMin <10){ //=== Take care if 0,1,2,3,4,5,6,7,8,9 to be 00,01,02,03...
sMin = [NSString stringWithFormat: #"0%li", iMin];
}else{
sMin = [NSString stringWithFormat: #"%li", iMin];
}
NSString *strFloatTime = [NSString stringWithFormat:#"%#:%#", sHour,sMin];
return strFloatTime;
}
You can use a format to show two digits, this simplifies creating a time string:
CGFloat time = 24.87;
int hours = fabs(time);
int minutes = (int)((time - hours) * 60.0);
NSLog(#"Time: %02d:%02d", hours, minutes);
Result: "Time: 24:52"
'02' is the number of digits.

Convert String to Float to move UISlider

How I can convert time of my JSON data to float value. Below code is written to convert time to float and I passed it to UISlider as total length of audio. I want to move slider with that time spans
//Json Data
{
duration = "00:03:45";
id = 8;
}
//Audio player sider bar function
if(audioController.playbackState == MPMusicPlaybackStatePlaying){
if (isSelected == YES) {
currentAutio = audioList[selectedAudio];
} else {
currentAutio = audioList[0];
}
NSNumberFormatter *numberFormatter = [[NSNumberFormatter alloc] init];
numberFormatter.numberStyle = NSNumberFormatterDecimalStyle;
float value = [numberFormatter numberFromString:currentAutio.duration].floatValue;
float currentPlaybackTime = [audioController currentPlaybackTime];
float TotalLength = value;
float remainingPlaybackTime = TotalLength - currentPlaybackTime;
float sliderPosition = (currentPlaybackTime *100) / TotalLength;
NSLog(#"current playbacktime %f",currentPlaybackTime);
NSLog(#"TotalLength %f",TotalLength);
NSLog(#"remainingPlaybackTime %f",remainingPlaybackTime);
NSLog(#"sliderPosition %f",sliderPosition);
//Update slider
[progressSlider setValue:sliderPosition];
//Update labels
NSDate* d1 = [NSDate dateWithTimeIntervalSince1970:currentPlaybackTime];
NSDate* d2 = [NSDate dateWithTimeIntervalSince1970:remainingPlaybackTime];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"mm:ss"];
NSString *currentTime = [dateFormatter stringFromDate:d1];
NSString *ramainingTime = [dateFormatter stringFromDate:d2];
[trackCurrentPlaybackTimeLabel setText:currentTime];
[trackLengthLabel setText:[NSString stringWithFormat:#"-%#",ramainingTime]];
}
I am getting following output:
Value = 0.00000
I don't think there is a built-in mechanism for converting a time interval into a float, but it's trivially easy to write:
func timeIntervalFrom(_ timeString: String) -> TimeInterval? {
//Add code to validate that string is in correct format
let components = time.components(separatedBy: ":")
guard components.count == 3,
let hours = Double(components[0]),
let minutes = Double(components[1]),
let seconds = Double(components[2]) else {
return nil
}
return hours * 3600 + minutes * 60 + seconds
}
Just convert the duration from String to integer components.
Then calculate the total duration in seconds.
You can get the integer components simply like this:
Swift
let timeComponents = duration.components(separatedBy: ":")
Objective C
NSArray *timeComponents = [duration componentsSeparatedByString:#":"];
timeComponents[0] will be the hours part.
timeComponents[1] will be the minutes part.
timeComponents[2] will be the seconds part.
Total seconds: (hours * 60 * 60) + (minutes * 60) + seconds.
After that, adjusting the slide bar value will be easy :)

how to seperate '72250' in 72 hr and 250 min in objective c?

Actually I got time interval as 750, so I want split-up in hours and minutes. I was trying but still unable to get. I did following code.
NSTimeInterval interval = 750;
NSDateComponentsFormatter *componentFormatter = [[NSDateComponentsFormatter alloc] init];
componentFormatter.unitsStyle = NSDateComponentsFormatterUnitsStylePositional;
componentFormatter.zeroFormattingBehavior = NSDateComponentsFormatterZeroFormattingBehaviorDropAll;
NSString *formattedString = [componentFormatter stringFromTimeInterval:interval];
NSLog(#"%#",formattedString);
I think this will help you
- (NSString *)timeInteralToString:(NSTimeInterval)interval {
NSInteger ti = (NSInteger)interval;
NSInteger seconds = ti % 60;
NSInteger minutes = (ti / 60) % 60;
NSInteger hours = (ti / 3600);
return [NSString stringWithFormat:#"%02ld:%02ld", (long)hours, (long)minutes];
}
This Works Correctly:
NSString *value1 = #"7570"; //This is Value
NSNumberFormatter *numberFormatter = [[NSNumberFormatter alloc] init];
numberFormatter.usesGroupingSeparator = YES;
[numberFormatter setLocale:[NSLocale localeWithLocaleIdentifier:#" "]];
NSString *formatted = [NSString stringWithFormat: #"%#", [value1 substringWithRange:NSMakeRange(0,2)] ];
NSString *str=[formatted stringByAppendingString:#"hr"];
NSString *formatted2 = [NSString stringWithFormat: #"%#", [value1 substringWithRange:NSMakeRange(2,2)]];
NSString *str2=[formatted2 stringByAppendingString:#"mm"];
NSLog(#"%#",[str stringByAppendingString:str2]);

running timers in tableViewCells being overwritten when tableView scrolls

I have tried asking this question several times but I haven't been able to explain what is going on. Maybe some screen shots may help. I can only post one because I don't have enough reputation points yet.
screenshot after tableview scroll
You can see that one of the timers (2) has been reset. I have tried to fix this multiple ways without success. Here is the code that puts the timers into the tableview cells:
-(void) calculateTimer:(NSTimer *)theTimer
{
self.timerItem = [theTimer userInfo];
// for date only cell
if(self.timerItem.timerType == 0){
[theTimer invalidate];
}
for (NRCItemCell *cell in [self.tableView visibleCells])
{
NSIndexPath *ip = [self.tableView indexPathForCell:cell];
NSUInteger row = [[[NRCItemStore sharedStore]allItems] indexOfObjectIdenticalTo:self.timerItem];
if (row == ip.row){
[self configureTimers:cell forRowAtIndexPath:ip];
cell.timer.text = self.timerItem.timerOutput;
cell.timerName.text = self.timerItem.timerName;
}
}
}
-(void)configureTimers:(NRCItemCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
NRCtimerItem *item = [[NRCItemStore sharedStore]allItems][indexPath.row];
NSInteger timerType = item.timerType;
// timerType set by TimerTypeTableView Controller as follows:
// 0 - date
// 1 - seconds elapsed
// 2 - minutes elapsed
// 3 - hours elapsed
// 4 - days elapsed
// 5 - months elapsed
// 6 - years elapsed
switch (timerType) {
case 0:{
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateStyle:NSDateFormatterMediumStyle];
[dateFormatter setTimeStyle:NSDateFormatterNoStyle];
NSDate *date = [NSDate date];
NSString *formattedDateString = [dateFormatter stringFromDate:date];
item.timerOutput = formattedDateString;
}
break;
case 1:
{
NSTimeInterval interval = [self.timerItem.startTime timeIntervalSinceNow];
interval = (-1 * interval);
int time = round(interval);
div_t h = div(time, 3600); //seconds total, divided by 3600 equals
int hours = h.quot; // hours, divided by 60 equals
div_t m = div(h.rem, 60); // minutes
int minutes = m.quot;
int seconds = m.rem; // and remainder is seconds
// NSLog(#"%d:%d:%d", hours, minutes, seconds);
//NSString *intervalString = [NSString stringWithFormat:#"%ld", (long)time];
NSString *intervalString = [NSString stringWithFormat:#"%d hours, %d minutes, %d seconds", hours, minutes, seconds];
NSString *outputString = [intervalString stringByAppendingString:#" ago"];
item.timerOutput = outputString;
}
break;
case 2:
{
NSTimeInterval interval = [self.timerItem.startTime timeIntervalSinceNow];
interval = (-1 * interval);
int time = roundf(interval);
div_t h = div(time, 3600); // seconds total, divided by 3600 equals
int hours = h.quot; // hours, divided by 60 equals
div_t m = div(h.rem, 60); // minutes
int minutes = m.quot;
NSString *intervalString = [NSString stringWithFormat:#"%d hours, %d minutes", hours, minutes];
NSString *outputString = [intervalString stringByAppendingString:#" ago"];
item.timerOutput = outputString;
}
break;
case 3:
{
NSTimeInterval interval = [self.timerItem.startTime timeIntervalSinceNow];
interval = (-1 * interval);
int time = roundf(interval);
div_t h = div(time, 3600); // seconds total, divided by 3600 equals
int hours = h.quot; // hours
NSString *intervalString = [NSString stringWithFormat:#"%d hours", hours];
NSString *outputString = [intervalString stringByAppendingString:#" ago"];
item.timerOutput = outputString;
}
break;
case 4:
{
NSTimeInterval interval = [self.timerItem.startTime timeIntervalSinceNow];
interval = (-1 * interval);
int time = roundf(interval);
div_t h = div(time, 3600); // seconds total, divided by 3600 equals
int hours = h.quot; // hours, divided by 24 equals
div_t d =div(h.rem, 24); // days
int days = d.quot;
NSString *intervalString = [NSString stringWithFormat:#"%d days, %d hours", days, hours];
NSString *outputString = [intervalString stringByAppendingString:#" ago"];
item.timerOutput = outputString;
}
break;
case 5:
{
NSTimeInterval interval = [self.timerItem.startTime timeIntervalSinceNow];
interval = (-1 * interval);
int time = roundf(interval);
div_t h = div(time, 3600); // seconds total, divided by 3600 equals
__unused int hours = h.quot; // hours, divided by 24 equals
div_t d =div(h.rem, 24); // days
int days = d.quot;
div_t y = div(d.rem, 12);// divided by 12 equals months
int months = y.quot;
NSString *intervalString = [NSString stringWithFormat:#"%d months, %d days", months, days];
NSString *outputString = [intervalString stringByAppendingString:#" ago"];
item.timerOutput = outputString;
}
break;
case 6:
{
NSTimeInterval interval = [self.timerItem.startTime timeIntervalSinceNow];
interval = (-1 * interval);
int time = roundf(interval);
div_t h = div(time, 3600); // seconds total, divided by 3600 equals
__unused int hours = h.quot; // hours, divided by 24 equals
div_t d =div(h.rem, 24); // days
int days = d.quot;
div_t y = div(d.rem, 365);// divided by 365 equals years
int years = y.quot;
NSString *intervalString = [NSString stringWithFormat:#"%d years, %d days", years, days];
NSString *outputString = [intervalString stringByAppendingString:#" ago"];
item.timerOutput = outputString;
}
break;
}
}
The key is the for(NRCItemCell *cell in [self.tableView visibleCells]) fast enumeration. The idea is to loop through the visible cells and only update a cell if the cell's indexPath is equal to the position of the timer in my datastore. However, it looks like scrolling the tableView causes a mismatch between indexPath and and position of the timer in the datastore so that the wrong cell gets overwritten. I have searched all over for an answer and have tried several different approaches but the solution depends on the subtitle label in my custom cell not being overwritten unless the cell position matches the datastore position (which is the way MVC should work, as I understand it). But using reusable cells and scrolling apparently doesn't work the way I thought it did. If there is a solution, I sure would like the help. Thanks in advance!
Your main issue here is the reuse of the cells. Every time you scroll the table the cells are reused with the data of other cells. To keep it short, store your timers data in an array and not in the actual cell.
Importent pointers:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
...
cell.timerView.data = nil; //reset the data in the current timer
myCell *updateCell = (id)[tableView cellForRowAtIndexPath:indexPath];
if (updateCell)// Makes sure the cell is still visible
updateCell.timerView.data = timersArray[indexpath.row];
});
}
My bad. I found the bug in my code. It wasn't with tableView at all. I was storing the timers in an array, correctly, not in the cell, even though that's what the comments in the code said. The bug was that I was inadvertently firing a timer every pass through the tableView, so these timers would fire at unpredictable times and then my code would overlay the cell for the corresponding timer. A lot of debugging work, but good experience. Code is working now.
Thanks for the comments!

iOS Format String into minutes and seconds

I am receiving a string from the YouTube JSONC api, but the duration is coming as a full number i.e 2321 instead of 23:21 or 2 instead of 0:02. How would I go about fixing this?
JSON C
EDIT:
int duration = [videos valueForKey:#"duration"];
int minutes = duration / 60;
int seconds = duration % 60;
NSString *time = [NSString stringWithFormat:#"%d:%02d", minutes, seconds];
Assuming the duration value is really the duration in seconds, then you can calculate the number of minutes and seconds and then format those into a string.
int duration = ... // some duration from the JSON
int minutes = duration / 60;
int seconds = duration % 60;
NSString *time = [NSString stringWithFormat:#"%d:%02d", minutes, seconds];
You should use DateComponentsFormatter if the duration is intended to be user-facing:
let formatter = DateComponentsFormatter()
formatter.allowedUnits = [ .minute, .second ]
formatter.zeroFormattingBehavior = [ .pad ]
let formattedDuration = formatter.string(from: duration)!
Try this very optimized
+ (NSString *)timeFormatConvertToSeconds:(NSString *)timeSecs
{
int totalSeconds=[timeSecs intValue];
int seconds = totalSeconds % 60;
int minutes = (totalSeconds / 60) % 60;
int hours = totalSeconds / 3600;
return [NSString stringWithFormat:#"%02d:%02d:%02d",hours, minutes, seconds];
}
int sec = diff;//INFO: time in seconds
int a_sec = 1;
int a_min = a_sec * 60;
int an_hour = a_min * 60;
int a_day = an_hour * 24;
int a_month = a_day * 30;
int a_year = a_day * 365;
NSString *text = #"";
if (sec >= a_year)
{
int years = floor(sec / a_year);
text = [NSString stringWithFormat:#"%d year%# ", years, years > 0 ? #"s" : #""];
sec = sec - (years * a_year);
}
if (sec >= a_month)
{
int months = floor(sec / a_month);
text = [NSString stringWithFormat:#"%#%d month%# ", text, months, months > 0 ? #"s" : #""];
sec = sec - (months * a_month);
}
if (sec >= a_day)
{
int days = floor(sec / a_day);
text = [NSString stringWithFormat:#"%#%d day%# ", text, days, days > 0 ? #"s" : #""];
sec = sec - (days * a_day);
}
if (sec >= an_hour)
{
int hours = floor(sec / an_hour);
text = [NSString stringWithFormat:#"%#%d hour%# ", text, hours, hours > 0 ? #"s" : #""];
sec = sec - (hours * an_hour);
}
if (sec >= a_min)
{
int minutes = floor(sec / a_min);
text = [NSString stringWithFormat:#"%#%d minute%# ", text, minutes, minutes > 0 ? #"s" : #""];
sec = sec - (minutes * a_min);
}
if (sec >= a_sec)
{
int seconds = floor(sec / a_sec);
text = [NSString stringWithFormat:#"%#%d second%#", text, seconds, seconds > 0 ? #"s" : #""];
}
NSLog(#"<%#>", text);
Here is the great code I finds for this
int duration = 1221;
int minutes = floor(duration/60)
int seconds = round(duration - (minutes * 60))
NSString * timeStr = [NSString stringWithFormat:#"%i:%i",minutes,seconds];
NSLog(#"Dilip timeStr : %#",timeStr);
And the output will belike this
Dilip timeStr : 20:21
You can subString the 2321 and get the first string as 23 and the second as 21 and convert them to int. Also check for the length of the text:
if (text.length < 4)
//add zeros on the left of String until be of length 4
Objective C:
NSDateComponentsFormatter * formatter = [[NSDateComponentsFormatter alloc]init];
[formatter setUnitsStyle:NSDateComponentsFormatterUnitsStyleShort];
[formatter setAllowedUnits:NSCalendarUnitSecond | NSCalendarUnitMinute];
[formatter setZeroFormattingBehavior:NSDateComponentsFormatterZeroFormattingBehaviorPad];
return [formatter stringFromTimeInterval:duration];

Resources