2013-10-31 41 views
0

我正在用Objective-C搜索一種方式將float(audioPlayer.currentTime,例如= 3.4592)轉換爲時間字符串,分鐘和秒(03:46 )將浮點數轉換爲時間格式mm:ss

我嘗試這樣做:

static NSDateFormatter *date_formatter = nil; 
if (date_formatter == nil) { 
    date_formatter = [[NSDateFormatter alloc] init]; 
    [date_formatter setDateFormat:@"mm:ss"]; 
} 
NSDate *diff = [[NSDate alloc] initWithTimeIntervalSinceNow:audioPlayer.currentTime]; 
NSLog(@"%@", [date_formatter stringFromDate:diff]); 

...但不工作。格式是正確的,但我找不到正確初始化「差異」日期的方法。

感謝您的幫助!

+0

哪些是audioPlayer.currentTime的單位?分,秒,毫秒? –

回答

1

您可以轉換浮到秒,這樣

double d = CMTimeGetSeconds(__avPlayer.currentItem.duration); 

那麼它轉換到這的NSString呈現以下方法即可以幫助你

- (NSString *) printSecond:(NSInteger) seconds { 

    if (seconds < 60) { 
     return [NSString stringWithFormat:@"00:%02d",seconds]; 
    } 

    if (seconds >= 60) { 

     int minutes = floor(seconds/60); 
     int rseconds = trunc(seconds - minutes * 60); 

     return [NSString stringWithFormat:@"%02d:%02d",minutes,rseconds]; 

    } 

    return @""; 

} 
+1

完美!這完全是我需要的。十分感謝! – rekam