Created
August 21, 2022 19:34
-
-
Save christianselig/f1bb56f9a5d0311a62ebb160d7ba4a72 to your computer and use it in GitHub Desktop.
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
import UIKit | |
import AVFoundation | |
class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource { | |
let tableView = UITableView(frame: .zero, style: .plain) | |
override func viewDidLoad() { | |
super.viewDidLoad() | |
tableView.register(PlayerTableViewCell.self, forCellReuseIdentifier: "PlayerCell") | |
tableView.delegate = self | |
tableView.dataSource = self | |
tableView.frame = view.bounds | |
view.addSubview(tableView) | |
} | |
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { | |
let cell = tableView.dequeueReusableCell(withIdentifier: "PlayerCell", for: indexPath) as! PlayerTableViewCell | |
let url = urls[indexPath.row] | |
let player = AVPlayer(url: url) | |
player.isMuted = true | |
cell.playerView.player = player | |
return cell | |
} | |
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { | |
return urls.count | |
} | |
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat { | |
return 232.0 | |
} | |
func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) { | |
guard let cell = cell as? PlayerTableViewCell else { fatalError() } | |
cell.playerView.player?.play() | |
} | |
} | |
class PlayerTableViewCell: UITableViewCell { | |
let playerView = PlayerView() | |
override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) { | |
super.init(style: style, reuseIdentifier: reuseIdentifier) | |
contentView.addSubview(playerView) | |
} | |
@available(*, unavailable) | |
required init?(coder aDecoder: NSCoder) { fatalError("\(#file) does not implement coder.") } | |
override func layoutSubviews() { | |
super.layoutSubviews() | |
playerView.frame = contentView.bounds | |
} | |
} | |
class PlayerView: UIView { | |
override class var layerClass: AnyClass { AVPlayerLayer.self } | |
var player: AVPlayer? { | |
get { | |
return (layer as? AVPlayerLayer)?.player | |
} | |
set { | |
(layer as? AVPlayerLayer)?.player = newValue | |
} | |
} | |
var playerLayer: AVPlayerLayer? { layer as? AVPlayerLayer } | |
} | |
let urls: [URL] = [ | |
"https://v.redd.it/i8r4cjel38e91/HLSPlaylist.m3u8?a=1663701482%2CMTRiZGYwZDM0NWM1MTU0MGM2NzZlMjY2Njc2OTQ2ZmZhNTMxYzIyZGVmYjg4NDE5NDA0MjE3NjcxZWQ5YmQzZg%3D%3D&v=1&f=hd", | |
"https://v.redd.it/bag1nyzft7d91/HLSPlaylist.m3u8?a=1663701521%2CZjg0N2RlZTk4MDMyYTIxMTQ0NDRmZTYyY2IxZmM3NWQ3MTM1MThlMjE3ZmU5M2MzMTQ0NDE1ZjVlOTNlNzNlYg%3D%3D&v=1&f=hd", | |
"https://v.redd.it/194ugls1n8g91/HLSPlaylist.m3u8?a=1663701531%2CM2Y3YTIwYjg4NzU2MjZjMWM0MzM2YjdkMmM5NWU0MDQ1YzE4YjNiNTFhOTVlYWZhNDkwYjIwN2NhYmYxODhjOQ%3D%3D&v=1&f=hd", | |
"https://v.redd.it/ja2vgktad4e91/HLSPlaylist.m3u8?a=1663701541%2CNjdmYjZmNDZkODRjM2Y0NDMzMDMyMGQ5Zjg1MjAyOTE0NTZjMDU0MTM0MGFkZjg2YmY1ZDljOTAxZDg5MTQwNA%3D%3D&v=1&f=hd", | |
"https://v.redd.it/0avzb1buoig91/HLSPlaylist.m3u8?a=1663701550%2CZTdiY2QzY2Q1MGU0ODgxNTA5MGJhNDExMGIyYWRlNTJmNDZjZTEyYjJlYjUxMjYyODdhNzgyODNiYTI5NDk2Mw%3D%3D&v=1&f=hd", | |
"https://v.redd.it/ltd6k9ahhgi91/HLSPlaylist.m3u8?a=1663701563%2CNDI5ZWY1MzJjZmY2YjU3NDE3ZDVkNTkzMjFjYjAxYjM1YTM4NjU3Yjk0YmNiOWE1NDAyNzY5MzdiMTFhNTIyMA%3D%3D&v=1&f=hd", | |
"https://v.redd.it/9nooi1601qg91/HLSPlaylist.m3u8?a=1663701574%2CYTJmNWRlNTc0YTRlZjU1MThjNjg4NjU0MzZmMDBhM2Q0OWI3MWY0OTY3ODhhZWJjZDU4OTZmNDdmOGYyOTQ5MA%3D%3D&v=1&f=hd", | |
"https://v.redd.it/iblb787bxyf91/HLSPlaylist.m3u8?a=1663701583%2CODg4YTkwZDAzMTExM2VjOTlmOWUwODRiMmViMTBiNGQ1NDkzODFiNmUxZWFmOTAxZWQxZjZmMGNlN2MxYzdkMw%3D%3D&v=1&f=hd", | |
"https://v.redd.it/27zb1bd5qsf91/HLSPlaylist.m3u8?a=1663701593%2CYWRmMjAwOTMzNWEyODgyNThhMjhjMmQ0MGU3MDk5NzhkOGUwZjE5NDI0ZmJhM2RlYjc1OGY5N2Y3OWJjYmFhNg%3D%3D&v=1&f=hd", | |
"https://v.redd.it/vdm3whzbzbh91/HLSPlaylist.m3u8?a=1663701602%2CYzg5YmEwMjJiMWQwMDMzODE4ZGVkZWRlMDhmMGM1N2ZmMTY5MTRmYzcyYjU0OTAxYTc5NTY5NDQ5MTA4NWExZQ%3D%3D&v=1&f=hd", | |
"https://v.redd.it/awsd4n55bhe91/HLSPlaylist.m3u8?a=1663701610%2CMDc1NzNkMzQ1NTliY2UyZGI4ZDQyOGRjZGZlOTkwNTk0MDFhMTMwMDg4ZDI4ZjJjYzhhYjA2ZTJmZmM0ZjQxMw%3D%3D&v=1&f=hd", | |
"https://v.redd.it/5nvewcmdtvd91/HLSPlaylist.m3u8?a=1663701622%2CMTM1MGMzMzgxOWI2Yzg0YjdkMTI2ZDMxMTI4OWVlNzNkYTQ5OWE1ZmU1YjQ4MmViZjUzNWNmY2ZkNjQ0Nzc4Yw%3D%3D&v=1&f=hd", | |
"https://v.redd.it/0ni02hqcoqd91/HLSPlaylist.m3u8?a=1663701633%2CY2ViMDhlZjk4ZjhjOTdhMzEwODQ0NWQ1YTQ0NjRlOGI1MzViZDQxZjViNTliM2RjZDExZTk3MjVlZmU0ODM1ZQ%3D%3D&v=1&f=hd", | |
"https://v.redd.it/3bslm2ha82g91/HLSPlaylist.m3u8?a=1663701635%2CNzBjNzc0YmQwMDUyOWRmMDFhOWExYjE3NjY1ZDczMDg3NTg1YTc5OTIwZGQxYmZiZWY2MjIxYjVmNGUwMDg0Ng%3D%3D&v=1&f=hd", | |
"https://v.redd.it/psbwrn1gdbf91/HLSPlaylist.m3u8?a=1663701637%2CYjMxNDkyOWUyNTQ0OTQwMTdlNjEyZmJjZDliN2E0YjJlOTUwYTUxOTM5NmI2MTM5MWE0ODFjMWZjYzNmNjUwMw%3D%3D&v=1&f=hd", | |
].map { URL(string: $0)! } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment