Created
January 16, 2020 17:42
-
-
Save chuckrincon/8169b48d40a5ba963ee78e143c79c653 to your computer and use it in GitHub Desktop.
AVKit Example
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 | |
class NewsTableViewCell: MasterPostTVCell { | |
// MARK: - @IBOutlet | |
@IBOutlet weak var vVideo: UIView! | |
@IBOutlet weak var lblTitle: UILabel! | |
@IBOutlet weak var lblDetail: UILabel! | |
// MARK: - Properties | |
var video_url: String? | |
var vPlayer: VPlayer? | |
override func awakeFromNib() { | |
super.awakeFromNib() | |
} | |
override func setSelected(_ selected: Bool, animated: Bool) { | |
super.setSelected(selected, animated: animated) | |
} | |
override func tableView(_ tableView: UITableView, cellForItemAt indexPath: IndexPath, news: News) -> MasterPostTVCell | |
{ | |
let cell = tableView.dequeueReusableCell(withIdentifier: "NewsTableViewCell", for: indexPath) as! NewsTableViewCell | |
cell.lblTitle.text = news.title | |
cell.lblDetail.text = news.content | |
guard let video_url = news.media else | |
{ | |
print("video_url is nil") | |
return cell | |
} | |
cell.video_url = video_url | |
return cell | |
} | |
override func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forItemAt indexPath: IndexPath, news: News) | |
{ | |
guard let video_url = news.media else { return } | |
DispatchQueue.main.async | |
{ | |
self.vPlayer?.dismiss() | |
self.vPlayer = nil | |
self.vPlayer = VPlayer.init(video_url: video_url, frame: self.vVideo.bounds, in: self.vVideo, showsPlaybackControls: true) | |
} | |
} | |
override func tableView(_ tableView: UITableView, didEndDisplaying cell: UITableViewCell, forItemAt indexPath: IndexPath) | |
{ | |
self.vPlayer?.dismiss() | |
self.vPlayer = nil | |
} | |
} |
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
{ | |
"data": { | |
"news": [{ | |
"id": 1, | |
"title": "Introducing Chromecast.", | |
"content": "The easiest way to enjoy online video and music on your TV—for the times that call for bigger joyrides. For $35. Learn how to use Chromecast with YouTube and more at google.com/chromecast.", | |
"media": "http://commondatastorage.googleapis.com/gtv-videos-bucket/sample/ForBiggerJoyrides.mp4", | |
"media_mime_type": "videp/mp4", | |
"created_at": "2020-01-16 10:18:25", | |
"updated_at": "2020-01-16 10:18:25" | |
}, { | |
"id": 2, | |
"title": "HBO GO now works with Chromecast.", | |
"content": "the easiest way to enjoy online video on your TV. For when you want to settle into your Iron Throne to watch the latest episodes. For $35.\nLearn how to use Chromecast with HBO GO and more at google.com/chromecast", | |
"media": "http://commondatastorage.googleapis.com/gtv-videos-bucket/sample/ForBiggerBlazes.mp4", | |
"media_mime_type": "videp/mp4", | |
"created_at": "2020-01-16 10:18:25", | |
"updated_at": "2020-01-16 10:18:25" | |
}] | |
} | |
} |
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 AVKit | |
import UIKit | |
import AVFoundation | |
class VPlayer: NSObject | |
{ | |
private var avPlayerController: AVPlayerViewController! | |
var player: AVPlayer! | |
private var video_url: String? | |
private var frame: CGRect? | |
init(url: URL, frame: CGRect, in view: UIView, showsPlaybackControls: Bool) | |
{ | |
self.player = AVPlayer(url: url) | |
self.avPlayerController = AVPlayerViewController() | |
self.avPlayerController.player = player | |
self.avPlayerController.view.frame = frame | |
// hide show control | |
self.avPlayerController.showsPlaybackControls = showsPlaybackControls | |
view.addSubview(self.avPlayerController!.view) | |
} | |
convenience init?(video_url: String, frame: CGRect, in view: UIView, showsPlaybackControls: Bool) | |
{ | |
guard let url: URL = URL.init(string: video_url) else { return nil } | |
self.init(url: url, frame: frame, in: view, showsPlaybackControls: showsPlaybackControls) | |
} | |
func set(video_url: String) | |
{ | |
guard let url: URL = URL.init(string: video_url) else { return } | |
self.set(url: url) | |
} | |
func set(url: URL) | |
{ | |
self.player = AVPlayer(url: url) | |
self.avPlayerController.player = player | |
} | |
func show(playbackControls showsPlaybackControls: Bool) | |
{ | |
self.avPlayerController.showsPlaybackControls = showsPlaybackControls | |
} | |
func play() | |
{ | |
self.avPlayerController.player?.play() | |
} | |
func pause() | |
{ | |
self.avPlayerController.player?.pause() | |
} | |
func dismiss() | |
{ | |
self.player = nil | |
self.avPlayerController.view.removeFromSuperview() | |
self.avPlayerController.dismiss(animated: true, completion: nil) | |
self.avPlayerController = nil | |
} | |
func showBtyes() | |
{ | |
guard let accesslog: AVPlayerItemAccessLog = self.player.currentItem?.accessLog() else { return } | |
let events = accesslog.events | |
let event = events[0] | |
print("\(event.numberOfBytesTransferred)") | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment