【问题标题】:AVPlayer - Add Seconds to CMTimeAVPlayer - 将秒数添加到 CMTime
【发布时间】:2013-02-12 10:17:08
【问题描述】:

如何将当前播放时间增加 5 秒?
其实这是我的代码:

CMTime currentTime = music.currentTime;

我不能使用 CMTimeGetSeconds() ,因为我需要 CMTime 格式。

感谢您的回答...

编辑:如何为 CMTime 设置变量?

【问题讨论】:

    标签: iphone ios xcode avplayer cmtime


    【解决方案1】:

    这是一种方法:

    CMTimeMakeWithSeconds(CMTimeGetSeconds(music.currentTime) + 5, music.currentTime.timescale);
    

    【讨论】:

    • 当我将 '+ 5' 替换为 '- 5' 时,它不起作用...你知道怎么了吗?
    • 当前时间是多少秒?也许这个数字变为负数?
    • 这是我的代码:CMTime currentTime = music.currentTime;浮动 currentTimeInt = CMTimeGetSeconds(currentTime); if (currentTimeInt > 5) { CMTime newTime = CMTimeMakeWithSeconds(CMTimeGetSeconds(music.currentTime) - 5, music.currentTime.timescale); [音乐搜索时间:新时间]; } else { NSLog(@"error"); }
    • ...编辑:错过了您的 if 语句。让我再检查一下。它是否适用于 + 5 但不适用于 - 5?
    • 减号后值不变。我留下了 if 语句
    【解决方案2】:

    优雅的方式是使用CMTimeAdd

    CMTime currentTime = music.currentTime;
    CMTime timeToAdd   = CMTimeMakeWithSeconds(5,1);
    
    CMTime resultTime  = CMTimeAdd(currentTime,timeToAdd);
    
    //then hopefully 
    [music seekToTime:resultTime];
    

    到您的编辑: 您可以通过这些方式创建 CMTime 结构

    CMTimeMake
    CMTimeMakeFromDictionary
    CMTimeMakeWithEpoch
    CMTimeMakeWithSeconds
    

    更多@: https://developer.apple.com/library/mac/#documentation/CoreMedia/Reference/CMTime/Reference/reference.html

    【讨论】:

    • 'AVPlayer' 可能不会响应'setCurrentTime'
    • 将“CMTime”发送到不兼容类型“NSTimeInterval”(又名“double”)的参数
    • 也许你应该使用 [music seekToTime: resultTime]
    【解决方案3】:

    在斯威夫特中:

    private extension CMTime {
    
        func timeWithOffset(offset: TimeInterval) -> CMTime {
    
            let seconds = CMTimeGetSeconds(self)
            let secondsWithOffset = seconds + offset
    
            return CMTimeMakeWithSeconds(secondsWithOffset, preferredTimescale: timescale)
    
        }
    
    }
    

    【讨论】:

      【解决方案4】:

      Swift 4,使用自定义运算符:

      extension CMTime {
          static func + (lhs: CMTime, rhs: TimeInterval) -> CMTime {
              return CMTime(seconds: lhs.seconds + rhs,
                            preferredTimescale: lhs.timescale)
          }
      
          static func += (lhs: inout CMTime, rhs: TimeInterval) {
              lhs = CMTime(seconds: lhs.seconds + rhs,
                            preferredTimescale: lhs.timescale)
          }
      
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2012-03-09
        • 2023-03-28
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2010-12-23
        • 2011-05-31
        • 2013-02-13
        相关资源
        最近更新 更多