在iPhone應用裡用到AVAudioPlayer
播放音頻文件。然後從AVAudioPlayer 中獲取持續時間
我獲得的duration值是這樣的:252.765442。
但是我想要的是這種格式: HH/MM/SS 02:52:76
如何轉換?請幫忙
我寫出來的代碼沒有用:
audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:tempFilePath] error:&err];
[audioPlayer setDelegate:self];
[audioPlayer prepareToPlay];
float duration = (float)audioPlayer.duration;
NSLog(@"duration:%f",duration);
試試看:
NSTimeInterval theTimeInterval = audioPlayer.duration;
// 獲取system calendar
NSCalendar *sysCalendar = [NSCalendar currentCalendar];
// 創建NSDates
NSDate *date1 = [[NSDate alloc] init];
NSDate *date2 = [[NSDate alloc] initWithTimeInterval:theTimeInterval sinceDate:date1];
// 轉換到小時、分鐘
unsigned int unitFlags = NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit;
NSDateComponents *breakdownInfo = [sysCalendar components:unitFlags fromDate:date1 toDate:date2 options:0];
NSLog(@"Duration: HH/MM/SS %d:%d:%d", [breakdownInfo hour], [breakdownInfo minute], [breakdownInfo second]);
[date1 release];
[date2 release];