2

durCurrently 我正在使用 iPhone 应用程序,AVAudioPlayer用于在我的应用程序中播放音频文件,然后从AVAudioPlayer.

我得到的持续时间值如下: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);
4

2 回答 2

8

你也可以试试,

    // Say you get the duration from AVAudioPlayer *p;
    NSString *dur = [NSString stringWithFormat:@"%02d:%02d", (int)((int)(p.duration)) / 60, (int)((int)(p.duration)) % 60];

正如@Victor John 评论的那样,在 Swift 中,

    String(format: "%02d:%02d", ((Int)((audioPlayer?.currentTime)!)) / 60, ((Int)((audioPlayer?.currentTime)!)) % 60)
于 2013-01-02T09:26:36.933 回答
1
NSTimeInterval theTimeInterval = audioPlayer.duration;
 // Get the system calendar
NSCalendar *sysCalendar = [NSCalendar currentCalendar];

// Create the NSDates
NSDate *date1 = [[NSDate alloc] init];
NSDate *date2 = [[NSDate alloc] initWithTimeInterval:theTimeInterval sinceDate:date1];
// Get conversion to hours, minutes, seconds
unsigned int unitFlags = NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit;
NSDateComponents *breakdownInfo = [sysCalendar components:unitFlags fromDate:date1  toDate:date2  options:0];
NSLog(@"%02d:%02d:%02d", [breakdownInfo hour], [breakdownInfo minute], [breakdownInfo second]);
[date1 release];
[date2 release];
于 2012-11-07T07:28:51.663 回答