前段時間我已經讓自己定製了一個循環的AVPlayer,隨意使用/評論!
protocol LoopingPlayerProgressDelegate: class {
func loopingPlayer(loopingPlayer: LoopingPlayer, didLoad percentage: Float)
func loopingPlayer(loopingPlayer: LoopingPlayer, didFinishLoading succeeded: Bool)
}
class LoopingPlayer: AVPlayer {
weak var progressDelegate: LoopingPlayerProgressDelegate?
var loopCount: Double = 0
var timer: NSTimer?
override init() {
super.init()
self.commonInit()
}
override init(URL url: NSURL!) {
super.init(URL: url)
self.commonInit()
}
override init(playerItem item: AVPlayerItem!) {
super.init(playerItem: item)
self.commonInit()
}
func commonInit() {
self.addObserver(self, forKeyPath: "currentItem", options: .New, context: nil)
self.actionAtItemEnd = .None
NSNotificationCenter.defaultCenter().addObserver(self, selector:"playerDidPlayToEndTimeNotification:", name:AVPlayerItemDidPlayToEndTimeNotification, object:nil)
if mutePlayers == true {
self.volume = 0.0
}
NSNotificationCenter.defaultCenter().addObserver(self, selector:"mute", name:"MutePlayers", object:nil)
NSNotificationCenter.defaultCenter().addObserver(self, selector:"unmute", name:"UnmutePlayers", object:nil)
}
deinit {
self.timer?.invalidate()
self.removeObserver(self, forKeyPath: "currentItem")
NSNotificationCenter.defaultCenter().removeObserver(self)
}
func mute() {
self.volume = 0.0
}
func unmute() {
self.volume = 1.0
}
var playableDuration: CMTime {
get {
if let item: AnyObject = self.currentItem?.loadedTimeRanges?.first {
if let timeRange = item.CMTimeRangeValue {
let playableDuration = CMTimeAdd(timeRange.start, timeRange.duration)
return playableDuration
}
}
return kCMTimeZero
}
}
var loadingProgress: Float {
get {
if (self.currentItem == nil) {
self.timer?.invalidate()
self.progressDelegate?.loopingPlayer(self, didFinishLoading: false)
return 0
}
let playableDurationInSeconds = CMTimeGetSeconds(self.playableDuration)
let totalDurationInSeconds = CMTimeGetSeconds(self.currentItem.duration)
if (totalDurationInSeconds.isNormal) {
var progress = Float(playableDurationInSeconds/totalDurationInSeconds)
self.progressDelegate?.loopingPlayer(self, didLoad: progress)
if (progress > 0.90) {
self.progressDelegate?.loopingPlayer(self, didFinishLoading: true)
self.timer?.invalidate()
}
return progress
}
return 0
}
}
func playerDidPlayToEndTimeNotification(notification: NSNotification) {
let playerItem: AVPlayerItem = notification.object as! AVPlayerItem
if (playerItem != self.currentItem) {
return
}
self.seekToTime(kCMTimeZero)
self.play()
loopCount += 1
}
override func observeValueForKeyPath(keyPath: String, ofObject object: AnyObject, change: [NSObject : AnyObject], context: UnsafeMutablePointer<Void>) {
if keyPath == "currentItem" {
self.timer?.invalidate()
self.timer = NSTimer.scheduledTimerWithTimeInterval(0.1, target: self, selector: "loadingProgress", userInfo: nil, repeats: true)
}
}
}
它已經有一段時間了,因此它可能有點塵土飛揚。對於那個很抱歉!
它也適用於HLS文件;)
希望這會有所幫助!
你甚至沒有向我們展示你的循環代碼,所以很難幫助你。 – pbush25
對不起,添加了循環代碼 – OvidijusR
而且,它是如何崩潰的?堆棧跟蹤,請;-) – rholmes