Skip to content

Commit

Permalink
added URLS for queues video plays, to reproduce a list of videos
Browse files Browse the repository at this point in the history
  • Loading branch information
davidlondono committed May 18, 2016
1 parent eef6dd8 commit 923afe7
Show file tree
Hide file tree
Showing 4 changed files with 111 additions and 14 deletions.
12 changes: 10 additions & 2 deletions Example/PlayerView/ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,13 @@ class ViewController: UIViewController {


playerVideo.delegate = self
let url1 = NSURL(string: "http://techslides.com/demos/sample-videos/small.mp4")!
let url = NSURL(string: "http://www.sample-videos.com/video/mp4/720/big_buck_bunny_720p_30mb.mp4")!

playerVideo.url = url
//playerVideo.url = url

playerVideo.urls = [url1,url1]
playerVideo.addVideosOnQueue(urls: [url])
tap.numberOfTapsRequired = 2
tap.addTarget(self, action: .changeFill)
view.addGestureRecognizer(tap)
Expand Down Expand Up @@ -121,7 +124,10 @@ extension ViewController: PlayerViewDelegate {
}
func playerVideo(player: PlayerView, loadedTimeRanges: [PlayerviewTimeRange]) {

let dur2 = Float(loadedTimeRanges.first!.end.seconds)
let durationTotal = loadedTimeRanges.reduce(0) { (actual, range) -> Double in
return actual + range.end.seconds
}
let dur2 = Float(durationTotal)
let progress = dur2 / duration
progressBar?.progress = progress

Expand Down Expand Up @@ -158,6 +164,8 @@ extension ViewController: PlayerViewDelegate {
}

func playerVideo(playerFinished player: PlayerView) {
player.next()
player.play()
print("video has finished")
}
}
2 changes: 1 addition & 1 deletion PlayerView.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

Pod::Spec.new do |s|
s.name = "PlayerView"
s.version = "0.2.1"
s.version = "0.2.5"
s.summary = "A UIView for videos using AVPlayer with delegate"

# This description is used to generate tags and improve search results.
Expand Down
11 changes: 10 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ Installation is also available using the dependency manager [Carthage](https://g
To integrate, add the following line to your `Cartfile`:

```ogdl
github "davidlondono/PlayerView" >= 0.1.0
github "davidlondono/PlayerView" >= 0.2.5
```

### Swift Package Manager
Expand Down Expand Up @@ -124,9 +124,18 @@ playerVideo.pause()
// stop the video on current time
playerVideo.stop()


// stop the video on current time
playerVideo.next()

//to set the url of Video
if let url = NSURL(string: "http://www.sample-videos.com/video/mp4/720/big_buck_bunny_720p_30mb.mp4") {
playerVideo.url = url
//or
playerVideo.urls = [url]

//add videos on queue
playerVideo.addVideosOnQueue(urls: [url])
}

//Take a screenshot on time, and return time to ensure the tolerance of the image
Expand Down
100 changes: 90 additions & 10 deletions Sources/Classes/PlayerView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -101,9 +101,9 @@ private extension CMTime {
//MARK: - Public Variables
public weak var delegate: PlayerViewDelegate?

public var player: AVPlayer? {
public var player: AVQueuePlayer? {
get {
return playerLayer.player
return playerLayer.player as? AVQueuePlayer
}

set {
Expand Down Expand Up @@ -175,12 +175,21 @@ private extension CMTime {


if let playerItem = player.currentItem {
playerItem.removeObserver(self, forKeyPath: "loadedTimeRanges", context: &loadedContext)
playerItem.removeObserver(self, forKeyPath: "duration", context: &durationContext)
playerItem.removeObserver(self, forKeyPath: "status", context: &statusItemContext)
removeObserversVideoItem(playerItem)
}
self.player = nil
}
func addObserversVideoItem(playerItem: AVPlayerItem) {
playerItem.addObserver(self, forKeyPath: "loadedTimeRanges", options: [], context: &loadedContext)
playerItem.addObserver(self, forKeyPath: "duration", options: [], context: &durationContext)
playerItem.addObserver(self, forKeyPath: "status", options: [], context: &statusItemContext)
}
func removeObserversVideoItem(playerItem: AVPlayerItem) {

playerItem.removeObserver(self, forKeyPath: "loadedTimeRanges", context: &loadedContext)
playerItem.removeObserver(self, forKeyPath: "duration", context: &durationContext)
playerItem.removeObserver(self, forKeyPath: "status", context: &statusItemContext)
}

func removeCurrentTimeObserver() {

Expand Down Expand Up @@ -217,6 +226,9 @@ private extension CMTime {
currentTime = 0
pause()
}
public func next() {
player?.advanceToNextItem()
}

public func availableDuration() -> CMTimeRange {
let range = self.player?.currentItem?.loadedTimeRanges.first
Expand Down Expand Up @@ -274,10 +286,10 @@ private extension CMTime {
}
//reset before put another URL
resetPlayer()
let avPlayer = AVPlayer(URL: url)
let avPlayer = AVQueuePlayer(URL: url)
self.player = avPlayer

avPlayer.actionAtItemEnd = .None
avPlayer.actionAtItemEnd = .Advance


//avPlayer.play()
Expand All @@ -291,9 +303,8 @@ private extension CMTime {

avPlayer.addObserver(self, forKeyPath: "status", options: [.New], context: &statusContext)
avPlayer.addObserver(self, forKeyPath: "rate", options: [.New], context: &rateContext)
playerItem.addObserver(self, forKeyPath: "loadedTimeRanges", options: [], context: &loadedContext)
playerItem.addObserver(self, forKeyPath: "duration", options: [], context: &durationContext)
playerItem.addObserver(self, forKeyPath: "status", options: [], context: &statusItemContext)
avPlayer.addObserver(self, forKeyPath: "currentItem", options: [.Old,.New], context: &playerItemContext)
addObserversVideoItem(playerItem)
//playerItem.addObserver(self, forKeyPath: "currentTime", options: [], context: &currentTimeContext)

NSNotificationCenter.defaultCenter().addObserver(self, selector: .playerItemDidPlayToEndTime, name: AVPlayerItemDidPlayToEndTimeNotification, object: playerItem)
Expand All @@ -302,6 +313,65 @@ private extension CMTime {
}
}

public var urls: [NSURL]? {
didSet {
guard let urls = urls else {
resetPlayer()
return
}
//reset before put another URL
resetPlayer()

let playerItems = urls.map { (url) -> AVPlayerItem in
return AVPlayerItem(URL: url)
}

let avPlayer = AVQueuePlayer(items: playerItems)
self.player = avPlayer

avPlayer.actionAtItemEnd = .Advance


//avPlayer.play()


let playerItem = avPlayer.currentItem!

avPlayer.status
playerItem.status


avPlayer.addObserver(self, forKeyPath: "status", options: [.New], context: &statusContext)
avPlayer.addObserver(self, forKeyPath: "rate", options: [.New], context: &rateContext)
avPlayer.addObserver(self, forKeyPath: "currentItem", options: [.Old,.New], context: &playerItemContext)
addObserversVideoItem(playerItem)
//playerItem.addObserver(self, forKeyPath: "currentTime", options: [], context: &currentTimeContext)

NSNotificationCenter.defaultCenter().addObserver(self, selector: .playerItemDidPlayToEndTime, name: AVPlayerItemDidPlayToEndTimeNotification, object: playerItem)
avPlayer.status
// Do any additional setup after loading the view, typically from a nib.
}
}
public func addVideosOnQueue(urls urls: [NSURL]) {

//on last item on player
if let item = player?.items().last {

//for each url found
urls.forEach({ (url) in

//create a video item
let itemNew = AVPlayerItem(URL: url)

//and insert the item on the player
player?.insertItem(itemNew, afterItem: item)
})
}

}
public func addVideosOnQueue(urls: NSURL...) {
return addVideosOnQueue(urls: urls)
}
// MARK: public object lifecycle view

override public class func layerClass() -> AnyClass {
Expand Down Expand Up @@ -338,6 +408,7 @@ private extension CMTime {
private var durationContext = true
private var currentTimeContext = true
private var rateContext = true
private var playerItemContext = true


public func playerItemDidPlayToEndTime(aNotification: NSNotification) {
Expand Down Expand Up @@ -399,6 +470,15 @@ private extension CMTime {

self.delegate?.playerVideo(self, rate: newRate)

} else if context == &playerItemContext{
guard let oldItem = (change?[NSKeyValueChangeOldKey] as? AVPlayerItem) else{
return
}
removeObserversVideoItem(oldItem)
guard let newItem = (change?[NSKeyValueChangeNewKey] as? AVPlayerItem) else{
return
}
addObserversVideoItem(newItem)
} else {
super.observeValueForKeyPath(keyPath, ofObject: object, change: change, context: context)
}
Expand Down

0 comments on commit 923afe7

Please sign in to comment.