Skip to content

Commit

Permalink
improving player logic
Browse files Browse the repository at this point in the history
  • Loading branch information
vitormesquita committed May 23, 2018
1 parent 0654174 commit 3e79cf9
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 34 deletions.
2 changes: 1 addition & 1 deletion KiwiPlayer.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

Pod::Spec.new do |s|
s.name = 'KiwiPlayer'
s.version = "0.1.7"
s.version = "0.1.8"
s.summary = 'Kiwi Player allows you go forward and go back in videos easily! 💃'
s.homepage = 'https://github.com/vitormesquita/KiwiPlayer'
s.license = { :type => 'MIT', :file => 'LICENSE' }
Expand Down
25 changes: 18 additions & 7 deletions KiwiPlayer/ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

import UIKit
import AVKit
import MediaPlayer

class ViewController: UIViewController {

Expand All @@ -20,7 +21,8 @@ class ViewController: UIViewController {
var items: [URL] = []
var queuePlayer: KiwiPlayer = KiwiPlayer()

// let routerPickerView = AVRoutePickerView()
let routerPickerView = AVRoutePickerView()
var isloaded = false

override func viewDidLoad() {
super.viewDidLoad()
Expand All @@ -30,18 +32,27 @@ class ViewController: UIViewController {
.map { $0! }

queuePlayer.setVideosURL(items)
queuePlayer.enableExternalPlayback = true
queuePlayer.delegate = self
queuePlayer.playFromBeginnig()

view.layer.addSublayer(queuePlayer.playerLayer)

slider.maximumValue = Float(queuePlayer.totalDurationInSeconds)

// routerPickerView.activeTintColor = .red
// routerPickerView.translatesAutoresizingMaskIntoConstraints = false
// self.view.addSubview(routerPickerView)
// routerPickerView.topAnchor.constraint(equalTo: view.topAnchor, constant: 40).isActive = true
// routerPickerView.trailingAnchor.constraint(equalTo: view.trailingAnchor, constant: -16).isActive = true
routerPickerView.activeTintColor = .red
routerPickerView.translatesAutoresizingMaskIntoConstraints = false
self.view.addSubview(routerPickerView)
routerPickerView.topAnchor.constraint(equalTo: view.topAnchor, constant: 40).isActive = true
routerPickerView.trailingAnchor.constraint(equalTo: view.trailingAnchor, constant: -16).isActive = true
}

override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)

if !isloaded {
queuePlayer.playFromBeginnig()
isloaded = true
}
}

override func viewDidLayoutSubviews() {
Expand Down
27 changes: 18 additions & 9 deletions Source/KiwiPlayer+AVPlayerItemObservers.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,10 @@ extension KiwiPlayer {
internal func addPlayerItemObservers(_ playerItem: AVPlayerItem?) {
guard let playerItem = playerItem else { return }

playerItem.addObserver(self, forKeyPath: playbackBufferEmptyKey, options: [.new, .old], context: &playerItemContext)
playerItem.addObserver(self, forKeyPath: playbackLikelyToKeepUpKey, options: [.new, .old], context: &playerItemContext)
playerItem.addObserver(self, forKeyPath: playbackBufferFullKey, options: [.new, .old], context: &playerItemContext)
playerItem.addObserver(self, forKeyPath: playbackLoadedTimeRanges, options: [.new, .old], context: &playerItemContext)
playerItem.addObserver(self, forKeyPath: playbackBufferEmptyKey, options: [] /*[.new, .old]*/, context: &playerItemContext)
playerItem.addObserver(self, forKeyPath: playbackLikelyToKeepUpKey, options: [] /*[.new, .old]*/, context: &playerItemContext)
playerItem.addObserver(self, forKeyPath: playbackBufferFullKey, options: [] /*[.new, .old]*/, context: &playerItemContext)
playerItem.addObserver(self, forKeyPath: playbackLoadedTimeRanges, options: [] /*[.new, .old]*/, context: &playerItemContext)

NotificationCenter.default.addObserver(self, selector: #selector(playerItemDidPlayToEndTime(_:)), name: .AVPlayerItemDidPlayToEndTime, object: playerItem)
NotificationCenter.default.addObserver(self, selector: #selector(playerItemFailedToPlayToEndTime(_:)), name: .AVPlayerItemFailedToPlayToEndTime, object: playerItem)
Expand All @@ -46,14 +46,19 @@ extension KiwiPlayer {
@objc internal func playerItemDidPlayToEndTime(_ notification: Notification) {
timePassed += currentPlayer?.currentTime().seconds ?? 0

guard let player = currentPlayer, let currentDuration = player.currentItem?.duration,
player.currentTime().seconds >= currentDuration.seconds else {
return
}

if let nextPlayer = nextPlayer {
currentPlayer = nextPlayer
currentItem = findNextElement(currentItem: currentItem)

play()

} else {
delegate?.playbackQueueIsOver()
stop()
}
}

Expand All @@ -73,17 +78,21 @@ extension KiwiPlayer {

switch keyPath {
case playbackBufferEmptyKey:
bufferingState = .delayed
if item.isPlaybackBufferEmpty {
bufferingState = .delayed
}

case playbackLikelyToKeepUpKey:
if item.isPlaybackLikelyToKeepUp {
bufferingState = .ready
}

case playbackLoadedTimeRanges:
bufferingState = .ready

case playbackBufferFullKey:
bufferingState = .loaded

case playbackLoadedTimeRanges:
break

default:
break
}
Expand Down
4 changes: 0 additions & 4 deletions Source/KiwiPlayer+ApplicationObservers.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,6 @@ extension KiwiPlayer {
NotificationCenter.default.addObserver(self, selector: #selector(applicationWillEnterForeground(_:)), name: .UIApplicationWillEnterForeground, object: UIApplication.shared)
}

internal func removeApplicationObservers() {
NotificationCenter.default.removeObserver(self)
}

@objc internal func applicationWillResignActive(_ aNotification: Notification) {
if playbackState == .playing {
pause()
Expand Down
26 changes: 13 additions & 13 deletions Source/KiwiPlayer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,15 @@ open class KiwiPlayer: NSObject {
/// QueuePlayer's delegate to notify when change something
public weak var delegate: KiwiPlayerDelegate?

/// Define the audio volume to current video
public var volume: Float = 1 {
didSet {
if let currentPlayer = currentPlayer {
currentPlayer.volume = volume
}
}
}

/// QueuePlayer's mute action
public var isMuted: Bool = false {
didSet {
Expand All @@ -47,7 +56,8 @@ open class KiwiPlayer: NSObject {
}
}
}


///
public var enableExternalPlayback: Bool = false {
didSet {
if let currentPlayer = currentPlayer {
Expand Down Expand Up @@ -131,7 +141,7 @@ open class KiwiPlayer: NSObject {
}

deinit {
removeApplicationObservers()
NotificationCenter.default.removeObserver(self)

currentPlayer = nil
currentItem = nil
Expand All @@ -149,7 +159,7 @@ open class KiwiPlayer: NSObject {

internal func setPlayerFromBeginning() {
currentItem = itemsQueue.first
currentPlayer = AVPlayer(playerItem: itemsQueue.first!.copy() as? AVPlayerItem)
currentPlayer = AVPlayer(playerItem: currentItem!.copy() as? AVPlayerItem)
currentPlayer?.seek(to: kCMTimeZero)
}
}
Expand All @@ -167,16 +177,6 @@ extension KiwiPlayer {
}
}

/// Define the audio volume to current video
public var volume: Float {
get {
return currentPlayer?.volume ?? 1
}
set {
currentPlayer?.volume = newValue
}
}

/// Set the videos URLs in queue
public func setVideosURL(_ videosURL: [URL]) {
itemsQueue = videosURL.map { AVURLAsset(url: $0) }.map { AVPlayerItem(asset: $0) }
Expand Down

0 comments on commit 3e79cf9

Please sign in to comment.