Repository navigation
Expand file tree
/
Copy pathSubtleVolume.swift
More file actions
336 lines (277 loc) · 9.96 KB
/
Copy pathSubtleVolume.swift
File metadata and controls
336 lines (277 loc) · 9.96 KB
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
//
// SubtleVolume.swift
// SubtleVolume
//
// Created by Andrea Mazzini on 05/03/16.
// Copyright © 2016 Fancy Pixel. All rights reserved.
//
import UIKit
import MediaPlayer
import AVFoundation
/**
The style of the volume indicator
- plain: A plain bar
- rounded: A plain bar with rounded corners
*/
@objc public enum SubtleVolumeStyle: Int {
case plain
case rounded
}
/**
The entry and exit animation of the volume indicator
- None: The indicator is always visible
- SlideDown: The indicator fades in/out and slides from/to the top into position
- FadeIn: The indicator fades in and out
*/
@objc public enum SubtleVolumeAnimation: Int {
case none
case slideDown
case fadeIn
}
/**
Errors being thrown by `SubtleError`.
- unableToChangeVolumeLevel: `SubtleVolume` was unable to change audio level
*/
public enum SubtleVolumeError: Error {
case unableToChangeVolumeLevel
}
/**
Delegate protocol fo `SubtleVolume`.
Notifies the delegate when a change is about to happen (before the entry animation)
and when a change occurred (and the exit animation is complete)
*/
@objc public protocol SubtleVolumeDelegate {
/**
The volume is about to change. This is fired before performing any entry animation
- parameter subtleVolume: The current instance of `SubtleVolume`
- parameter value: The value of the volume (between 0 an 1.0)
*/
@objc optional func subtleVolume(_ subtleVolume: SubtleVolume, willChange value: Double)
/**
The volume did change. This is fired after the exit animation is done
- parameter subtleVolume: The current instance of `SubtleVolume`
- parameter value: The value of the volume (between 0 an 1.0)
*/
@objc optional func subtleVolume(_ subtleVolume: SubtleVolume, didChange value: Double)
/**
The volume did change. This is fired after the exit animation is done
- parameter subtleVolume: The current instance of `SubtleVolume`
- parameter value: The value of the volume (between 0 an 1.0)
- returns: An optional UIImage displayed near the bar
*/
@objc optional func subtleVolume(_ subtleVolume: SubtleVolume, accessoryFor value: Double) -> UIImage?
}
/**
Replace the system volume popup with a more subtle way to display the volume
when the user changes it with the volume rocker.
*/
@objc open class SubtleVolume: UIView {
/**
The style of the volume indicator
*/
open fileprivate(set) var style = SubtleVolumeStyle.plain
/**
The entry and exit animation of the indicator. The animation is triggered by the volume
If the animation is set to `.None`, the volume indicator is always visible
*/
open var animation = SubtleVolumeAnimation.none {
didSet {
updateVolume(volumeLevel, animated: false)
}
}
open var barBackgroundColor = UIColor.clear {
didSet {
container.backgroundColor = barBackgroundColor
}
}
open var barTintColor = UIColor.white {
didSet {
overlay.backgroundColor = barTintColor
}
}
open weak var delegate: SubtleVolumeDelegate?
fileprivate let volume = MPVolumeView(frame: .zero)
fileprivate let overlay = UIView(frame: .zero)
fileprivate let container = UIView(frame: .zero)
fileprivate let accessory = UIImageView(frame: .zero)
/// Returns the current volume. Read-only
public fileprivate(set) var volumeLevel = Double(0)
/// Returns the animation state. Read-only
public fileprivate(set) var isAnimating = false
/// Returns the default volume bump when you programmatically change the volume
public static let DefaultVolumeStep: Double = 0.05
/// Padding for the inner volume bar
public var padding: CGSize = .zero
private var audioSessionOutputVolumeObserver: Any?
/// Initialize with a style and a frame
///
/// - Parameters:
/// - style: the SubtleVolumeStyle
/// - frame: the view's frame
@objc convenience public init(style: SubtleVolumeStyle, frame: CGRect) {
self.init(frame: frame)
self.style = style
}
/// Initialize with a style and a frame
///
/// - Parameters:
/// - style: the SubtleVolumeStyle
@objc convenience public init(style: SubtleVolumeStyle) {
self.init(style: style, frame: CGRect.zero)
}
@objc required public init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
setup()
}
@objc required public override init(frame: CGRect) {
super.init(frame: frame)
setup()
}
required public init() {
fatalError("Please use the convenience initializers instead")
}
/// Increase the volume by a given step
///
/// - Parameter delta: the volume increase. The volume goes from 0 to 1, delta must be a Double in that range
/// - Throws: `SubtleVolumeError.unableToChangeVolumeLevel`
@objc public func increaseVolume(by step: Double = DefaultVolumeStep, animated: Bool = false) throws {
try setVolumeLevel(volumeLevel + step, animated: animated)
}
/// Increase the volume by a given step
///
/// - Parameter delta: the volume increase. The volume goes from 0 to 1, delta must be a Double in that range
/// - Throws: `SubtleVolumeError.unableToChangeVolumeLevel`
@objc public func decreaseVolume(by step: Double = DefaultVolumeStep, animated: Bool = false) throws {
try setVolumeLevel(volumeLevel - step, animated: animated)
}
/**
Programatically set the volume level.
- parameter volumeLevel: The new level of volume (between 0 an 1.0)
- parameter animated: Indicating whether the change should be animated
*/
@objc public func setVolumeLevel(_ volumeLevel: Double, animated: Bool = false) throws {
guard let slider = volume.subviews.compactMap({ $0 as? UISlider }).first else {
throw SubtleVolumeError.unableToChangeVolumeLevel
}
var level = volumeLevel
if level < 0 {
level = 0
}
if level > 1 {
level = 1
}
let updateVolume = {
// Trick iOS into thinking that slider value has changed
slider.value = Float(level)
}
// If user opted out of animation, toggle observation for the duration of the change
if !animated {
audioSessionOutputVolumeObserver = nil
updateVolume()
DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 0.1) { [weak self] in
guard let `self` = self else { return }
self.audioSessionOutputVolumeObserver = AVAudioSession.sharedInstance().observe(\.outputVolume) { [weak self] (audioSession, _) in
self?.updateVolume(level, animated: true)
}
}
} else {
updateVolume()
}
}
/// Resume audio session. Call this once the app becomes active after being pushed in background
@objc public func resume() {
do {
try AVAudioSession.sharedInstance().setActive(true)
} catch {
print("Unable to initialize AVAudioSession")
return
}
}
fileprivate func setup() {
resume()
updateVolume(Double(AVAudioSession.sharedInstance().outputVolume), animated: false)
self.audioSessionOutputVolumeObserver = AVAudioSession.sharedInstance().observe(\.outputVolume) { [weak self] (audioSession, _) in
guard let `self` = self else { return }
self.updateVolume(Double(audioSession.outputVolume), animated: true)
}
backgroundColor = .clear
container.backgroundColor = .clear
volume.setVolumeThumbImage(UIImage(), for: UIControl.State())
volume.isUserInteractionEnabled = false
volume.alpha = 0.0001
volume.showsRouteButton = false
addSubview(volume)
overlay.backgroundColor = .white
container.addSubview(overlay)
addSubview(accessory)
addSubview(container)
}
open override func layoutSubviews() {
super.layoutSubviews()
switch style {
case .plain:
container.layer.cornerRadius = 0
overlay.layer.cornerRadius = 0
case .rounded:
container.layer.cornerRadius = container.frame.height / 2
overlay.layer.cornerRadius = container.frame.height / 2
}
}
fileprivate func updateVolume(_ value: Double, animated: Bool) {
delegate?.subtleVolume?(self, willChange: value)
let previous = volumeLevel
volumeLevel = value
let image = delegate?.subtleVolume?(self, accessoryFor: volumeLevel)
accessory.image = image
var insets = UIEdgeInsets.zero
if image != nil {
insets.left += (frame.height)
accessory.frame = CGRect(x: 0, y: 0, width: frame.height, height: frame.height)
}
insets.left += padding.width
insets.right += padding.width
insets.bottom += padding.height
insets.top += padding.height
let width = frame.size.width - insets.left - insets.right
let height = frame.size.height - insets.top - insets.bottom
overlay.frame = frame
container.frame = CGRect(x: insets.left, y: insets.top, width: width, height: height)
overlay.frame = CGRect(x: 0, y: 0, width: container.frame.width * CGFloat(previous), height: height)
if animated {
isAnimating = true
}
UIView.animate(withDuration: animated ? 0.1 : 0, animations: { () -> Void in
self.overlay.frame.size.width = self.container.frame.width * CGFloat(self.volumeLevel)
})
UIView.animateKeyframes(withDuration: animated ? 2 : 0, delay: 0, options: .beginFromCurrentState, animations: { () -> Void in
UIView.addKeyframe(withRelativeStartTime: 0, relativeDuration: 0.2, animations: {
switch self.animation {
case .none: break
case .fadeIn:
self.alpha = 1
case .slideDown:
self.alpha = 1
self.transform = CGAffineTransform.identity
}
})
UIView.addKeyframe(withRelativeStartTime: 0.8, relativeDuration: 0.2, animations: { () -> Void in
switch self.animation {
case .none: break
case .fadeIn:
self.alpha = 0.0001
case .slideDown:
self.alpha = 0.0001
self.transform = CGAffineTransform(translationX: 0, y: -self.frame.height)
}
})
}) { finished in
if self.isAnimating {
self.isAnimating = !finished
}
self.delegate?.subtleVolume?(self, didChange: value)
}
}
deinit {
NotificationCenter.default.removeObserver(self)
}
}