Skip to content

Commit

Permalink
fixing player observer, rename delegate and put shared scheme
Browse files Browse the repository at this point in the history
  • Loading branch information
vitormesquita committed Feb 15, 2018
1 parent 82629ea commit 93b7883
Show file tree
Hide file tree
Showing 5 changed files with 105 additions and 11 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.2'
s.version = '0.1.3'
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
93 changes: 93 additions & 0 deletions KiwiPlayer.xcodeproj/xcshareddata/xcschemes/KiwiPlayer.xcscheme
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
<?xml version="1.0" encoding="UTF-8"?>
<Scheme
LastUpgradeVersion = "0920"
version = "1.3">
<BuildAction
parallelizeBuildables = "YES"
buildImplicitDependencies = "YES">
<BuildActionEntries>
<BuildActionEntry
buildForTesting = "YES"
buildForRunning = "YES"
buildForProfiling = "YES"
buildForArchiving = "YES"
buildForAnalyzing = "YES">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "FCF29E7B2034C16E0020FA8B"
BuildableName = "KiwiPlayer.app"
BlueprintName = "KiwiPlayer"
ReferencedContainer = "container:KiwiPlayer.xcodeproj">
</BuildableReference>
</BuildActionEntry>
</BuildActionEntries>
</BuildAction>
<TestAction
buildConfiguration = "Debug"
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
language = ""
shouldUseLaunchSchemeArgsEnv = "YES">
<Testables>
</Testables>
<MacroExpansion>
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "FCF29E7B2034C16E0020FA8B"
BuildableName = "KiwiPlayer.app"
BlueprintName = "KiwiPlayer"
ReferencedContainer = "container:KiwiPlayer.xcodeproj">
</BuildableReference>
</MacroExpansion>
<AdditionalOptions>
</AdditionalOptions>
</TestAction>
<LaunchAction
buildConfiguration = "Debug"
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
language = ""
launchStyle = "0"
useCustomWorkingDirectory = "NO"
ignoresPersistentStateOnLaunch = "NO"
debugDocumentVersioning = "YES"
debugServiceExtension = "internal"
allowLocationSimulation = "YES">
<BuildableProductRunnable
runnableDebuggingMode = "0">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "FCF29E7B2034C16E0020FA8B"
BuildableName = "KiwiPlayer.app"
BlueprintName = "KiwiPlayer"
ReferencedContainer = "container:KiwiPlayer.xcodeproj">
</BuildableReference>
</BuildableProductRunnable>
<AdditionalOptions>
</AdditionalOptions>
</LaunchAction>
<ProfileAction
buildConfiguration = "Release"
shouldUseLaunchSchemeArgsEnv = "YES"
savedToolIdentifier = ""
useCustomWorkingDirectory = "NO"
debugDocumentVersioning = "YES">
<BuildableProductRunnable
runnableDebuggingMode = "0">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "FCF29E7B2034C16E0020FA8B"
BuildableName = "KiwiPlayer.app"
BlueprintName = "KiwiPlayer"
ReferencedContainer = "container:KiwiPlayer.xcodeproj">
</BuildableReference>
</BuildableProductRunnable>
</ProfileAction>
<AnalyzeAction
buildConfiguration = "Debug">
</AnalyzeAction>
<ArchiveAction
buildConfiguration = "Release"
revealArchiveInOrganizer = "YES">
</ArchiveAction>
</Scheme>
2 changes: 1 addition & 1 deletion KiwiPlayer/ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ class ViewController: UIViewController {
}
}

extension ViewController: QueuePlayerDelegate {
extension ViewController: KiwiPlayerDelegate {
func playbackTimeDidChange(_ seconds: Float64) {
slider.value = Float(seconds)
}
Expand Down
11 changes: 6 additions & 5 deletions Source/KiwiPlayer+AVPlayerObserver.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,17 @@ import AVFoundation
// MARK: - AVPlayer Observer
extension KiwiPlayer {

internal func addPlayerObserver() {
timeObserver = currentPlayer?.addPeriodicTimeObserver(forInterval: CMTimeMake(1, 2), queue: DispatchQueue.main, using: {[weak self] (time) in
internal func addPlayerObserver(_ player: AVPlayer?) {
guard let player = player else { return }
timeObserver = player.addPeriodicTimeObserver(forInterval: CMTimeMake(1, 2), queue: DispatchQueue.main, using: {[weak self] (time) in
guard let strongSelf = self else { return }
strongSelf.currentTime = time.seconds
})
}

internal func removePlayerObserver() {
if let timeObserver = timeObserver {
currentPlayer?.removeTimeObserver(timeObserver)
internal func removePlayerObserver(_ player: AVPlayer?) {
if let timeObserver = timeObserver, let player = player {
player.removeTimeObserver(timeObserver)
}
}
}
Expand Down
8 changes: 4 additions & 4 deletions Source/KiwiPlayer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import UIKit
import AVFoundation

public protocol QueuePlayerDelegate: class {
public protocol KiwiPlayerDelegate: class {
func bufferingStateDidChange(_ bufferState: BufferingState)
func playbackStateDidChange(_ playerState: PlaybackState)
func playbackTimeDidChange(_ seconds: Float64)
Expand Down Expand Up @@ -37,7 +37,7 @@ open class KiwiPlayer: NSObject {
public var playerLayer: AVPlayerLayer

/// QueuePlayer's delegate to notify when change something
public weak var delegate: QueuePlayerDelegate?
public weak var delegate: KiwiPlayerDelegate?

/// Video queue
internal var itemsQueue: [AVPlayerItem] = []
Expand Down Expand Up @@ -75,13 +75,14 @@ open class KiwiPlayer: NSObject {
}
set {
playerItemRemoveObservers(playerLayer.player?.currentItem)
removePlayerObserver(playerLayer.player)

newValue?.volume = volume
newValue?.actionAtItemEnd = .pause

playerLayer.player = newValue

addPlayerObserver()
addPlayerObserver(newValue)
addPlayerItemObservers(newValue?.currentItem)
}
}
Expand Down Expand Up @@ -110,7 +111,6 @@ open class KiwiPlayer: NSObject {

deinit {
removeApplicationObservers()
removePlayerObserver()

currentPlayer = nil
currentItem = nil
Expand Down

0 comments on commit 93b7883

Please sign in to comment.