@@ -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