Created
March 26, 2021 14:46
-
-
Save vinnyA3/85e649cfe459ae4a097844d062289879 to your computer and use it in GitHub Desktop.
swift access log
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 AVKit | |
import AVFoundation | |
class ViewController: UIViewController { | |
@IBOutlet weak var playerContainer: UIView! | |
var player: AVPlayer = VideoPlayer.initPlayer() | |
var playerItem: AVPlayerItem? | |
override func viewDidLoad() { | |
super.viewDidLoad() | |
playerItem = self.player.currentItem | |
playerItem?.preferredPeakBitRate = 700000.00 | |
_ = Timer.scheduledTimer(withTimeInterval: 12.0, repeats: false) { [weak self] (timer) in | |
self?.playerItem?.preferredPeakBitRate = 0 | |
} | |
// Setup Log Observables | |
playerItem?.addObserver(self, forKeyPath: "playbackBufferEmpty", options: .new, context: nil) | |
playerItem?.addObserver(self, forKeyPath: "playbackLikelyToKeepUp", options: .new, context: nil) | |
playerItem?.addObserver(self, forKeyPath: "playbackBufferFull", options: .new, context: nil) | |
NotificationCenter.default.addObserver(self, | |
selector: #selector(handleAVPlayerAccess), | |
name: NSNotification.Name.AVPlayerItemNewAccessLogEntry, | |
object: nil) | |
NotificationCenter.default.addObserver(self, | |
selector: #selector(handleAVPlayerError), | |
name: NSNotification.Name.AVPlayerItemNewErrorLogEntry, | |
object: nil) | |
} | |
override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) { | |
if object is AVPlayerItem { | |
switch keyPath! { | |
case "playbackBufferEmpty": | |
print("[PLAYBACK LOGS] playback buffer is empty") | |
break | |
case "playbackLikelyToKeepUp": fallthrough | |
case "playbackBufferFull": fallthrough | |
default: | |
return | |
} | |
} | |
} | |
override func viewDidAppear(_ animated: Bool) { | |
let playerViewController = AVPlayerViewController() | |
playerViewController.player = player | |
present(playerViewController, animated: true){ | |
self.player.play() | |
} | |
} | |
@objc | |
func handleAVPlayerError(notification: Notification) { | |
guard let playerItem = notification.object as? AVPlayerItem, | |
let lastEvent = playerItem.errorLog()?.events.last else { | |
return | |
} | |
print("[PLAYER ERROR ACCESS LOG] uri: ", lastEvent.uri) | |
print("[PLAYER ERROR ACCESS LOG] errorStatusCode: ", lastEvent.errorStatusCode) | |
print("[PLAYER ERROR ACCESS LOG] errorDomain: ", lastEvent.errorDomain) | |
print("[PLAYER ERROR ACCESS LOG] errorComment: ", lastEvent.errorComment) | |
} | |
@objc | |
func handleAVPlayerAccess(notification: Notification) { | |
guard let playerItem = notification.object as? AVPlayerItem, | |
let lastEvent = playerItem.accessLog()?.events.last else { | |
return | |
} | |
print("[PLAYER ACCESS LOG] indicatedBitrate", lastEvent.indicatedBitrate) | |
print("[PLAYER ACCESS LOG] observedBitrate", lastEvent.observedBitrate) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment