160 lines
8.5 KiB
Swift
160 lines
8.5 KiB
Swift
import AVFoundation
|
|
import AVKit
|
|
import SnapKit
|
|
import UIKit
|
|
|
|
/// 景区空镜独立预览面板,仅播放应用内演示短片,不接入订单或网络素材。
|
|
@MainActor
|
|
final class ScenicMaterialPreviewViewController: UIViewController {
|
|
private let clip: ScenicMaterialClip
|
|
private let routeName: String
|
|
private let capturedAt: Date
|
|
private let calendar: Calendar
|
|
private let playerController = AVPlayerViewController()
|
|
private let coverView = UIImageView()
|
|
private let playButton = UIButton(type: .system)
|
|
private let playbackStatus = ScenicMaterialsStyle.label("本地演示短片 · 非真实景区视频", size: 11, color: ScenicMaterialsStyle.secondary)
|
|
private var playbackTask: Task<Void, Never>?
|
|
|
|
/// 预览指定片段,原片元信息与本地演示视频时长明确区分。
|
|
init(clip: ScenicMaterialClip, routeName: String, capturedAt: Date, calendar: Calendar = .current) {
|
|
self.clip = clip
|
|
self.routeName = routeName
|
|
self.capturedAt = capturedAt
|
|
self.calendar = calendar
|
|
super.init(nibName: nil, bundle: nil)
|
|
modalPresentationStyle = .pageSheet
|
|
}
|
|
|
|
@available(*, unavailable)
|
|
required init?(coder: NSCoder) { fatalError("init(coder:) has not been implemented") }
|
|
|
|
override func viewDidLoad() {
|
|
super.viewDidLoad()
|
|
setupUI()
|
|
sheetPresentationController?.detents = [.custom { min($0.maximumDetentValue, 510) }]
|
|
sheetPresentationController?.prefersGrabberVisible = true
|
|
sheetPresentationController?.preferredCornerRadius = 24
|
|
NotificationCenter.default.addObserver(self, selector: #selector(pausePlayback),
|
|
name: UIApplication.willResignActiveNotification, object: nil)
|
|
}
|
|
|
|
override func viewWillDisappear(_ animated: Bool) {
|
|
super.viewWillDisappear(animated)
|
|
pausePlayback()
|
|
playbackTask?.cancel()
|
|
playbackTask = nil
|
|
}
|
|
|
|
private func setupUI() {
|
|
view.backgroundColor = .white
|
|
view.accessibilityIdentifier = "scenic.materials.preview.sheet"
|
|
let title = ScenicMaterialsStyle.label("视频预览", size: 19, weight: .semibold)
|
|
let closeButton = UIButton(type: .system)
|
|
closeButton.setImage(UIImage(systemName: "xmark"), for: .normal)
|
|
closeButton.tintColor = ScenicMaterialsStyle.secondary
|
|
closeButton.accessibilityLabel = "关闭景区视频预览"
|
|
closeButton.accessibilityIdentifier = "scenic.materials.preview.close"
|
|
closeButton.addTarget(self, action: #selector(closeTapped), for: .touchUpInside)
|
|
let mediaContainer = UIView()
|
|
mediaContainer.backgroundColor = .black
|
|
mediaContainer.layer.cornerRadius = 12
|
|
mediaContainer.clipsToBounds = true
|
|
coverView.image = UIImage(named: clip.coverAssetName)
|
|
coverView.contentMode = .scaleAspectFill
|
|
coverView.clipsToBounds = true
|
|
playButton.setImage(UIImage(systemName: "play.circle.fill", withConfiguration: UIImage.SymbolConfiguration(pointSize: 48)), for: .normal)
|
|
playButton.tintColor = .white
|
|
playButton.accessibilityLabel = "播放本地演示视频"
|
|
playButton.accessibilityIdentifier = "scenic.materials.preview.play"
|
|
playButton.addTarget(self, action: #selector(playTapped), for: .touchUpInside)
|
|
playbackStatus.numberOfLines = 2
|
|
let metadata = ScenicMaterialsStyle.label("片段 \(String(format: "%02d", clip.ordinal)) · 原片 \(clip.durationText) · \(clip.resolution) · \(clip.sizeText)", size: 12, color: ScenicMaterialsStyle.secondary)
|
|
metadata.numberOfLines = 2
|
|
metadata.accessibilityIdentifier = "scenic.materials.preview.metadata"
|
|
let route = ScenicMaterialsStyle.label(routeName, size: 17, weight: .semibold)
|
|
route.numberOfLines = 2
|
|
route.accessibilityIdentifier = "scenic.materials.preview.route"
|
|
let formatter = DateFormatter()
|
|
formatter.calendar = calendar
|
|
formatter.timeZone = calendar.timeZone
|
|
formatter.locale = Locale(identifier: "zh_CN")
|
|
formatter.dateFormat = "MM-dd HH:mm"
|
|
let time = ScenicMaterialsStyle.label(formatter.string(from: capturedAt), size: 12, color: ScenicMaterialsStyle.secondary)
|
|
let done = UIButton(type: .system)
|
|
done.setTitle("返回素材列表", for: .normal)
|
|
done.setTitleColor(ScenicMaterialsStyle.primary, for: .normal)
|
|
done.titleLabel?.font = .systemFont(ofSize: 14, weight: .medium)
|
|
done.backgroundColor = ScenicMaterialsStyle.primary.withAlphaComponent(0.07)
|
|
done.layer.cornerRadius = 10
|
|
done.accessibilityIdentifier = "scenic.materials.preview.done"
|
|
done.addTarget(self, action: #selector(closeTapped), for: .touchUpInside)
|
|
let scroll = UIScrollView()
|
|
let content = UIView()
|
|
view.addSubview(scroll)
|
|
scroll.addSubview(content)
|
|
[title, closeButton, playbackStatus, mediaContainer, metadata, route, time, done].forEach(content.addSubview)
|
|
addChild(playerController)
|
|
playerController.view.backgroundColor = .black
|
|
playerController.view.isHidden = true
|
|
playerController.allowsPictureInPicturePlayback = false
|
|
playerController.exitsFullScreenWhenPlaybackEnds = true
|
|
mediaContainer.addSubview(playerController.view)
|
|
playerController.didMove(toParent: self)
|
|
mediaContainer.addSubview(coverView)
|
|
mediaContainer.addSubview(playButton)
|
|
scroll.snp.makeConstraints { $0.edges.equalTo(view.safeAreaLayoutGuide) }
|
|
content.snp.makeConstraints { $0.edges.equalTo(scroll.contentLayoutGuide); $0.width.equalTo(scroll.frameLayoutGuide) }
|
|
title.snp.makeConstraints { $0.top.equalToSuperview().offset(20); $0.leading.equalToSuperview().offset(22) }
|
|
closeButton.snp.makeConstraints { $0.trailing.equalToSuperview().inset(10); $0.centerY.equalTo(title); $0.size.equalTo(44) }
|
|
playbackStatus.snp.makeConstraints { $0.top.equalTo(title.snp.bottom).offset(10); $0.leading.trailing.equalToSuperview().inset(22) }
|
|
mediaContainer.snp.makeConstraints {
|
|
$0.top.equalTo(playbackStatus.snp.bottom).offset(14)
|
|
$0.leading.trailing.equalToSuperview().inset(22)
|
|
$0.height.equalTo(mediaContainer.snp.width).multipliedBy(9.0 / 16.0)
|
|
}
|
|
playerController.view.snp.makeConstraints { $0.edges.equalToSuperview() }
|
|
coverView.snp.makeConstraints { $0.edges.equalToSuperview() }
|
|
playButton.snp.makeConstraints { $0.edges.equalToSuperview() }
|
|
metadata.snp.makeConstraints { $0.top.equalTo(mediaContainer.snp.bottom).offset(10); $0.leading.trailing.equalTo(mediaContainer) }
|
|
route.snp.makeConstraints { $0.top.equalTo(metadata.snp.bottom).offset(17); $0.leading.trailing.equalTo(mediaContainer) }
|
|
time.snp.makeConstraints { $0.top.equalTo(route.snp.bottom).offset(8); $0.leading.trailing.equalTo(mediaContainer) }
|
|
done.snp.makeConstraints {
|
|
$0.top.equalTo(time.snp.bottom).offset(18)
|
|
$0.leading.trailing.equalTo(mediaContainer)
|
|
$0.height.equalTo(44)
|
|
$0.bottom.equalToSuperview().inset(14)
|
|
}
|
|
}
|
|
|
|
@objc private func playTapped() {
|
|
guard playbackTask == nil, clip.isAvailable else { return }
|
|
guard let url = Bundle.main.url(forResource: "drone_material_demo", withExtension: "mp4")
|
|
?? Bundle.main.url(forResource: "drone_material_demo", withExtension: "mp4", subdirectory: "DroneDock") else {
|
|
playbackStatus.text = "本地演示视频暂不可用"
|
|
return
|
|
}
|
|
playButton.isEnabled = false
|
|
playbackTask = Task { @MainActor [weak self] in
|
|
let playable = (try? await AVURLAsset(url: url).load(.isPlayable)) == true
|
|
guard let self else { return }
|
|
self.playbackTask = nil
|
|
self.playButton.isEnabled = true
|
|
guard !Task.isCancelled, self.view.window != nil else { return }
|
|
guard playable else {
|
|
self.playbackStatus.text = "本地演示视频暂不可播放"
|
|
return
|
|
}
|
|
self.playerController.player = AVPlayer(url: url)
|
|
self.playerController.view.isHidden = false
|
|
self.coverView.isHidden = true
|
|
self.playButton.isHidden = true
|
|
self.playbackStatus.text = "正在播放 3 秒本地演示 · 原片时长见下方"
|
|
self.playerController.player?.play()
|
|
}
|
|
}
|
|
|
|
@objc private func pausePlayback() { playerController.player?.pause() }
|
|
@objc private func closeTapped() { pausePlayback(); dismiss(animated: true) }
|
|
}
|