Skip to content

Commit 21c68e4

Browse files
committed
example: add loading & error state to the CustomPlayerView
1 parent 03d8b0a commit 21c68e4

File tree

1 file changed

+86
-27
lines changed

1 file changed

+86
-27
lines changed

Example/Example/CustomPlayerView.swift

Lines changed: 86 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,39 @@ public class CustomPlayerView: UIStackView {
2424
return playerView
2525
}()
2626

27-
private lazy var playPauseButton = {
28-
let playPauseButton = UIButton()
29-
playPauseButton.setImage(UIImage(systemName: "play"), for: .normal)
30-
playPauseButton.isEnabled = false
31-
return playPauseButton
27+
private lazy var errorView = {
28+
let errorView = UILabel()
29+
errorView.numberOfLines = 5
30+
errorView.font = errorView.font.withSize(18)
31+
errorView.textColor = UIColor.red
32+
return errorView
33+
}()
34+
35+
private lazy var spinnerView = {
36+
let spinnerView = UIActivityIndicatorView(style: .large)
37+
return spinnerView;
38+
}()
39+
40+
private lazy var controlBar = {
41+
let controlBar = UIStackView()
42+
controlBar.distribution = .fill
43+
controlBar.axis = .vertical
44+
controlBar.backgroundColor = UIColor.lightGray.withAlphaComponent(0.2)
45+
return controlBar
46+
}()
47+
48+
private lazy var playButton = {
49+
let playButton = UIButton()
50+
playButton.setImage(UIImage(systemName: "play"), for: .normal)
51+
playButton.addTarget(self, action: #selector(play), for: .touchUpInside)
52+
return playButton
53+
}()
54+
55+
private lazy var pauseButton = {
56+
let pauseButton = UIButton()
57+
pauseButton.setImage(UIImage(systemName: "pause"), for: .normal)
58+
pauseButton.addTarget(self, action: #selector(pause), for: .touchUpInside)
59+
return pauseButton
3260
}()
3361

3462
private lazy var contentTitleView = {
@@ -47,46 +75,64 @@ public class CustomPlayerView: UIStackView {
4775

4876
override init(frame: CGRect) {
4977
super.init(frame: .zero)
50-
commonInit()
78+
setupUI()
5179
}
5280

5381
required init(coder aDecoder: NSCoder) {
5482
super.init(coder: aDecoder)
55-
commonInit()
83+
setupUI()
5684
}
5785

58-
private func commonInit() {
86+
private func setupUI() {
5987
distribution = .fill
6088
axis = .vertical
6189

6290
playerView.translatesAutoresizingMaskIntoConstraints = false
6391
addArrangedSubview(playerView)
6492

65-
let controlBar = UIView()
93+
spinnerView.isHidden = false;
94+
spinnerView.translatesAutoresizingMaskIntoConstraints = false
95+
addArrangedSubview(spinnerView)
96+
spinnerView.heightAnchor.constraint(equalToConstant: 60).isActive = true
97+
spinnerView.startAnimating()
98+
99+
errorView.isHidden = true
100+
errorView.translatesAutoresizingMaskIntoConstraints = false
101+
addArrangedSubview(errorView)
102+
103+
controlBar.isHidden = true
66104
controlBar.translatesAutoresizingMaskIntoConstraints = false
67105
addArrangedSubview(controlBar)
68106
controlBar.heightAnchor.constraint(equalToConstant: 60).isActive = true
69-
controlBar.backgroundColor = UIColor.lightGray.withAlphaComponent(0.2)
70107

71-
playPauseButton.translatesAutoresizingMaskIntoConstraints = false
72-
addSubview(playPauseButton)
73-
playPauseButton.topAnchor.constraint(equalTo: controlBar.topAnchor).isActive = true
74-
playPauseButton.leftAnchor.constraint(equalTo: controlBar.leftAnchor).isActive = true
75-
playPauseButton.bottomAnchor.constraint(equalTo: controlBar.bottomAnchor).isActive = true
76-
playPauseButton.widthAnchor.constraint(equalTo: controlBar.heightAnchor).isActive = true
108+
playButton.isHidden = false
109+
playButton.translatesAutoresizingMaskIntoConstraints = false
110+
controlBar.addArrangedSubview(playButton)
111+
playButton.topAnchor.constraint(equalTo: controlBar.topAnchor).isActive = true
112+
playButton.leftAnchor.constraint(equalTo: controlBar.leftAnchor).isActive = true
113+
playButton.bottomAnchor.constraint(equalTo: controlBar.bottomAnchor).isActive = true
114+
playButton.widthAnchor.constraint(equalTo: controlBar.heightAnchor).isActive = true
115+
116+
pauseButton.isHidden = true
117+
pauseButton.translatesAutoresizingMaskIntoConstraints = false
118+
controlBar.addArrangedSubview(pauseButton)
119+
pauseButton.topAnchor.constraint(equalTo: controlBar.topAnchor).isActive = true
120+
pauseButton.leftAnchor.constraint(equalTo: controlBar.leftAnchor).isActive = true
121+
pauseButton.bottomAnchor.constraint(equalTo: controlBar.bottomAnchor).isActive = true
122+
pauseButton.widthAnchor.constraint(equalTo: controlBar.heightAnchor).isActive = true
77123

78124
contentTitleView.translatesAutoresizingMaskIntoConstraints = false
79-
addSubview(contentTitleView)
125+
controlBar.addArrangedSubview(contentTitleView)
80126
contentTitleView.topAnchor.constraint(equalTo: controlBar.topAnchor).isActive = true
81127
contentTitleView.rightAnchor.constraint(equalTo: controlBar.rightAnchor).isActive = true
82-
contentTitleView.leftAnchor.constraint(equalTo: playPauseButton.rightAnchor).isActive = true
128+
contentTitleView.leftAnchor.constraint(equalTo: controlBar.leftAnchor, constant: 60).isActive = true
83129
contentTitleView.heightAnchor.constraint(equalTo: controlBar.heightAnchor, multiplier: 0.5).isActive = true
84130

85131
playerTitleView.translatesAutoresizingMaskIntoConstraints = false
86-
addSubview(playerTitleView)
132+
controlBar.addArrangedSubview(playerTitleView)
87133
playerTitleView.bottomAnchor.constraint(equalTo: controlBar.bottomAnchor).isActive = true
88134
playerTitleView.rightAnchor.constraint(equalTo: controlBar.rightAnchor).isActive = true
89-
playerTitleView.leftAnchor.constraint(equalTo: playPauseButton.rightAnchor).isActive = true
135+
playerTitleView.leftAnchor.constraint(equalTo: controlBar.leftAnchor, constant: 60).isActive = true
90136
playerTitleView.heightAnchor.constraint(equalTo: controlBar.heightAnchor, multiplier: 0.5).isActive = true
91137
}
92138

@@ -111,18 +157,31 @@ extension CustomPlayerView : PlayerDelegate {
111157

112158
playerTitleView.text = settings.playerTitle
113159

114-
playPauseButton.isEnabled = true
115160
switch settings.playbackState {
116161
case "playing":
117-
playPauseButton.setImage(UIImage(systemName: "pause"), for: .normal)
118-
playPauseButton.addTarget(self, action: #selector(pause), for: .touchUpInside)
162+
playButton.isHidden = true
163+
pauseButton.isHidden = false
119164
default:
120-
playPauseButton.setImage(UIImage(systemName: "play"), for: .normal)
121-
playPauseButton.addTarget(self, action: #selector(play), for: .touchUpInside)
165+
playButton.isHidden = false
166+
pauseButton.isHidden = true
167+
}
168+
169+
if (event.type == "MetadataLoaded") {
170+
spinnerView.isHidden = true
171+
spinnerView.stopAnimating()
172+
errorView.isHidden = true
173+
controlBar.isHidden = false
174+
if (autoplay) {
175+
playerView.setPlaybackState("playing")
176+
}
122177
}
123178

124-
if (autoplay && event.type == "MetadataLoaded") {
125-
playerView.setPlaybackState("playing")
179+
if (event.type == "NoContentAvailable" || event.type == "PlaybackErrored") {
180+
spinnerView.isHidden = true
181+
spinnerView.stopAnimating()
182+
errorView.isHidden = false
183+
errorView.text = event.description
184+
controlBar.isHidden = true
126185
}
127186
}
128187

0 commit comments

Comments
 (0)