Created
June 3, 2020 00:32
-
-
Save DDavis1025/ff2b1dd0a82c2e51f710a86357b1ec19 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import UIKit | |
import AVFoundation | |
class VideoPlayerView: UIView { | |
private var playerLayer: AVPlayerLayer? | |
var player:AVPlayer? | |
let activityIndicatorView:UIActivityIndicatorView = { | |
let aiv = UIActivityIndicatorView(style: UIActivityIndicatorView.Style.large) | |
aiv.startAnimating() | |
return aiv | |
}() | |
lazy var pauseButton: UIButton = { | |
let button = UIButton(type: .system) | |
let config = UIImage.SymbolConfiguration(pointSize: 35, weight: .black, scale: .medium) | |
let image = UIImage(systemName: "pause.fill", withConfiguration: config) as UIImage? | |
let whiteImage = image?.withTintColor(.white, renderingMode: .alwaysOriginal) | |
button.setImage(whiteImage, for: .normal) | |
button.translatesAutoresizingMaskIntoConstraints = false | |
button.isUserInteractionEnabled = true | |
button.addTarget(self, action: #selector(handlePause), for: .touchUpInside) | |
return button | |
}() | |
@objc func handlePause() { | |
print("pause clicked") | |
player?.pause() | |
} | |
let controlsContainerView: UIView = { | |
let view = UIView() | |
view.backgroundColor = UIColor(white: 0, alpha: 1) | |
return view | |
}() | |
override init(frame: CGRect) { | |
super.init(frame: frame) | |
setupPlayer() | |
addSubview(controlsContainerView) | |
controlsContainerView.frame = bounds | |
controlsContainerView.addSubview(activityIndicatorView) | |
activityIndicatorView.translatesAutoresizingMaskIntoConstraints = false | |
activityIndicatorView.centerXAnchor.constraint(equalTo: centerXAnchor).isActive = true | |
activityIndicatorView.centerYAnchor.constraint(equalTo: centerYAnchor).isActive = true | |
controlsContainerView.addSubview(pauseButton) | |
pauseButton.centerXAnchor.constraint(equalTo: centerXAnchor).isActive = true | |
pauseButton.centerYAnchor.constraint(equalTo: centerYAnchor).isActive = true | |
} | |
private func setupPlayer() { | |
let urlString = "http://www.w3schools.com/html/mov_bbb.mp4" | |
guard let url = URL(string: urlString) else { return } | |
player = AVPlayer(url: url) | |
let playerLayer = AVPlayerLayer(player: player) | |
playerLayer.videoGravity = .resizeAspectFill | |
self.layer.addSublayer(playerLayer) | |
self.playerLayer = playerLayer | |
player?.play() | |
player?.addObserver(self, forKeyPath: "currentItem.loadedTimeRanges", options: .new, context: nil) | |
} | |
override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) { | |
if keyPath == "currentItem.loadedTimeRanges" { | |
print(change) | |
activityIndicatorView.stopAnimating() | |
controlsContainerView.backgroundColor = .clear | |
} | |
} | |
required init?(coder: NSCoder) { | |
fatalError("init(coder:) has not been implemented") | |
} | |
override func layoutSubviews() { | |
super.layoutSubviews() | |
playerLayer?.frame = bounds | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment