This file contains hidden or 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
| private let kAdvertUrl = "https://bitdash-a.akamaihd.net/content/MI201109210084_1/m3u8s/f08e80da-bf1d-4e3d-8899-f0f6155f6efa.m3u8".toUrl |
This file contains hidden or 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
| extension PlayerViewController: AVPlayerViewControllerDelegate { | |
| func playerViewController(_ playerViewController: AVPlayerViewController, willPresent interstitial: AVInterstitialTimeRange) { | |
| let playerVC = PlayerViewController() | |
| playerVC.url = kAdvertUrl | |
| present(playerVC, animated: false, completion: nil) | |
| } | |
| } |
This file contains hidden or 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
| private func createPlayerItem(url: URL) -> AVPlayerItem { | |
| let playerItem = AVPlayerItem(url: url) | |
| // Add advertisement timerange | |
| var timeRanges: [AVInterstitialTimeRange] = [] | |
| for index in stride(from: 60, to: 600, by: 60) { | |
| let range = AVInterstitialTimeRange(timeRange: CMTimeRange(start: CMTime(seconds: Double(index), preferredTimescale: .max), duration: CMTime(seconds: 1, preferredTimescale: .max))) | |
| timeRanges.append(range) | |
| } | |
| playerItem.interstitialTimeRanges = timeRanges | |
| return playerItem |
This file contains hidden or 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
| override func viewWillDisappear(_ animated: Bool) { | |
| super.viewWillDisappear(animated) | |
| player?.pause() | |
| } | |
| override func viewWillAppear(_ animated: Bool) { | |
| super.viewWillAppear(animated) | |
| player?.play() | |
| } |
This file contains hidden or 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
| class PlayerViewController: AVPlayerViewController { | |
| var url: URL? | |
| override func viewDidAppear(_ animated: Bool) { | |
| super.viewDidAppear(animated) | |
| if !initialized { | |
| if asset != nil { | |
| startAssetPlayback() | |
| } else { | |
| startAdvertPlayback() |
This file contains hidden or 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
| private func setupPlayer() { | |
| self.delegate = self | |
| NotificationCenter.default.addObserver(self, selector: #selector(playbackFinished), name: NSNotification.Name.AVPlayerItemDidPlayToEndTime, object: nil) | |
| } | |
| @objc private func playbackFinished() { | |
| if let queuePlayer = player as? AVQueuePlayer { | |
| if queuePlayer.currentItem != queuePlayer.items().last { | |
| return | |
| } |
This file contains hidden or 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
| private func startAssetPlayback() { | |
| switch asset?.type { | |
| case .movie: | |
| if let url = asset?.url.toUrl { | |
| let playerItem = createPlayerItem(url: url) | |
| player = AVPlayer(playerItem: playerItem) | |
| } | |
| case .series: | |
| var playerItems: [AVPlayerItem] = [] | |
| if let rail = rail?.list { |
This file contains hidden or 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
| private func openPlayer(with asset: AssetModel) { | |
| let playerVC = PlayerViewController() | |
| playerVC.asset = asset | |
| playerVC.rail = rail | |
| present(playerVC, animated: true, completion: nil) | |
| } |
This file contains hidden or 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
| class PlayerViewController: AVPlayerViewController { | |
| var asset: AssetModel? | |
| var rail: RailModel? | |
| var initialized: Bool = false | |
| override func viewDidAppear(_ animated: Bool) { | |
| super.viewDidAppear(animated) | |
| if !initialized { | |
| if asset != nil { | |
| startAssetPlayback() |
This file contains hidden or 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
| override func didUpdateFocus(in context: UIFocusUpdateContext, with coordinator: UIFocusAnimationCoordinator) { | |
| super.didUpdateFocus(in: context, with: coordinator) | |
| print("Previous focused item: \(String(describing: context.previouslyFocusedView)) \nNext focused item: \(String(describing: context.nextFocusedView)) \nFocus direction: \(context.focusHeading.rawValue)") | |
| } |
NewerOlder