168 lines
3.7 KiB
Swift
168 lines
3.7 KiB
Swift
//
|
|
// BRPlayerListCell.swift
|
|
// BeeReel
|
|
//
|
|
// Created by 长沙鸿瑶 on 2025/6/24.
|
|
//
|
|
|
|
import UIKit
|
|
|
|
class BRPlayerListCell: BRCollectionViewCell, BRPlayerProtocol {
|
|
|
|
|
|
weak var viewModel: BRPlayerViewModel? {
|
|
didSet {
|
|
self.controlView.viewModel = viewModel
|
|
}
|
|
}
|
|
|
|
var shortModel: BRShortModel? {
|
|
didSet {
|
|
self.controlView.shortModel = shortModel
|
|
self.player.coverImageView?.br_setImage(url: shortModel?.image_url)
|
|
}
|
|
}
|
|
|
|
var videoInfo: BRVideoInfoModel? {
|
|
didSet {
|
|
self.controlView.videoInfo = videoInfo
|
|
player.setPlayUrl(url: videoInfo?.video_url ?? "")
|
|
}
|
|
}
|
|
|
|
var isCurrent: Bool = false {
|
|
didSet {
|
|
self.controlView.isCurrent = isCurrent
|
|
}
|
|
}
|
|
|
|
var durationTime: TimeInterval {
|
|
return player.duration
|
|
}
|
|
var currentTime: TimeInterval {
|
|
return player.currentTime
|
|
}
|
|
|
|
var rate: Float = 1 {
|
|
didSet {
|
|
self.player.rate = rate
|
|
}
|
|
}
|
|
|
|
func prepare() {
|
|
|
|
}
|
|
|
|
func start() {
|
|
player.start()
|
|
}
|
|
|
|
func pause() {
|
|
player.pause()
|
|
}
|
|
|
|
func stop() {
|
|
player.stop()
|
|
}
|
|
|
|
func replay() {
|
|
player.replay()
|
|
}
|
|
|
|
func seekTo(progress: Float) {
|
|
let duration = self.durationTime
|
|
let time = duration * TimeInterval(progress)
|
|
self.player.seek(toTime: time)
|
|
}
|
|
|
|
func seekTo(time: TimeInterval) {
|
|
self.player.seek(toTime: time)
|
|
}
|
|
|
|
var ControlViewClass: BRPlayerControlView.Type {
|
|
return BRPlayerControlView.self
|
|
}
|
|
|
|
private lazy var player: BRPlayer = {
|
|
let player = BRPlayer(controlView: self.controlView)
|
|
player.playerView = self.playerView
|
|
player.delegate = self
|
|
|
|
return player
|
|
}()
|
|
|
|
private lazy var playerView: UIView = {
|
|
let view = UIView()
|
|
return view
|
|
}()
|
|
|
|
private lazy var controlView: BRPlayerControlView = {
|
|
let view = ControlViewClass.init()
|
|
return view
|
|
}()
|
|
|
|
override init(frame: CGRect) {
|
|
super.init(frame: frame)
|
|
|
|
br_setupUI()
|
|
}
|
|
|
|
@MainActor required init?(coder: NSCoder) {
|
|
fatalError("init(coder:) has not been implemented")
|
|
}
|
|
|
|
}
|
|
|
|
extension BRPlayerListCell {
|
|
|
|
private func br_setupUI() {
|
|
contentView.addSubview(playerView)
|
|
|
|
|
|
playerView.snp.makeConstraints { make in
|
|
make.edges.equalToSuperview()
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
//MARK: -------------- BRPlayerDelegate --------------
|
|
extension BRPlayerListCell: BRPlayerDelegate {
|
|
|
|
func br_playerDidPlayFinish(_ player: BRPlayer) {
|
|
self.viewModel?.playFinish(player: self)
|
|
}
|
|
|
|
func br_playerDurationDidChange(_ player: BRPlayer, duration: TimeInterval) {
|
|
self.controlView.durationTime = duration
|
|
|
|
}
|
|
|
|
func br_playerCurrentTimeDidChange(_ player: BRPlayer, time: TimeInterval) {
|
|
self.controlView.currentTime = time
|
|
|
|
if player.duration <= 0 {
|
|
self.controlView.progress = 0
|
|
} else {
|
|
self.controlView.progress = time / player.duration
|
|
}
|
|
self.viewModel?.playProgressDidChange(time: time)
|
|
}
|
|
|
|
func br_playerInBufferToPlay(_ player: BRPlayer) {
|
|
self.controlView.isLoading = true
|
|
}
|
|
|
|
func br_playerBufferingCompleted(_ player: BRPlayer) {
|
|
self.controlView.isLoading = false
|
|
}
|
|
|
|
func br_playerReadyToPlay(_ player: BRPlayer) {
|
|
let time = TimeInterval(self.videoInfo?.play_seconds ?? 0) / 1000
|
|
if time > 1 {
|
|
self.seekTo(time: time)
|
|
}
|
|
}
|
|
}
|