399d2c7be1
* Reorganize project and top level - Move sources into Sources - Update podspec to reflect new sources location - Move Example files into top level * Update Fastfile - Point to new project/workspace locations * Update Fastfile with correct config * Fix CI build failures - Update Fastfile to point to correct scheme - Add Tests file to PlayerKit-iOSTests target - Update Example scheme to exclude tests - Update scheme for iOS Framework target to include tests * Update .gitignore and commit Pods directory * Remove cocoapods from lane altogether
103 lines
2.6 KiB
Swift
103 lines
2.6 KiB
Swift
//
|
|
// PlayerViewController.swift
|
|
// PlayerKit
|
|
//
|
|
// Created by King, Gavin on 3/8/17.
|
|
// Copyright © 2017 CocoaPods. All rights reserved.
|
|
//
|
|
|
|
import UIKit
|
|
import PlayerKit
|
|
import AVFoundation
|
|
|
|
class PlayerViewController: UIViewController, PlayerDelegate {
|
|
private struct Constants {
|
|
static let VideoURL = URL(string: "https://github.com/vimeo/PlayerKit/blob/master/Example/PlayerKit/video.mp4?raw=true")!
|
|
}
|
|
|
|
@IBOutlet weak var playButton: UIButton!
|
|
@IBOutlet weak var slider: UISlider!
|
|
@IBOutlet weak var label: UILabel!
|
|
@IBOutlet weak var activityIndicator: UIActivityIndicatorView!
|
|
|
|
private let player = RegularPlayer()
|
|
|
|
override func viewDidLoad() {
|
|
super.viewDidLoad()
|
|
|
|
player.delegate = self
|
|
|
|
self.addPlayerToView()
|
|
|
|
self.player.set(AVURLAsset(url: Constants.VideoURL))
|
|
}
|
|
|
|
// MARK: Setup
|
|
|
|
private func addPlayerToView() {
|
|
player.view.autoresizingMask = [.flexibleWidth, .flexibleHeight]
|
|
player.view.frame = self.view.bounds
|
|
self.view.insertSubview(player.view, at: 0)
|
|
}
|
|
|
|
// MARK: Actions
|
|
|
|
@IBAction func didTapPlayButton() {
|
|
self.player.playing ? self.player.pause() : self.player.play()
|
|
}
|
|
|
|
@IBAction func didChangeSliderValue() {
|
|
let value = Double(self.slider.value)
|
|
|
|
let time = value * self.player.duration
|
|
|
|
self.player.seek(to: time)
|
|
}
|
|
|
|
// MARK: VideoPlayerDelegate
|
|
|
|
func playerDidUpdateState(player: Player, previousState: PlayerState) {
|
|
self.activityIndicator.isHidden = true
|
|
|
|
switch player.state {
|
|
case .loading:
|
|
|
|
self.activityIndicator.isHidden = false
|
|
|
|
case .ready:
|
|
|
|
break
|
|
|
|
case .failed:
|
|
|
|
NSLog("🚫 \(String(describing: player.error))")
|
|
}
|
|
}
|
|
|
|
func playerDidUpdatePlaying(player: Player) {
|
|
self.playButton.isSelected = player.playing
|
|
}
|
|
|
|
func playerDidUpdateTime(player: Player) {
|
|
guard player.duration > 0 else {
|
|
return
|
|
}
|
|
|
|
let ratio = player.time / player.duration
|
|
|
|
if self.slider.isHighlighted == false {
|
|
self.slider.value = Float(ratio)
|
|
}
|
|
}
|
|
|
|
func playerDidUpdateBufferedTime(player: Player) {
|
|
guard player.duration > 0 else {
|
|
return
|
|
}
|
|
|
|
let ratio = Int((player.bufferedTime / player.duration) * 100)
|
|
|
|
self.label.text = "Buffer: \(ratio)%"
|
|
}
|
|
}
|