-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathKVolumeSlider.swift
More file actions
149 lines (119 loc) · 4.9 KB
/
KVolumeSlider.swift
File metadata and controls
149 lines (119 loc) · 4.9 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
//
//
// Copyright © 2018 Kenan Atmaca. All rights reserved.
// kenanatmaca.com
//
//
import UIKit
import MediaPlayer
import AVFoundation
fileprivate class CustomProgressView: UIProgressView {
var height:CGFloat = 1.0
var weight:CGFloat = 10.0
override func sizeThatFits(_ size: CGSize) -> CGSize {
let size:CGSize = CGSize.init(width: weight, height: height)
return size
}
}
fileprivate enum Keys {
static let AVAudioSessionOutputKey = "outputVolume"
}
class KVolumeSlider: UIView {
private var session:AVAudioSession = AVAudioSession.sharedInstance()
private var kWindow:UIWindow!
private var volumeView:MPVolumeView = MPVolumeView(frame: CGRect.zero)
private var progressView:CustomProgressView!
private let screen = (UIScreen.main.bounds.size.width,UIScreen.main.bounds.size.height)
private var hiddenBarCounter:Int = 0
var backColor:UIColor = UIColor.gray.withAlphaComponent(0.3) {
didSet {
progressView.backgroundColor = backColor
}
}
init() {
let viewFrame = CGRect.init(x: 10, y: 10, width: screen.0 * 0.15, height: 5)
super.init(frame: viewFrame)
setupViews()
setupAVSession()
setupObservers()
}
override init(frame: CGRect) {
super.init(frame: frame)
setupViews()
setupAVSession()
setupObservers()
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
private func setupViews() {
self.backgroundColor = .clear
kWindow = UIApplication.shared.delegate?.window!
progressView = CustomProgressView(progressViewStyle: .bar)
progressView.height = 7
progressView.weight = self.frame.width
progressView.layer.cornerRadius = round(progressView.height / 2)
progressView.clipsToBounds = true
progressView.frame = self.frame
progressView.tintColor = UIColor.gray.withAlphaComponent(0.3)
progressView.progress = session.outputVolume
progressView.backgroundColor = backColor
progressView.alpha = 0
addSubview(progressView)
volumeView.setVolumeThumbImage(UIImage(), for: UIControlState())
volumeView.isUserInteractionEnabled = false
volumeView.alpha = 0.0001
volumeView.showsRouteButton = false
volumeView.backgroundColor = .clear
addSubview(volumeView)
}
private func setupAVSession() {
do {
try session.setActive(true)
} catch {
print(error.localizedDescription)
}
session.addObserver(self, forKeyPath: Keys.AVAudioSessionOutputKey, options: .new, context: nil)
}
private func setupObservers() {
NotificationCenter.default.addObserver(self, selector: #selector(applicationWillResignActive(notification:)), name: .UIApplicationWillResignActive, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(applicationDidBecomeActive(notification:)), name: .UIApplicationWillEnterForeground, object: nil)
}
private func showProgressView(_ val:Float) {
if hiddenBarCounter == 0 {
kWindow.windowLevel = UIWindowLevelStatusBar + 1
}
hiddenBarCounter += 1
UIView.animate(withDuration: 0.3, delay: 0, options: [.curveEaseIn], animations: {
self.progressView.alpha = 1
self.progressView.progress = val
}, completion: { (finish) in
UIView.animate(withDuration: 2, animations: {
self.progressView.alpha = 0
}, completion: { (finish) in
self.hiddenBarCounter -= 1
if self.hiddenBarCounter == 0 {
self.kWindow.windowLevel = UIWindowLevelNormal
}
})
})
}
override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {
guard let change = change, let value = change[.newKey] as? Float, keyPath == Keys.AVAudioSessionOutputKey else {
return
}
showProgressView(value)
}
@objc func applicationWillResignActive(notification: Notification) {
session.removeObserver(self, forKeyPath: Keys.AVAudioSessionOutputKey, context: nil)
}
@objc func applicationDidBecomeActive(notification: Notification) {
showProgressView(session.outputVolume)
setupAVSession()
}
deinit {
session.removeObserver(self, forKeyPath: Keys.AVAudioSessionOutputKey, context: nil)
NotificationCenter.default.removeObserver(self, name: .UIApplicationWillEnterForeground, object: nil)
NotificationCenter.default.removeObserver(self, name: .UIApplicationWillResignActive, object: nil)
}
}//