-
Notifications
You must be signed in to change notification settings - Fork 95
/
EZPlayerControlView.swift
478 lines (393 loc) · 17.3 KB
/
EZPlayerControlView.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
//
// EZPlayerControlView.swift
// EZPlayer
//
// Created by yangjun zhu on 2016/12/28.
// Copyright © 2016年 yangjun zhu. All rights reserved.
//
import UIKit
import AVFoundation
import MediaPlayer
import AVKit
open class EZPlayerControlView: UIView{
weak public var player: EZPlayer?{
didSet{
player?.setControlsHidden(false, animated: true)
self.autohideControlView()
}
}
// open var tapGesture: UITapGestureRecognizer!
var hideControlViewTask: Task?
public var autohidedControlViews = [UIView]()
// var controlsHidden = false
@IBOutlet weak var navBarContainer: UIView!
@IBOutlet weak var navBarContainerTopConstraint: NSLayoutConstraint!
@IBOutlet weak var ToolBarContainerBottomConstraint: NSLayoutConstraint!
@IBOutlet weak var toolBarContainer: UIView!
@IBOutlet weak var safeAreaBottomView: UIView!
@IBOutlet weak var timeLabel: UILabel!
@IBOutlet weak var progressView: UIProgressView!
@IBOutlet weak var titleLabel: UILabel!
@IBOutlet weak var playPauseButton: UIButton!
@IBOutlet weak var fullEmbeddedScreenButton: UIButton!
@IBOutlet weak var fullEmbeddedScreenButtonWidthConstraint: NSLayoutConstraint!
@IBOutlet weak var timeSlider: UISlider!
@IBOutlet weak var videoshotPreview: UIView!
@IBOutlet weak var videoshotPreviewLeadingConstraint: NSLayoutConstraint!
@IBOutlet weak var videoshotImageView: UIImageView!
@IBOutlet weak var loading: EZPlayerLoading!
@IBOutlet weak var audioSubtitleCCButtonWidthConstraint: NSLayoutConstraint!
@IBOutlet weak var pipButtonWidthConstraint: NSLayoutConstraint!
@IBOutlet weak var airplayContainer: UIView!
// MARK: - Life cycle
deinit {
}
override open func awakeFromNib() {
super.awakeFromNib()
self.timeSlider.value = 0
self.progressView.progress = 0
self.progressView.progressTintColor = UIColor.lightGray
self.progressView.trackTintColor = UIColor.clear
self.progressView.backgroundColor = UIColor.clear
self.videoshotPreview.isHidden = true
self.audioSubtitleCCButtonWidthConstraint.constant = 0
self.pipButtonWidthConstraint.constant = 0
self.autohidedControlViews = [self.navBarContainer,self.toolBarContainer,self.safeAreaBottomView]
// self.tapGesture = UITapGestureRecognizer(target: self, action: #selector(self.tapGestureTapped(_:)))
// self.tapGesture.delegate = self
// self.addGestureRecognizer(self.tapGesture)
let airplayImage = UIImage(named: "btn_airplay", in: Bundle(for: EZPlayerControlView.self),compatibleWith: nil)
let airplayView = MPVolumeView(frame: self.airplayContainer.bounds)
airplayView.showsVolumeSlider = false
airplayView.showsRouteButton = true
airplayView.setRouteButtonImage(airplayImage, for: .normal)
self.airplayContainer.addSubview(airplayView)
// self.loading.start()
}
// MARK: - EZPlayerCustomControlView
fileprivate var isProgressSliderSliding = false {
didSet{
if !(self.player?.isM3U8 ?? true) {
// self.videoshotPreview.isHidden = !isProgressSliderSliding
}
}
}
@IBAction func progressSliderTouchBegan(_ sender: Any) {
guard let player = self.player else {
return
}
self.player(player, progressWillChange: TimeInterval(self.timeSlider.value))
}
@IBAction func progressSliderValueChanged(_ sender: Any) {
guard let player = self.player else {
return
}
self.player(player, progressChanging: TimeInterval(self.timeSlider.value))
}
@IBAction func progressSliderTouchEnd(_ sender: Any) {
self.videoshotPreview.isHidden = true
guard let player = self.player else {
return
}
self.player(player, progressDidChange: TimeInterval(self.timeSlider.value))
}
// public func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldReceive touch: UITouch) -> Bool{
// self.autohideControlView()
// return !self.autohidedControlViews.contains(touch.view!) && !self.autohidedControlViews.contains(touch.view!.superview!)
// // return true
// }
//
// MARK: - private
// @objc fileprivate func tapGestureTapped(_ sender: UIGestureRecognizer) {
// guard let player = self.player else {
// return
// }
// player.controlsHidden = !player.controlsHidden
// }
fileprivate func hideControlView(_ animated: Bool) {
// if self.controlsHidden == true{
// return
// }
if animated{
UIView.setAnimationsEnabled(false)
UIView.animate(withDuration: EZPlayerAnimatedDuration, delay: 0,options: .curveEaseInOut, animations: {
self.autohidedControlViews.forEach{
$0.alpha = 0
}
}, completion: {finished in
self.autohidedControlViews.forEach{
$0.isHidden = true
}
UIView.setAnimationsEnabled(true)
})
}else{
self.autohidedControlViews.forEach{
$0.alpha = 0
$0.isHidden = true
}
}
}
fileprivate func showControlView(_ animated: Bool) {
// if self.controlsHidden == false{
// return
// }
if animated{
UIView.setAnimationsEnabled(false)
self.autohidedControlViews.forEach{
$0.isHidden = false
}
UIView.animate(withDuration: EZPlayerAnimatedDuration, delay: 0,options: .curveEaseInOut, animations: {
if self.player?.displayMode == .fullscreen{
self.navBarContainerTopConstraint.constant = ((EZPlayerUtils.hasSafeArea || (ProcessInfo.processInfo.operatingSystemVersion.majorVersion >= 13)) && self.player?.fullScreenMode == .landscape) ? 20 : EZPlayerUtils.statusBarHeight
self.ToolBarContainerBottomConstraint.constant = EZPlayerUtils.hasSafeArea ? self.player?.fullScreenMode == .portrait ? 34 : 21 : 0
}else{
self.navBarContainerTopConstraint.constant = 0
self.ToolBarContainerBottomConstraint.constant = 0
}
self.autohidedControlViews.forEach{
$0.alpha = 1
}
}, completion: {finished in
self.autohideControlView()
UIView.setAnimationsEnabled(true)
})
}else{
self.autohidedControlViews.forEach{
$0.isHidden = false
$0.alpha = 1
}
if self.player?.displayMode == .fullscreen{
self.navBarContainerTopConstraint.constant = ((EZPlayerUtils.hasSafeArea || (ProcessInfo.processInfo.operatingSystemVersion.majorVersion >= 13)) && self.player?.fullScreenMode == .landscape) ? 20 : EZPlayerUtils.statusBarHeight
self.ToolBarContainerBottomConstraint.constant = EZPlayerUtils.hasSafeArea ? self.player?.fullScreenMode == .portrait ? 34 : 21 : 0
}else{
self.navBarContainerTopConstraint.constant = 0
}
self.autohideControlView()
}
}
fileprivate func autohideControlView(){
guard let player = self.player , player.autohiddenTimeInterval > 0 else {
return
}
cancel(self.hideControlViewTask)
self.hideControlViewTask = delay(5, task: { [weak self] in
guard let weakSelf = self else {
return
}
// weakSelf.hideControlView()
weakSelf.player?.setControlsHidden(true, animated: true)
})
}
fileprivate func showThumbnail(){
guard let player = self.player else {
return
}
if !player.isM3U8 {
self.videoshotPreview.isHidden = false
player.generateThumbnails(times: [ TimeInterval(self.timeSlider.value)],maximumSize:CGSize(width: self.videoshotImageView.bounds.size.width, height: self.videoshotImageView.bounds.size.height)) { (thumbnails) in
let trackRect = self.timeSlider.convert(self.timeSlider.bounds, to: nil)
let thumbRect = self.timeSlider.thumbRect(forBounds: self.timeSlider.bounds, trackRect: trackRect, value: self.timeSlider.value)
var lead = thumbRect.origin.x + thumbRect.size.width/2 - self.videoshotPreview.bounds.size.width/2
if lead < 0 {
lead = 0
}else if lead + self.videoshotPreview.bounds.size.width > player.view.bounds.width {
lead = player.view.bounds.width - self.videoshotPreview.bounds.size.width
}
self.videoshotPreviewLeadingConstraint.constant = lead
if thumbnails.count > 0 {
let thumbnail = thumbnails[0]
if thumbnail.result == .succeeded {
self.videoshotImageView.image = thumbnail.image
}
}
}
}
}
}
extension EZPlayerControlView: EZPlayerCustom {
// MARK: - EZPlayerCustomAction
@IBAction public func playPauseButtonPressed(_ sender: Any) {
guard let player = self.player else {
return
}
if player.isPlaying {
player.pause()
}else{
player.play()
}
}
@IBAction public func fullEmbeddedScreenButtonPressed(_ sender: Any) {
guard let player = self.player else {
return
}
switch player.displayMode {
case .embedded:
player.toFull()
case .fullscreen:
if player.lastDisplayMode == .embedded{
player.toEmbedded()
}else if player.lastDisplayMode == .float{
player.toFloat()
}
default:
break
}
}
@IBAction public func audioSubtitleCCButtonPressed(_ sender: Any) {
guard let player = self.player else {
return
}
let audibleLegibleViewController = EZPlayerAudibleLegibleViewController(nibName: String(describing: EZPlayerAudibleLegibleViewController.self),bundle: Bundle(for: EZPlayerAudibleLegibleViewController.self),player:player, sourceView:sender as? UIView)
EZPlayerUtils.viewController(from: self)?.present(audibleLegibleViewController, animated: true, completion: {
})
}
@IBAction public func backButtonPressed(_ sender: Any) {
guard let player = self.player else {
return
}
// let floatModelSupported = EZPlayerUtils.floatModelSupported(player)
let displayMode = player.displayMode
if displayMode == .fullscreen {
if player.lastDisplayMode == .embedded{
player.toEmbedded()
}else if player.lastDisplayMode == .float{
player.toFloat()
}
}
// else if(displayMode == .float){
//
// }
player.backButtonBlock?(displayMode)
}
@IBAction public func pipButtonPressed(_ sender: Any) {
guard let player = self.player else {
return
}
player.toFloat()
}
// MARK: - EZPlayerGestureRecognizer
public func player(_ player: EZPlayer, singleTapGestureTapped singleTap: UITapGestureRecognizer) {
player.setControlsHidden(!player.controlsHidden, animated: true)
}
public func player(_ player: EZPlayer, doubleTapGestureTapped doubleTap: UITapGestureRecognizer) {
self.playPauseButtonPressed(doubleTap)
}
// MARK: - EZPlayerHorizontalPan
public func player(_ player: EZPlayer, progressWillChange value: TimeInterval) {
if player.isLive ?? true{
return
}
self.showControlView(true)
cancel(self.hideControlViewTask)
self.isProgressSliderSliding = true
}
public func player(_ player: EZPlayer, progressChanging value: TimeInterval) {
if player.isLive ?? true{
return
}
self.timeLabel.text = EZPlayerUtils.formatTime(position: value, duration: self.player?.duration ?? 0)
if !self.timeSlider.isTracking {
self.timeSlider.value = Float(value)
}
self.showThumbnail()
}
public func player(_ player: EZPlayer, progressDidChange value: TimeInterval) {
if player.isLive ?? true{
return
}
self.autohideControlView()
// self.isProgressSliderSliding = false
self.player?.seek(to: value, completionHandler: { (isFinished) in
self.isProgressSliderSliding = false
})
}
// MARK: - EZPlayerDelegate
public func playerHeartbeat(_ player: EZPlayer) {
if let asset = player.playerasset, let playerIntem = player.playerItem ,playerIntem.status == .readyToPlay{
if asset.audios != nil || asset.subtitles != nil || asset.closedCaption != nil{
self.audioSubtitleCCButtonWidthConstraint.constant = 50
}else{
self.audioSubtitleCCButtonWidthConstraint.constant = 0
}
}
self.airplayContainer.isHidden = !player.allowsExternalPlayback
self.pipButtonWidthConstraint.constant = (player.scrollView != nil || player.floatMode == .none || player.displayMode == .float || player.displayMode == .none) ? 0 : 50
}
public func player(_ player: EZPlayer, playerDisplayModeDidChange displayMode: EZPlayerDisplayMode) {
switch displayMode {
case .none:
break
case .embedded:
self.fullEmbeddedScreenButtonWidthConstraint.constant = 50
self.fullEmbeddedScreenButton.setImage(UIImage(named: "btn_fullscreen22x22", in: Bundle(for: EZPlayerControlView.self), compatibleWith: nil), for: .normal)
self.pipButtonWidthConstraint.constant = (player.scrollView != nil || player.floatMode == .none ) ? 0 : 50
case .fullscreen:
self.fullEmbeddedScreenButtonWidthConstraint.constant = 50
self.fullEmbeddedScreenButton.setImage(UIImage(named: "btn_normalscreen22x22", in: Bundle(for: EZPlayerControlView.self), compatibleWith: nil), for: .normal)
if player.lastDisplayMode == .none{
self.fullEmbeddedScreenButtonWidthConstraint.constant = 0
}
self.pipButtonWidthConstraint.constant = (player.scrollView != nil || player.floatMode == .none) ? 0 : 50
case .float:
self.fullEmbeddedScreenButtonWidthConstraint.constant = 0
self.pipButtonWidthConstraint.constant = 0
break
}
}
public func player(_ player: EZPlayer, playerStateDidChange state: EZPlayerState) {
//播放器按钮状态
switch state {
case .playing ,.buffering:
//播放状态
// self.playPauseButton.isSelected = true //暂停按钮
self.playPauseButton.setImage(UIImage(named: "btn_pause22x22", in: Bundle(for: EZPlayerControlView.self), compatibleWith: nil), for: .normal)
case .seekingBackward ,.seekingForward:
break
default:
// self.playPauseButton.isSelected = false // 播放按钮
self.playPauseButton.setImage(UIImage(named: "btn_play22x22", in: Bundle(for: EZPlayerControlView.self), compatibleWith: nil), for: .normal)
}
// switch state {
// case .playing ,.pause,.seekingForward,.seekingBackward,.stopped,.bufferFinished:
// self.loading.stop()
// break
// default:
// self.loading.start()
// break
// }
}
public func player(_ player: EZPlayer, bufferDurationDidChange bufferDuration: TimeInterval, totalDuration: TimeInterval) {
if totalDuration.isNaN || bufferDuration.isNaN || totalDuration == 0 || bufferDuration == 0{
self.progressView.progress = 0
}else{
self.progressView.progress = Float(bufferDuration/totalDuration)
}
}
public func player(_ player: EZPlayer, currentTime: TimeInterval, duration: TimeInterval) {
if currentTime.isNaN || (currentTime == 0 && duration.isNaN){
return
}
self.timeSlider.isEnabled = !duration.isNaN
self.timeSlider.minimumValue = 0
self.timeSlider.maximumValue = duration.isNaN ? Float(currentTime) : Float(duration)
self.titleLabel.text = player.contentItem?.title ?? player.playerasset?.title
if !self.isProgressSliderSliding {
self.timeSlider.value = Float(currentTime)
self.timeLabel.text = duration.isNaN ? "Live" : EZPlayerUtils.formatTime(position: currentTime, duration: duration)
}
}
public func player(_ player: EZPlayer, playerControlsHiddenDidChange controlsHidden: Bool, animated: Bool) {
if controlsHidden {
self.hideControlView(animated)
}else{
self.showControlView(animated)
}
}
public func player(_ player: EZPlayer ,showLoading: Bool){
if showLoading {
self.loading.start()
}else{
self.loading.stop()
}
}
}