为什么AVPlayerItem的canPlayFastForward方法返回False?

时间:2016-04-13 23:16:20

标签: ios avfoundation avplayer

我真的想用AVFoundation实现快进和反向播放。

据我所知,如果AVPlayerItem的canPlayReverse和canPlayFastForward返回False,我只能用AVPlayer播放0.0~2.0的速率。

但我需要-1.0并且速率超过2.0。

我的问题是我无法找到 为什么结果为假。

没有提及canPlayFastForward何时在Apple的doc上返回false。

任何人都可以解释时间和原因 canPlayFastForward &的结果 canPlayReverse false ,如何将其更改为 true

1 个答案:

答案 0 :(得分:3)

AVPlayerItem的属性canPlayReverse更改为canPlayFastForward之前,您可能正在查看AVPlayerItem status.readToPlay false。如果您这样做,您将始终获得import AVFoundation let anAsset = AVAsset(URL: <#A URL#>) let playerItem = AVPlayerItem(asset: anAsset) let canPlayFastForward = playerItem.canPlayFastForward if (canPlayFastForward){ print("This line won't execute") }

不要这样做:

AVPlayerItem

而是观察status的属性import AVFoundation dynamic var songItem:AVPlayerItem! //Make it instance variable let anAsset = AVAsset(URL: <#A URL#>) let songItem = AVPlayerItem(asset: anAsset) playerItem.addObserver(self, forKeyPath: "status", options: .new, context: nil) 。以下是Apple的documentation

  

AVPlayerItem对象是动态的。的价值   所有基于文件的AVPlayerItem.canPlayFastForward都将更改为YES   资产和一些基于流的资产(如果源播放列表提供   允许它的媒体)项目准备好播放时。该   当玩家项目准备好玩时获得通知的方式是   通过键值观察观察AVPlayerItem.status属性   (KVO)。

observeValue

在同一个类中使用override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) { if let status = change?[.newKey] as? Int{ if(status == AVPlayerItemStatus.readyToPlay.rawValue){ yourPlayer.rate = 2.0 // or whatever you want } } } 方法:

deinit {
        playerItem.removeObserver(self, forKeyPath: "status")
    }

不要忘记从songItem的状态观察者中删除此课程

class CompetitionExports
  self.abstract_class: true # order just include the relevent ActiveRecord modules
  MODEL = Export

  def initialize(id)
    @competition_export_id = id
  end

  def all
    return [] unless validate
    MODEL.where(id: competition_exports_id).joins(:users) #can pluck/ select what you want here
  end

  private
  attr_reader :competition_export_id
  def validate
    return true if MODEL.where(id: competiton_export_id).has_competition?
    self.errors.add(:competition, 'does not exist')
    false
  end
end

class UserExports
  self.abstract_class: true # order just include the relevent ActiveRecord modules
  MODEL = Export
  def initialize(id)
    @user_export_id = id
  end

  def all_at(date)
    return [] unless validate(date)
    MODEL.where(id: user_exports_id).joins(:users) #can pluck/ select what you want here
  end

  private
  attr_reader :user_export_id
  def validate(date)
    return true if MODEL.where(id: user_export_id, date: date).present? 
    self.errors.add(:user, "no users at #{date}")
    false
  end
end