feat: 完善机巢模拟调试与素材交付交互
增加禁飞期间的设备指令拦截及本地模拟拍摄流程;适配订单素材库和景区空境库,完善素材筛选、预览与下载;订单交付入口改为方式选择弹窗并复用云端上传。补充单元测试、接口记录和模拟器截图。
This commit is contained in:
@@ -1,9 +1,10 @@
|
||||
import Kingfisher
|
||||
import AVFoundation
|
||||
import AVKit
|
||||
import SnapKit
|
||||
import UIKit
|
||||
|
||||
/// 景区空镜独立预览面板,仅播放应用内演示短片,不接入订单或网络素材。
|
||||
/// 景区空境视频预览,正式素材播放服务端地址,Debug 素材保留本地预览。
|
||||
@MainActor
|
||||
final class ScenicMaterialPreviewViewController: UIViewController {
|
||||
private let clip: ScenicMaterialClip
|
||||
@@ -11,10 +12,17 @@ final class ScenicMaterialPreviewViewController: UIViewController {
|
||||
private let capturedAt: Date
|
||||
private let calendar: Calendar
|
||||
private let playerController = AVPlayerViewController()
|
||||
private let contentView = UIView()
|
||||
private let coverView = UIImageView()
|
||||
private let playButton = UIButton(type: .system)
|
||||
private let playbackStatus = ScenicMaterialsStyle.label("本地演示短片 · 非真实景区视频", size: 11, color: ScenicMaterialsStyle.secondary)
|
||||
private let playbackStatus = ScenicMaterialsStyle.label(size: 13, color: ScenicMaterialsStyle.secondary)
|
||||
private let playbackTime = ScenicMaterialsStyle.label(size: 11, color: ScenicMaterialsStyle.secondary)
|
||||
private let progressView = UIProgressView(progressViewStyle: .bar)
|
||||
private var playbackTask: Task<Void, Never>?
|
||||
private var playbackObservation: ScenicMaterialPlaybackObservation?
|
||||
private var playbackGeneration = UUID()
|
||||
private var playbackDuration: Double?
|
||||
private var playbackFinished = false
|
||||
|
||||
/// 预览指定片段,原片元信息与本地演示视频时长明确区分。
|
||||
init(clip: ScenicMaterialClip, routeName: String, capturedAt: Date, calendar: Calendar = .current) {
|
||||
@@ -32,9 +40,16 @@ final class ScenicMaterialPreviewViewController: UIViewController {
|
||||
override func viewDidLoad() {
|
||||
super.viewDidLoad()
|
||||
setupUI()
|
||||
sheetPresentationController?.detents = [.custom { min($0.maximumDetentValue, 510) }]
|
||||
sheetPresentationController?.detents = [.custom { [weak self] context in
|
||||
guard let self else { return min(context.maximumDetentValue, 490) }
|
||||
let width = max(self.view.bounds.width, 320)
|
||||
let fittingSize = self.contentView.systemLayoutSizeFitting(
|
||||
CGSize(width: width, height: UIView.layoutFittingCompressedSize.height),
|
||||
withHorizontalFittingPriority: .required, verticalFittingPriority: .fittingSizeLevel)
|
||||
return min(context.maximumDetentValue, fittingSize.height)
|
||||
}]
|
||||
sheetPresentationController?.prefersGrabberVisible = true
|
||||
sheetPresentationController?.preferredCornerRadius = 24
|
||||
sheetPresentationController?.preferredCornerRadius = 22
|
||||
NotificationCenter.default.addObserver(self, selector: #selector(pausePlayback),
|
||||
name: UIApplication.willResignActiveNotification, object: nil)
|
||||
}
|
||||
@@ -42,37 +57,51 @@ final class ScenicMaterialPreviewViewController: UIViewController {
|
||||
override func viewWillDisappear(_ animated: Bool) {
|
||||
super.viewWillDisappear(animated)
|
||||
pausePlayback()
|
||||
playbackTask?.cancel()
|
||||
playbackTask = nil
|
||||
}
|
||||
|
||||
override func viewDidDisappear(_ animated: Bool) {
|
||||
super.viewDidDisappear(animated)
|
||||
stopPlayback()
|
||||
}
|
||||
|
||||
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 title = ScenicMaterialsStyle.label("视频预览", size: 18, weight: .semibold)
|
||||
let mediaContainer = UIView()
|
||||
mediaContainer.backgroundColor = .black
|
||||
mediaContainer.layer.cornerRadius = 12
|
||||
mediaContainer.layer.cornerRadius = 10
|
||||
mediaContainer.clipsToBounds = true
|
||||
coverView.image = UIImage(named: clip.coverAssetName)
|
||||
mediaContainer.accessibilityIdentifier = "scenic.materials.preview.media"
|
||||
if clip.isRemote {
|
||||
coverView.kf.setImage(with: clip.coverURL, placeholder: UIImage(systemName: "photo"))
|
||||
playbackStatus.text = "点击播放原片"
|
||||
} else {
|
||||
coverView.image = UIImage(named: clip.coverAssetName)
|
||||
playbackStatus.text = "本地演示短片 · 非真实景区视频"
|
||||
}
|
||||
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)
|
||||
updatePlayButton(isPlaying: false)
|
||||
playbackStatus.numberOfLines = 2
|
||||
let metadata = ScenicMaterialsStyle.label("片段 \(String(format: "%02d", clip.ordinal)) · 原片 \(clip.durationText) · \(clip.resolution) · \(clip.sizeText)", size: 12, color: ScenicMaterialsStyle.secondary)
|
||||
progressView.progressTintColor = ScenicMaterialsStyle.primary
|
||||
progressView.trackTintColor = ScenicMaterialsStyle.border
|
||||
progressView.accessibilityIdentifier = "scenic.materials.preview.progress"
|
||||
progressView.accessibilityLabel = "播放进度"
|
||||
playbackTime.accessibilityIdentifier = "scenic.materials.preview.playbackTime"
|
||||
resetPlaybackTime()
|
||||
let metadata = ScenicMaterialsStyle.label("\(clip.resolution) · \(clip.sizeText)", size: 11, color: ScenicMaterialsStyle.secondary)
|
||||
metadata.numberOfLines = 2
|
||||
metadata.textAlignment = .right
|
||||
metadata.accessibilityIdentifier = "scenic.materials.preview.metadata"
|
||||
let route = ScenicMaterialsStyle.label(routeName, size: 17, weight: .semibold)
|
||||
let timeline = UIStackView(arrangedSubviews: [playbackTime, metadata])
|
||||
timeline.alignment = .top
|
||||
timeline.distribution = .fillEqually
|
||||
timeline.spacing = 8
|
||||
let route = ScenicMaterialsStyle.label(routeName, size: 16, weight: .semibold)
|
||||
route.numberOfLines = 2
|
||||
route.accessibilityIdentifier = "scenic.materials.preview.route"
|
||||
let formatter = DateFormatter()
|
||||
@@ -80,80 +109,192 @@ final class ScenicMaterialPreviewViewController: UIViewController {
|
||||
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 time = ScenicMaterialsStyle.label("\(formatter.string(from: capturedAt)) · 片段 \(clip.ordinal)", size: 12, color: ScenicMaterialsStyle.secondary)
|
||||
time.accessibilityIdentifier = "scenic.materials.preview.capturedAt"
|
||||
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.setTitleColor(.white, for: .normal)
|
||||
done.titleLabel?.font = .systemFont(ofSize: 15, weight: .semibold)
|
||||
done.backgroundColor = ScenicMaterialsStyle.primary
|
||||
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)
|
||||
scroll.addSubview(contentView)
|
||||
[title, playbackStatus, mediaContainer, progressView, timeline, route, time, done].forEach(contentView.addSubview)
|
||||
addChild(playerController)
|
||||
playerController.view.backgroundColor = .black
|
||||
playerController.view.isHidden = true
|
||||
playerController.showsPlaybackControls = false
|
||||
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) }
|
||||
contentView.snp.makeConstraints { $0.edges.equalTo(scroll.contentLayoutGuide); $0.width.equalTo(scroll.frameLayoutGuide) }
|
||||
title.snp.makeConstraints { $0.top.equalToSuperview().offset(24); $0.leading.trailing.equalToSuperview().inset(18) }
|
||||
playbackStatus.snp.makeConstraints { $0.top.equalTo(title.snp.bottom).offset(6); $0.leading.trailing.equalTo(title) }
|
||||
mediaContainer.snp.makeConstraints {
|
||||
$0.top.equalTo(playbackStatus.snp.bottom).offset(14)
|
||||
$0.leading.trailing.equalToSuperview().inset(22)
|
||||
$0.top.equalTo(playbackStatus.snp.bottom).offset(18)
|
||||
$0.leading.trailing.equalTo(title)
|
||||
$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) }
|
||||
playButton.snp.makeConstraints { $0.center.equalToSuperview(); $0.size.equalTo(44) }
|
||||
progressView.snp.makeConstraints { $0.top.equalTo(mediaContainer.snp.bottom).offset(16); $0.leading.trailing.equalTo(mediaContainer); $0.height.equalTo(3) }
|
||||
timeline.snp.makeConstraints { $0.top.equalTo(progressView.snp.bottom).offset(8); $0.leading.trailing.equalTo(mediaContainer) }
|
||||
route.snp.makeConstraints { $0.top.equalTo(timeline.snp.bottom).offset(22); $0.leading.trailing.equalTo(mediaContainer) }
|
||||
time.snp.makeConstraints { $0.top.equalTo(route.snp.bottom).offset(7); $0.leading.trailing.equalTo(mediaContainer) }
|
||||
done.snp.makeConstraints {
|
||||
$0.top.equalTo(time.snp.bottom).offset(18)
|
||||
$0.top.equalTo(time.snp.bottom).offset(20)
|
||||
$0.leading.trailing.equalTo(mediaContainer)
|
||||
$0.height.equalTo(44)
|
||||
$0.bottom.equalToSuperview().inset(14)
|
||||
$0.height.equalTo(46)
|
||||
$0.bottom.equalToSuperview().inset(18)
|
||||
}
|
||||
}
|
||||
|
||||
@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 = "本地演示视频暂不可用"
|
||||
if let player = playerController.player {
|
||||
if player.rate > 0 {
|
||||
pausePlayback()
|
||||
} else {
|
||||
if playbackFinished { player.seek(to: .zero); playbackFinished = false }
|
||||
player.play()
|
||||
updatePlayButton(isPlaying: true)
|
||||
updatePlayingStatus()
|
||||
}
|
||||
return
|
||||
}
|
||||
let demoURL = Bundle.main.url(forResource: "drone_material_demo", withExtension: "mp4")
|
||||
?? Bundle.main.url(forResource: "drone_material_demo", withExtension: "mp4", subdirectory: "DroneDock")
|
||||
guard let url = clip.isRemote ? clip.remoteURL : demoURL else {
|
||||
playbackStatus.text = "视频地址暂不可用"
|
||||
return
|
||||
}
|
||||
playButton.isEnabled = false
|
||||
playbackGeneration = UUID()
|
||||
let generation = playbackGeneration
|
||||
playbackTask = Task { @MainActor [weak self] in
|
||||
let playable = (try? await AVURLAsset(url: url).load(.isPlayable)) == true
|
||||
guard let self else { return }
|
||||
let asset = AVURLAsset(url: url)
|
||||
let playable = (try? await asset.load(.isPlayable)) == true
|
||||
let duration = try? await asset.load(.duration)
|
||||
guard let self, self.playbackGeneration == generation else { return }
|
||||
self.playbackTask = nil
|
||||
self.playButton.isEnabled = true
|
||||
guard !Task.isCancelled, self.view.window != nil else { return }
|
||||
guard playable else {
|
||||
self.playbackStatus.text = "本地演示视频暂不可播放"
|
||||
self.playbackStatus.text = "视频暂不可播放,请稍后重试"
|
||||
return
|
||||
}
|
||||
self.playerController.player = AVPlayer(url: url)
|
||||
let seconds = duration?.seconds ?? .nan
|
||||
self.playbackDuration = seconds.isFinite && seconds > 0 ? seconds : nil
|
||||
let player = AVPlayer(playerItem: AVPlayerItem(asset: asset))
|
||||
self.playerController.player = player
|
||||
self.playerController.view.isHidden = false
|
||||
self.coverView.isHidden = true
|
||||
self.playButton.isHidden = true
|
||||
self.playbackStatus.text = "正在播放 3 秒本地演示 · 原片时长见下方"
|
||||
self.playerController.player?.play()
|
||||
self.playbackFinished = false
|
||||
self.playbackObservation = ScenicMaterialPlaybackObservation(player: player) { [weak self] time in
|
||||
Task { @MainActor [weak self] in
|
||||
guard let self, self.playbackGeneration == generation else { return }
|
||||
self.updatePlaybackProgress(time)
|
||||
}
|
||||
}
|
||||
NotificationCenter.default.addObserver(self, selector: #selector(playbackEnded),
|
||||
name: .AVPlayerItemDidPlayToEndTime, object: player.currentItem)
|
||||
self.updatePlaybackProgress(.zero)
|
||||
self.updatePlayingStatus()
|
||||
self.updatePlayButton(isPlaying: true)
|
||||
player.play()
|
||||
}
|
||||
}
|
||||
|
||||
@objc private func pausePlayback() { playerController.player?.pause() }
|
||||
@objc private func closeTapped() { pausePlayback(); dismiss(animated: true) }
|
||||
private func updatePlaybackProgress(_ time: CMTime) {
|
||||
let seconds = time.seconds.isFinite ? max(time.seconds, 0) : 0
|
||||
progressView.progress = playbackDuration.map { Float(min(seconds / $0, 1)) } ?? 0
|
||||
let total = playbackDuration.map(Self.durationText) ?? "时长未知"
|
||||
playbackTime.text = "\(Self.durationText(seconds)) / \(total)"
|
||||
}
|
||||
|
||||
private func updatePlayingStatus() {
|
||||
if clip.isRemote {
|
||||
playbackStatus.text = "正在播放原片"
|
||||
} else if let duration = playbackDuration {
|
||||
playbackStatus.text = "正在播放 \(Int(duration.rounded())) 秒本地演示 · 原片 \(clip.durationText)"
|
||||
} else {
|
||||
playbackStatus.text = "正在播放本地演示 · 原片 \(clip.durationText)"
|
||||
}
|
||||
}
|
||||
|
||||
private func updatePlayButton(isPlaying: Bool) {
|
||||
let symbol = isPlaying ? "pause.fill" : "play.fill"
|
||||
playButton.setImage(UIImage(systemName: symbol, withConfiguration: UIImage.SymbolConfiguration(pointSize: 24)), for: .normal)
|
||||
playButton.accessibilityLabel = isPlaying ? "暂停播放" : (clip.isRemote ? "播放景区视频" : "播放本地演示视频")
|
||||
}
|
||||
|
||||
private func resetPlaybackTime() {
|
||||
playbackTime.text = "00:00 / \(clip.isRemote ? "" : "原片 ")\(clip.durationText)"
|
||||
}
|
||||
|
||||
private static func durationText(_ seconds: Double) -> String {
|
||||
let value = max(Int(seconds), 0)
|
||||
return String(format: "%02d:%02d", value / 60, value % 60)
|
||||
}
|
||||
|
||||
@objc private func playbackEnded() {
|
||||
playbackFinished = true
|
||||
if let duration = playbackDuration { updatePlaybackProgress(CMTime(seconds: duration, preferredTimescale: 600)) }
|
||||
updatePlayButton(isPlaying: false)
|
||||
playbackStatus.text = clip.isRemote ? "播放结束,点击重新播放" : "本地演示已结束 · 原片 \(clip.durationText)"
|
||||
}
|
||||
|
||||
@objc private func pausePlayback() {
|
||||
playerController.player?.pause()
|
||||
if playbackTask != nil {
|
||||
playbackGeneration = UUID()
|
||||
playbackTask?.cancel()
|
||||
playbackTask = nil
|
||||
playButton.isEnabled = true
|
||||
}
|
||||
updatePlayButton(isPlaying: false)
|
||||
if playerController.player != nil {
|
||||
playbackStatus.text = clip.isRemote ? "已暂停,点击继续播放" : "本地演示已暂停 · 原片 \(clip.durationText)"
|
||||
}
|
||||
}
|
||||
|
||||
private func stopPlayback() {
|
||||
pausePlayback()
|
||||
playbackGeneration = UUID()
|
||||
NotificationCenter.default.removeObserver(self, name: .AVPlayerItemDidPlayToEndTime, object: playerController.player?.currentItem)
|
||||
playbackObservation = nil
|
||||
playerController.player = nil
|
||||
playerController.view.isHidden = true
|
||||
coverView.isHidden = false
|
||||
playbackDuration = nil
|
||||
playbackFinished = false
|
||||
progressView.progress = 0
|
||||
resetPlaybackTime()
|
||||
}
|
||||
|
||||
@objc private func closeTapped() {
|
||||
stopPlayback()
|
||||
dismiss(animated: true)
|
||||
}
|
||||
}
|
||||
|
||||
/// 与播放器成对持有周期观察令牌,释放时移除观察,避免弹窗销毁后残留定时回调。
|
||||
private final class ScenicMaterialPlaybackObservation {
|
||||
private let player: AVPlayer
|
||||
private let token: Any
|
||||
|
||||
init(player: AVPlayer, onTime: @escaping @Sendable (CMTime) -> Void) {
|
||||
self.player = player
|
||||
token = player.addPeriodicTimeObserver(forInterval: CMTime(seconds: 0.1, preferredTimescale: 600),
|
||||
queue: .main, using: onTime)
|
||||
}
|
||||
|
||||
deinit { player.removeTimeObserver(token) }
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user