Skip to content

Commit a1adc5a

Browse files
author
Chris Hu
committed
DemoVideoCapture: 添加剩余播放时间
1 parent fdd46a7 commit a1adc5a

File tree

2 files changed

+51
-6
lines changed

2 files changed

+51
-6
lines changed

DemoVideoCapture/DemoVideoCapture/Base.lproj/Main.storyboard

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -152,9 +152,15 @@
152152
<rect key="frame" x="0.0" y="0.0" width="375" height="200"/>
153153
<subviews>
154154
<progressView opaque="NO" contentMode="scaleToFill" verticalHuggingPriority="750" translatesAutoresizingMaskIntoConstraints="NO" id="bX3-zE-3CQ">
155-
<rect key="frame" x="20" y="178" width="335" height="2"/>
155+
<rect key="frame" x="20" y="178" width="275" height="2"/>
156156
<color key="progressTintColor" red="0.99215686274509807" green="0.40784313725490196" blue="0.60784313725490191" alpha="1" colorSpace="calibratedRGB"/>
157157
</progressView>
158+
<label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" text="00:00" textAlignment="center" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="x5Q-SK-hsG">
159+
<rect key="frame" x="305" y="169" width="60" height="21"/>
160+
<fontDescription key="fontDescription" type="system" pointSize="17"/>
161+
<color key="textColor" red="0.99215686270000003" green="0.40784313729999999" blue="0.60784313729999995" alpha="1" colorSpace="calibratedRGB"/>
162+
<nil key="highlightedColor"/>
163+
</label>
158164
<button opaque="NO" contentMode="scaleToFill" contentHorizontalAlignment="center" contentVerticalAlignment="center" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="cg5-R2-rJQ">
159165
<rect key="frame" x="137.5" y="50" width="100" height="100"/>
160166
<constraints>
@@ -195,14 +201,17 @@
195201
</subviews>
196202
<color key="backgroundColor" white="0.0" alpha="0.0" colorSpace="calibratedWhite"/>
197203
<constraints>
198-
<constraint firstAttribute="trailing" secondItem="bX3-zE-3CQ" secondAttribute="trailing" constant="20" id="1Ym-gU-Nx0"/>
204+
<constraint firstAttribute="trailing" secondItem="bX3-zE-3CQ" secondAttribute="trailing" constant="80" id="1Ym-gU-Nx0"/>
205+
<constraint firstItem="x5Q-SK-hsG" firstAttribute="leading" secondItem="bX3-zE-3CQ" secondAttribute="trailing" constant="10" id="AiL-EK-oj2"/>
199206
<constraint firstItem="cg5-R2-rJQ" firstAttribute="centerY" secondItem="dBT-5A-Ibe" secondAttribute="centerY" id="IDJ-a7-7bN"/>
200207
<constraint firstItem="jML-jp-ueV" firstAttribute="centerY" secondItem="cg5-R2-rJQ" secondAttribute="centerY" id="Ri0-5p-HWr"/>
201208
<constraint firstItem="Vkp-EM-c9U" firstAttribute="centerY" secondItem="cg5-R2-rJQ" secondAttribute="centerY" id="Z0Y-yo-jb7"/>
202209
<constraint firstItem="cg5-R2-rJQ" firstAttribute="centerX" secondItem="dBT-5A-Ibe" secondAttribute="centerX" id="aeQ-Us-9X5"/>
203210
<constraint firstItem="Vkp-EM-c9U" firstAttribute="leading" secondItem="cg5-R2-rJQ" secondAttribute="trailing" constant="20" id="cW3-iN-9gk"/>
204211
<constraint firstItem="jML-jp-ueV" firstAttribute="trailing" secondItem="cg5-R2-rJQ" secondAttribute="leading" constant="-20" id="ed2-gR-Tvi"/>
205212
<constraint firstItem="bX3-zE-3CQ" firstAttribute="leading" secondItem="dBT-5A-Ibe" secondAttribute="leading" constant="20" id="lP2-CI-aas"/>
213+
<constraint firstItem="x5Q-SK-hsG" firstAttribute="centerY" secondItem="bX3-zE-3CQ" secondAttribute="centerY" id="mbd-Hf-ooq"/>
214+
<constraint firstAttribute="trailing" secondItem="x5Q-SK-hsG" secondAttribute="trailing" constant="10" id="nKT-gN-AYd"/>
206215
<constraint firstAttribute="bottom" secondItem="bX3-zE-3CQ" secondAttribute="bottom" constant="20" id="w3K-S4-ZP5"/>
207216
</constraints>
208217
</view>
@@ -239,6 +248,7 @@
239248
<outlet property="btnNextVideoNetwork" destination="Vkp-EM-c9U" id="YiQ-7q-LH9"/>
240249
<outlet property="btnPlayVideoCapture" destination="1n5-G9-3WC" id="9n3-tR-dg3"/>
241250
<outlet property="btnPlayVideoNetwork" destination="cg5-R2-rJQ" id="XrY-Vi-FNf"/>
251+
<outlet property="lbProgress" destination="x5Q-SK-hsG" id="PUI-Fn-DXb"/>
242252
<outlet property="progressViewVideoNetwork" destination="bX3-zE-3CQ" id="tLU-Cj-Pem"/>
243253
<outlet property="viewVideoCapture" destination="aJ0-yc-234" id="Owh-m9-2XM"/>
244254
<outlet property="viewVideoNetwork" destination="1xc-Ij-p8A" id="3Gi-0n-hvW"/>

DemoVideoCapture/DemoVideoCapture/VideoPlayViewController.swift

Lines changed: 39 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ class VideoPlayViewController: UIViewController {
4848

4949
// 进度条
5050
@IBOutlet weak var progressViewVideoNetwork: UIProgressView!
51+
@IBOutlet weak var lbProgress: UILabel!
5152

5253

5354
override func viewDidLoad() {
@@ -56,6 +57,14 @@ class VideoPlayViewController: UIViewController {
5657

5758
override func viewWillAppear(_ animated: Bool) {
5859
super.viewWillAppear(animated)
60+
61+
NotificationCenter.default.addObserver(self, selector: #selector(VideoPlayViewController.actionPlayVideoNetworkDone(_:)), name: NSNotification.Name.AVPlayerItemDidPlayToEndTime, object: nil)
62+
}
63+
64+
override func viewWillDisappear(_ animated: Bool) {
65+
super.viewWillDisappear(animated)
66+
67+
NotificationCenter.default.removeObserver(self, name: NSNotification.Name.AVPlayerItemDidPlayToEndTime, object: nil)
5968
}
6069

6170
override func viewDidAppear(_ animated: Bool) {
@@ -103,6 +112,25 @@ class VideoPlayViewController: UIViewController {
103112
// }
104113
}
105114

115+
func timeString(_ time: Int) -> String {
116+
let hour = time / 3600
117+
let min = time / 60
118+
let sec = time % 60
119+
120+
var minStr = ""
121+
var secStr = ""
122+
minStr = min > 9 ? "\(min)" : "0\(min)"
123+
secStr = sec > 9 ? "\(sec)" : "0\(sec)"
124+
125+
if hour == 0 {
126+
return "\(minStr):\(secStr)"
127+
} else {
128+
var hourStr = ""
129+
hourStr = hour > 9 ? "\(hour)" : "0\(hour)"
130+
return "\(hourStr):\(minStr):\(secStr)"
131+
}
132+
}
133+
106134
private func actionPlayVideoNetwork() {
107135
isVideoPlaying = !isVideoPlaying
108136

@@ -119,14 +147,16 @@ class VideoPlayViewController: UIViewController {
119147
// 监听播放进度
120148
avPlayer.addPeriodicTimeObserver(forInterval: CMTime(value: 1, timescale: 1), queue: DispatchQueue.main, using: { (time) in
121149

122-
let total = Float(CMTimeGetSeconds(avPlayerItem.duration))
123-
let current = Float(CMTimeGetSeconds(time))
150+
let total = lroundf(Float(CMTimeGetSeconds(avPlayerItem.duration)))
151+
let current = lroundf(Float(CMTimeGetSeconds(time)))
124152
if current > 0 {
125-
self.progressViewVideoNetwork.progress = current / total
153+
self.progressViewVideoNetwork.progress = Float(current) / Float(total)
154+
155+
self.lbProgress.text = self.timeString(total - current)
126156
}
127157

158+
// 也可以使用AVPlayerItemDidPlayToEndTime通知
128159
if self.progressViewVideoNetwork.progress == 1 {
129-
print("Done")
130160
self.btnPlayVideoNetwork.setImage(UIImage(named: "btnPlay"), for: .normal)
131161
self.progressViewVideoNetwork.progress = 0
132162
}
@@ -197,7 +227,12 @@ class VideoPlayViewController: UIViewController {
197227

198228
btnPlayVideoNetwork.isHidden = isOperationShowing
199229
progressViewVideoNetwork.isHidden = isOperationShowing
230+
lbProgress.isHidden = isOperationShowing
200231
}
201232
}
233+
234+
func actionPlayVideoNetworkDone(_ sender: Notification) {
235+
print("Done")
236+
}
202237

203238
}

0 commit comments

Comments
 (0)