gpt4 book ai didi

ios - 获取音频持续时间字符串到浮点值

转载 作者:塔克拉玛干 更新时间:2023-11-02 10:26:10 26 4
gpt4 key购买 nike

{
duration = "00:06:29";
id = 7;
}

NSNumberFormatter *numberFormatter = [[NSNumberFormatter alloc] init];
numberFormatter.numberStyle = NSNumberFormatterDecimalStyle;

float value = [numberFormatter numberFromString:currentAutio.duration].floatValue;
NSLog(@"Current audio %f",value);

out 是当前音频 0.0000

如何将持续时间字符串转换为浮点值?

最佳答案

最简单的方法是

NSString *duration = @"00:06:29";
NSArray *array = [duration componentsSeparatedByString:@":"];

NSLog(@"Current hrs %ld",(long)[(NSString *)(array[0]) integerValue]);
NSLog(@"Current min %ld",(long)[(NSString *)(array[1]) integerValue]);
NSLog(@"Current sec %ld",(long)[(NSString *)(array[2]) integerValue]);

否则,您也可以使用 NSDateNSDateFormatter

编辑


使用 NSDate,请注意小时部分不应超过 23,否则结果可能与预期不符。

NSString *duration = @"00:06:29";

NSDateFormatter *formatter = [NSDateFormatter new];
formatter.dateFormat = @"HH:mm:ss";

NSDate *date = [formatter dateFromString:duration];

NSCalendar *calendar = [NSCalendar currentCalendar];
NSDateComponents *components = [calendar components:NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond fromDate:date];
NSLog(@"hrs %ld", (long)components.hour);
NSLog(@"min %ld", (long)components.minute);
NSLog(@"sec %ld", (long)components.second);

关于ios - 获取音频持续时间字符串到浮点值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41329321/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com