2

dur現在、私は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];

@ビクタージョンがコメントしたように、スウィフトでは、

    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 に答える