oOOMIAO 2012-11-07 09:10 采纳率: 10%
浏览 6700
已采纳

如何从 AVAudioPlayer中获取duration 持续时间 (HH:MM:SS)

在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);
  • 写回答

1条回答

  • vikeyToy 2012-11-07 09:27
    关注

    试试看:

    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];
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘